1. 程式人生 > >hdu1789 Doing Homework again(貪心+排序)

hdu1789 Doing Homework again(貪心+排序)

Doing Homework again

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 18294    Accepted Submission(s): 10648


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<bits/stdc++.h>
 2 using namespace std;
 3 struct node
 4 {
 5     int day,score;
 6     int flag;
 7 } a[1005];
 8 bool cmp(node x,node y)
 9 {
10     if(x.day==y.day)
11     {
12         return x.score>y.score;
13     }
14     else
15     {
16         return x.day<y.day;
17     }
18 }
19 void init()
20 {
21     for(int i=0; i<1005; i++)
22     {
23         a[i].day=0;
24         a[i].score=0;a[i].flag=1;
25     }
26 }
27 int main()
28 {
29     int t;
30     while(~scanf("%d",&t))
31     {
32         while(t--)
33         {
34             int n;
35             scanf("%d",&n);
36             init();
37             for(int i=0;i<n;i++)
38             {
39                 scanf("%d",&a[i].day);
40             }
41             for(int i=0;i<n;i++)
42             {
43                 scanf("%d",&a[i].score);
44             }
45             sort(a,a+n,cmp);
46             int temp=1,ans=0;
47             for(int i=0;i<n;i++)
48             {
49                 if(a[i].day>=temp)
50                 {
51                     temp++;
52                     continue; 
53                 }
54                 int p=a[i].score,pos=i;
55                 for(int j=0;j<i;j++)
56                 {
57                     if(a[j].score<p&&a[j].flag)//前面有耗時少的,而且沒有扣過分 
58                     {
59                         p=a[j].score;
60                         pos=j;
61                     }
62                 }
63                 ans+=p;
64                 a[pos].flag=0;//扣分標記 
65             }
66             printf("%d\n",ans);
67         }
68     }
69     return 0;
70 }