HDU ACM 1257 最少拦截系统 (贪心法)
http://acm.hdu.edu.cn/showproblem.php?pid=1257
用贪心的思想,每一个系统尽可能多的去拦截导弹.
//View Code
1 #include <iostream> 2 #include <algorithm> 3 #include <stack> 4 using namespace std; 5 const int MAX = 1000 + 10; 6 const int INF = 0x3fffffff; 7 struct Mouse 8 { 9 int fat; 10 int speed; 11 int num; 12 }; 13 bool cmp(Mouse a,Mouse b) 14 { 15 if(a.fat != b.fat ) 16 { 17 return a.fat < b.fat; 18 } 19 else 20 { 21 if(a.speed != b.speed ) 22 { 23 return a.fat > b.fat; 24 } 25 else 26 { 27 return a.num < b.num; 28 } 29 } 30 } 31 int main() 32 { 33 int f,s; 34 int n = 1; 35 Mouse m[MAX] = {0}; 36 while(cin>>f>>s) 37 { 38 39 m[n].fat = f; 40 m[n].speed = s; 41 m[n].num = n; 42 n++; 43 } 44 sort(m+1,m+n,cmp); 45 int father[MAX] = {0}; 46 int i,j; 47 int max = 0; 48 int mark = 0; 49 for(i=1;i<n;i++) 50 { 51 father[i] = 1; 52 for(j=1;j<i;j++) 53 { 54 if(m[j].fat < m[i].fat && m[j].speed > m[i].speed && father[i] < father[j] + 1) 55 { 56 father[i] = father[j] + 1; 57 if(father[i] > max) 58 { 59 max = father[i]; 60 mark = i; 61 } 62 } 63 } 64 } 65 int now = max; 66 cout<<max<<endl; 67 stack <int> stack; 68 for(i=mark;i>0;i--) 69 { 70 if(father[i] == now) 71 { 72 now--; 73 stack.push(m[i].num); 74 } 75 } 76 while(!stack.empty()) 77 { 78 cout<<stack.top ()<<endl; 79 stack.pop (); 80 } 81 return 0; 82 }