wa了几次,发现是题目意思没搞懂。
题目:
Description
Long, long ago, country A invented a missile system to destroy the missiles from their enemy. That system can launch only one missile to destroy multiple missiles if the heights of all the missiles form a non-decrease sequence.
But recently, the scientists found that the system is not strong enough. So they invent another missile system. The new system can launch one single missile to destroy many more enemy missiles. Basically, the system can destroy the missile from near to far. When the system is begun, it chooses one enemy missile to destroy, and then destroys a missile whose height is lower and farther than the first missile. The third missile to destroy is higher and farther than the second missile… the odd missile to destroy is higher and farther than the previous one, and the even missile to destroy is lower and farther than the previous one.
Now, given you a list of the height of missiles from near to far, please find the most missiles that can be destroyed by one missile launched by the new system.
Input
The input contains multiple test cases.
In each test case, first line is an integer n (0<n<=1000), which is the number of missiles to destroy. Then follows one line which contains n integers (<=10^9), the height of the missiles followed by distance.
The input is terminated by n=0.
Output
For each case, print the most missiles that can be destroyed in one line.
Sample Input
45 3 2 431 1 10
Sample Output
31
解法:
// Copyright <lijiancheng> [2014]
// Sicily 1685
#include <iostream>
using namespace std;
int main() {
int arr[1000];
int ans;
int t;
while (cin >> t && t) {
ans = 1;
for (int i = 0; i < t; i++) {
cin >> arr[i];
}
int temp = 1;
int start = 1; // 1 means the even, 0 means odd
for (int i = 0; i < t-1; i++) {
start = 1;
for (int j = i+1; j < t; j++) {
if(start) {
if (arr[j] < arr[j-1]) {
temp++;
start = 0;
}
// else {
// break; // 题目说只要后面的比前面的further就好了,所以不用break 下面同理。
// }
}
else {
if (arr[j] > arr[j-1]) {
temp++;
start = 1;
}
// else {
// break;
// }
}
}
if (temp > ans) {
ans = temp;
}
temp = 1;
}
cout << ans << endl;
}
}
这里估计也可以用dp来做,等下我想下怎么做再写写~