ZZUSOFTOJ interceptor missile (function use)

1116: Intercept Missile

Time limit: 1 Sec Memory limit: 128 MB
Submission: 4 Resolution: 3
[ Submission ] [ Status ] [ Discussion Board ] [Protector: External Import]

Title description

  A certain country has developed a missile interception system in order to defend against enemy missile attacks. However, this missile interception system has a flaw: Although its first shell can reach any height, each subsequent shell cannot be higher than the previous one. One day, the radar caught an enemy missile attack. Since the system is still in the trial phase, there is only one system, so it may not be able to intercept all missiles. 
    Enter the altitude of the missiles in turn (the altitude data given by the radar is a positive integer not greater than 30,000), calculate the maximum number of missiles that this system can intercept, and how many sets of such missile interception systems are required to intercept all missiles. 

 

enter

One line is the height of the missiles flying in turn 

 

Output

The two lines are the maximum number of missiles that can be intercepted and the minimum number of systems to be equipped to intercept all missiles. 

 

Sample input

389 207 155 300 299 170 158 65

 

Sample output

6
2

 

analysis:

The method of the two questions is the same (although it seems fancy and troublesome), that is, sort the overall data in ascending and descending order, and use a function to simplify the operation.

Emphasize the distinction between the following four functions:

lower_bound( begin,end,num)//求数组中(begin到end-1)第一个大于等于num的地址

upper_bound( begin,end,num)//大于

lower_bound( begin,end,num,greater<type>() )//小于等于

upper_bound( begin,end,num,greater<type>() )//小于

Code:

#include<bits/stdc++.h>
using namespace std;

int main()
{
	int a, ans2 = 1, ans1 = 1, ma[10010] = { 0 }, mi[10010] = { 0 };//ma为降序,mi为升序
	cin >> a;
	mi[0] = a;
	ma[0] = a;
	while (scanf("%d", &a) != EOF)
	{
		if (a<ma[ans1 - 1])
		{
			ma[ans1] = a;
			ans1++;
		}
		else
		{
			*upper_bound(ma, ma + ans1, a, greater<int>()) = a;//找出第一个小于a的位置
		}

		if (a>mi[ans2 - 1])
		{
			mi[ans2] = a;
			ans2++;
		}
		else
		{
			*upper_bound(mi, mi + ans2, a) = a;//找出第一个大于a的位置
		}
	}

	cout << ans1 << endl << ans2;
	return 0;
}

 

Guess you like

Origin blog.csdn.net/qq_43700916/article/details/88856091