hdu 1789 Doing Homework again

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1789

贪心  很不错的

View Code
 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <algorithm>
 5 using namespace std;
 6 struct  ss {
 7     int x,y;
 8 }num[1005];
 9 int flag[1005];
10 int cmp(ss s,ss t)
11 {
12     if(s.y==t.y)return s.x<t.x;
13     else return s.y>t.y;
14     
15 }
16 int main()
17 {
18     int i,j,k,n,ans;
19     scanf("%d",&k);
20     while (k--)
21     {
22         ans=0;
23         scanf("%d",&n);
24         for (i=1;i<=n;i++)
25             scanf("%d",&num[i].x);
26         for (i=1;i<=n;i++)
27             scanf("%d",&num[i].y);
28         sort(num+1,num+1+n,cmp);
29         memset(flag,0,sizeof(flag));
30         for (i=1;i<=n;i++)
31         {
32             for (j=num[i].x;j>=1;j--)
33                 if(!flag[j])
34                 {
35                     flag[j]=1;
36                     break;
37                 }
38                 if(j==0)ans+=num[i].y;
39         }
40         printf("%d\n",ans);
41     }
42     return 0;
43 }
posted @ 2012-04-10 21:23  我们一直在努力  阅读(151)  评论(0编辑  收藏  举报