-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1044.cpp
75 lines (61 loc) · 1.11 KB
/
1044.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
题目:p1044 拦截导弹
*/
#include <iostream>
#include <vector>
using namespace std;
//就是求最长递减子序列
int MaxMissile(vector<unsigned int> heights)
{
int dp[heights.size()]; //表示以h[i]结尾的递减子序列的最长长度
int i,j;
for(i=0;i<heights.size();i++)
{
dp[i] = 1;
for(j=0;j<i;j++)
{
if(heights[j]>=heights[i]&&dp[j]+1>dp[i])
dp[i] = dp[j]+1;
}
}
int max = 0;
for(i=0;i<heights.size();i++)
{
if(dp[i]>max)
max = dp[i];
}
return max;
}
// 就是求最长递增子序列,因为在递增子序列中,有一项,就意味着需要一个系统来拦截这颗导弹
int MinSys(vector<unsigned int> heights)
{
int dp[heights.size()]; //表示以h[i]结尾的递增子序列的最长长度
int i,j;
for(i=0;i<heights.size();i++)
{
dp[i] = 1;
for(j=0;j<i;j++)
{
if(heights[j]<heights[i]&&dp[j]+1>dp[i])
dp[i] = dp[j]+1;
}
}
int max = 0;
for(i=0;i<heights.size();i++)
{
if(dp[i]>max)
max = dp[i];
}
return max;
}
int main()
{
vector<unsigned int> heights;
int temp;
while(cin>>temp)
heights.push_back(temp);
cout << MaxMissile(heights) << endl;
cout << MinSys(heights) << endl;
return 0;
}