Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
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
3
3
3 3 3
10 5 1
3
1 3 1
6 2 3
7
1 4 6 4 2 4 3
3 2 1 7 6 5 4
Sample Output
該題首先是要排序,將所有項按天數從小到大排序,如果天數一樣的,按照分數從高到低排,排序好后就是貪心了,假如選中的就標記好,每次假如有相同天數的,要分數最小的。具體分析代碼如下:
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


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],flag;
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,代碼人生 閱讀(852)
評論(0) 編輯 收藏