Total Submission(s) : 18   Accepted Submission(s) : 13
Problem Description
Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every teacher gives him a deadline of handing in the homework. If Ignatius hands in the homework after the deadline, the teacher will reduce his score of the final test. And now we assume that doing everyone homework always takes one day. So Ignatius wants you to help him to arrange the order of doing homework to minimize the reduced score.

Input
The input contains several test cases. The first line of the input is a single integer T that is the number of test cases. T test cases follow.
Each test case start with a positive integer N(1<=N<=1000) which indicate the number of homework.. Then 2 lines follow. The first line contains N integers that indicate the deadlines of the subjects, and the next line contains N integers that indicate the reduced scores.

Output
For each test case, you should output the smallest total reduced score, one line per test case.

Sample Input
`333 3 310 5 131 3 16 2 371 4 6 4 2 4 33 2 1 7 6 5 4`

Sample Output
`035`

1 #include<iostream>
2 #include<algorithm>
3 using namespace std;
4 struct homework
5 {
6     int day;
7     int score;
8 };
9 bool cmp(homework a,homework b)
10 {
11     if(a.day==b.day)
12         return a.score>b.score;//假如天数一样，就比较分数大小，大的放前面
13     else
14         return a.day<b.day;
15 }
16 int main()
17 {
18     int T,i,j,min;
19     int sign[1001];
20     homework a[1001];
21     int n,sum,temp,d;
22     scanf("%d",&T);
23     while(T--)
24     {
25         scanf("%d",&n);
26         for(i=0;i<n;i++)
27             scanf("%d",&a[i].day);
28         for(i=0;i<n;i++)
29             scanf("%d",&a[i].score);
30         sum=0;
31         sort(a,a+n,cmp);//排序
32         memset(sign,0,sizeof(sign));
33         d=1,temp=0;
34         for(i=0;i<n;i++)
35         {
36             if(a[i].day>=d)//假如该天没作业,把a[i]定在该天完成
37             {
38                 sign[i]=1;
39                 d++;
40             }
41             else //假如有已经有作业了，则找出分数最小的与之交换
42             {
43                 min=a[i].score;
44                 temp=i;//temp为最小的分数的位置
45                 for(j=i-1;j>=0;j--)
46                     if(sign[j]==1&&a[j].score<min)
47                     {
48                         min=a[j].score;
49                         temp=j;
50
51                     }
52                 sum+=a[temp].score;//sum相加
53                 a[temp].score=a[i].score;//交换
54
55             }
56         }
57         printf("%d\n",sum);
58     }
59 }
60
61
posted on 2013-01-12 23:04 天YU地___PS，代码人生 阅读(543) 评论(0)  编辑  收藏

 只有注册用户登录后才能发表评论。 网站导航:
 < 2013年1月 >
303112345
6789101112
13141516171819
20212223242526
272829303112
3456789

一定要好好学习，天天向上!

•