HDU 1789 Doing Homework again (贪心)

 Doing Homework again
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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<stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 #define    MAX    1005
 5 
 6 int    main(void)
 7 {
 8     int    t,n;
 9     int    s[MAX][2];
10     int    sum,box,max,max_loc;
11 
12     scanf("%d",&t);
13     while(t --)
14     {
15         sum = box = 0;
16 
17         scanf("%d",&n);
18         for(int i = 0;i < n;i ++)
19             scanf("%d",&s[i][0]);        //0号单元存天数
20         for(int i = 0;i < n;i ++)
21         {
22             scanf("%d",&s[i][1]);        //1号单元存惩罚值
23             sum += s[i][1];
24         }
25         for(int i = n;i >= 1;i --)
26         {
27             max = -1;
28             for(int j = 0;j < n;j ++)
29                 if(s[j][0] >= i && s[j][1] > max)
30                 {
31                     max = s[j][1];
32                     max_loc = j;
33                 }
34             if(max != -1)
35             {
36                 box += max;
37                 s[max_loc][1] = -1;
38             }
39         }
40         printf("%d
",sum - box);
41     }
42 }
原文地址:https://www.cnblogs.com/xz816111/p/4167251.html