当前位置:网站首页>Yugu p1020 missile interception (binary search)
Yugu p1020 missile interception (binary search)
2022-07-07 07:59:00 【zjsru_ Beginner】
subject
In order to defend against the missile attack of the enemy , Developing a missile interception system . But there's a flaw in this interceptor system : Although its first shell can reach any height , But in the future, each shell can not be higher than the height of the previous one . One day , The radar caught the enemy's missiles coming . Because the system is still in the trial stage , So there's only one system , So it may not be possible to intercept all missiles .
Enter the Missile Altitude in turn ( The altitude data given by the radar is \le 50000≤50000 The positive integer ), Calculate how many missiles this system can intercept at most , If you want to intercept all missiles, at least how many of these missile interception systems should be equipped .
Input format
1 That's ok , Several integers ( Number \le 100000≤100000)
NOIP The data scale of the original question does not exceed 2000.
Output format
2 That's ok , One integer per row , The first number shows how many missiles the system can intercept at most , The second number indicates the minimum number of such missile interception systems to be equipped if all missiles are to be intercepted .
sample input
389 207 155 300 299 170 158 65
sample output
6 2
Design thinking
This question requires a non rising sequence length and a rising sequence length , Mainly used lower_bound And upper_bound To find out , First create an initial length of 1 Of len1 and len2,d1[1] and d2[1], As in the example above , First the 389 Add to an array , Compare again 389 And the next number 207, obviously 207 smaller , And add it to the array , Continue back until the number 300 when , Cannot be directly added to the array , You must replace the first one in the array that is less than 300 The number of , namely 207. Add and replace many times , It finds out the maximum number of missiles that can be intercepted and the minimum system to be equipped .
The following description follows lower_bound And upper_bound Usage of :
Both of these functions can only be used for ascending sequences , The following example :
int main()
{
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
cout << (lower_bound(a, a + 12, 4) - a) << endl; // Output 3
cout << (upper_bound(a, a + 12, 4) - a) << endl; // Output 4
}
If the sequence is descending :
int a[] = {4, 4, 4, 3, 3, 3, 2, 2, 2, 1, 1, 1};
cout << (lower_bound(a, a + 12, 4) - a) << endl; // Output 12
cout << (upper_bound(a, a + 12, 4) - a) << endl; // Output 12
cout << (lower_bound(a, a + 12, 1) - a) << endl; // Output 0
cout << (upper_bound(a, a + 12, 1) - a) << endl; // Output 0
The code is as follows :
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 100010;
int a[N], d1[N], d2[N], n;
int main() {
while (cin >> a[++n]); n--;
int len1 = 1, len2 = 1; // The initial length is 1
d1[1] = a[1]; // Used to calculate the length of Non rising sequence
d2[1] = a[1]; // Used to find the length of ascending sequence
for (int i=2; i<=n; i++) { // from a[2] Start enumerating each number
if (d1[len1] >= a[i]) d1[++len1] = a[i]; // If you meet the requirements and don't rise, join d1
else { // Otherwise, use a[i] Replace d1 One of the numbers
int p1 = upper_bound(d1 + 1, d1 + 1 + len1, a[i], greater<int>()) - d1;
d1[p1] = a[i];
}
if (d2[len2] < a[i]) d2[++len2] = a[i];
else {
int p2 = lower_bound(d2 + 1, d2 + 1 + len2, a[i]) - d2;
d2[p2] = a[i];
}
}
cout << len1 << endl << len2;
return 0;
}
Running results :
big data 201 tyx
边栏推荐
- Operation suggestions for today's spot Silver
- Binary tree and heap building in C language
- Li Kou interview question 04.01 Path between nodes
- Pytest+allure+jenkins environment -- completion of pit filling
- 自定义类加载器加载网络Class
- The principle and implementation of buffer playback of large video files
- php导出百万数据
- 探索Cassandra的去中心化分布式架构
- Qt学习27 应用程序中的主窗口
- [Stanford Jiwang cs144 project] lab3: tcpsender
猜你喜欢
【经验分享】如何为visio扩展云服务图标
IO stream file
Shell 脚本的替换功能实现
Open source ecosystem | create a vibrant open source community and jointly build a new open source ecosystem!
Iterable、Collection、List 的常见方法签名以及含义
Linux server development, redis source code storage principle and data model
这5个摸鱼神器太火了!程序员:知道了快删!
【p2p】本地抓包
Idea add class annotation template and method template
Introduction to basic components of wechat applet
随机推荐
[experience sharing] how to expand the cloud service icon for Visio
Value sequence (subsequence contribution problem)
Iterable、Collection、List 的常见方法签名以及含义
Mysql高低版本切换需要修改的配置5-8(此处以aicode为例)
自定义类加载器加载网络Class
大视频文件的缓冲播放原理以及实现
【webrtc】m98 screen和window采集
Installing postgresql11 database under centos7
[webrtc] m98 Screen and Window Collection
Qt学习26 布局管理综合实例
C语言通信行程卡后台系统
C language flight booking system
[performance pressure test] how to do a good job of performance pressure test?
Introduction to basic components of wechat applet
pytest+allure+jenkins安装问题:pytest: error: unrecognized arguments: --alluredir
These five fishing artifacts are too hot! Programmer: I know, delete it quickly!
leanote私有云笔记搭建
通信设备商,到底有哪些岗位?
[unity] several ideas about circular motion of objects
Rust Versus Go(哪种是我的首选语言?)