HDU-1789(贪心)
Doing Homework again
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10182 Accepted Submission(s): 5969
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.
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
题意:有个人有n个作业要做,做一个作业要一天,每个作业有截至日期,截至日期到了没做的话,就会扣掉相应的分数。求最少扣多少分。
思路:假设最少扣sum分。将作业按分数从大到小排序。然后将i个作业安排到截至日期那天,如果截至日期那天有作业的话,就往前推一天。如果到最前没都没有地方安排,就加到sum上。
这样安排是为了尽可能多的做分数大的作业。
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algorithm> 5 6 using namespace std; 7 8 #define maxn 1010 9 int t,n; 10 struct node{ 11 int d, s; 12 }no[maxn]; 13 14 //记录该天是否有作业做 15 bool remem[maxn]; 16 17 bool cmp(node& no1, node& no2){ 18 return no1.s > no2.s; 19 } 20 21 int main() 22 { 23 scanf("%d", &t); 24 while(t--){ 25 scanf("%d", &n); 26 for (int i = 0; i < n; i++){ 27 scanf("%d", &no[i].d); 28 } 29 for (int i = 0; i < n; i++){ 30 scanf("%d", &no[i].s); 31 } 32 33 memset(remem, false, sizeof(remem)); 34 sort(no, no + n, cmp); 35 36 int sum = 0; 37 for (int i = 0; i < n; i++){ 38 int j = no[i].d; 39 //天数从第一天开始 40 while(j > 0 && remem[j]) j--; 41 //天数从第一天开始,所以不能小于1 42 if(j < 1) sum += no[i].s; 43 else{ 44 remem[j] = true; 45 } 46 } 47 48 printf("%d\n", sum); 49 } 50 return 0; 51 }

浙公网安备 33010602011771号