hdu1257 least greedy interception system

  Topic links: http://acm.hdu.edu.cn/showproblem.php?pid=1257

   

A country in order to defend against enemy missile attacks, to develop a missile interception system, but such a missile interceptor system has a flaw: Although 
natural its first rounds to reach any height, but each shell and should not exceed ago out of heights. One day, Ray 
of captured enemy missile struck. as the system is still in beta, so only a system, it is possible that not intercept 
any missiles 
how to do it ? build more sets system chanting! tell us Daoman easy it cost? cost is a big problem ah, so Jiu here to help 
, please help calculate how much the system requires a minimum set of interceptors.

  This White Paper is also the question, how much he is not seeking missile interceptors most (in that case is not dropped longest subsequence problem), but the number of systems in order to seek a minimum of missile intercept them all, then obviously each of us comes missiles it had to intercept them, if there is a system to intercept a missile had before us comes the last missile it high, then you can use this system to intercept it, spit more than qualified, apparently with eligible systems are currently the lowest minimum height of the interception, because other higher minimum height can also prepare to intercept higher than the current missile missiles. If the current missile is high, there is no one system can intercept them, it can only open a new system up. Code:

 1 //贪心 
 2 #include<iostream>
 3 #include<cstdio>
 4 using namespace std;
 5 const int maxn=10005;
 6 int n;
 7 int a[maxn];
 8 int f[maxn];
 9 int cnt; 
10 int pos;
11 bool flag;
12 int main()
13 {
14     while(scanf("%d",&n)!=EOF)
15     {
16         cnt=0;
17         for(int i=1;i<=n;++i)scanf("%d",&a[i]);
18         for(int i=1;i<=n;++i)
19         {
20             int cha=10000000;
21             flag=0;
22             for(int j=1;j<=cnt;++j)
23             {
24                 if(f[j]>=a[i]&&f[j]-a[i]<cha)pos=j,flag=1,cha=f[j]-a[i]; 
25             }
26             if(flag)f[pos]=a[i];
27             else f[++cnt]=a[i];
28         }
29         printf("%d\n",cnt);
30     }
31     return 0;
32 }

 

Guess you like

Origin www.cnblogs.com/yuelian/p/11900711.html