Doing homework again(好久没A题了,一题做了好久。)

Doing homework again(好久没A题了,一题做了好久。)
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
   
   
   
   
0
3
5
 
该题首先是要排序,将所有项按天数从小到大排序,如果天数一样的,按照分数从高到低排,排序好后就是贪心了,假如选中的就标记好,每次假如有相同天数的,要分数最小的。具体分析代码如下:


 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 

你可能感兴趣的:(Doing homework again(好久没A题了,一题做了好久。))