HDU-1789Doing Homework Again

xiaoxiao2021-02-28  32

Doing Homework again

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 贪心算法的应用。 扣分多的作业当然应该优先安排在前面,所以先把家庭作业按扣分从大到小排列。 然后就是要考虑截止日期了。 贪心算法的策略也是体现在这里。要扣的分最少嘛,而且所有作业一天就能完成,这个时候你当然会选择在最后一天完成啦。 但是若有两个作业的截止日期相同,那就将第二个作业安排到截止日期前一天,也就是与截止日期最近的一天,这个时候就需要一个数组来标记那些天已经被占用(也就是代码中的occupied数组)。 AC代码如下: #include<stdio.h> #include<algorithm> #include<cstring> using namespace std; struct hw { int deadline; int score; bool operator<(const hw &b)const { return score == b.score ? deadline<b.deadline : score>b.score; } }h[1005]; int main(void) { int t; scanf("%d", &t); while (t--) { int n,max=-1; //max记录最大截止日期 scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &h[i].deadline); if (h[i].deadline > max) max = h[i].deadline; } int *occupied = new int[max + 1]; //occupied[i]表示第i天已被占用 memset(occupied, 0, (max+1)*sizeof(int)); for (int i = 0; i < n; i++) scanf("%d", &h[i].score); sort(h, h + n); int sum = 0; for (int i = 0; i < n;i++) { int t = h[i].deadline,j; for (j = t; j >= 1; j--) { if (!occupied[j]) { occupied[j] = 1; break; } } if (j < 1) sum += h[i].score; } printf("%d\n", sum); } return 0; }
转载请注明原文地址: https://www.6miu.com/read-1950041.html

最新回复(0)