贪心初步-A - Doing Homework again
Doing Homework again
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7494 Accepted Submission(s): 4429
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
Author
lcy
Source
Recommend
lcy
对于其他人来说这是一道再简单不过的水题,但是对于我来说,我是尝试了很多遍都没成功,刚开始头脑简单地直接用优先队列去做排序标准则定成分数除以截至日期,但是无法得到整体的最优解,因为这是一个很荒谬的定法,脱离了题目所定下的条件。所以后来经过讨论,我终于发现应当按照先有一个变量作为时间线,每个最终期限都填入时间线,每个最终期限里都填入这个期限对应的最大的分数,然后空出来的日子里就填入剩下时间期限未到的元素中分值最大的。这样剩下的元素就是所要的答案。
其实还可以先把所有要扣除的分数都加在一起,然后逐个减掉所要扣除的分数。然后得到最优解。
下面插入还没有写好的的代码,即错误的初步代码。其实应该用vector容器来做,自己写好排序规则利用qsort函数。
#include<cstdio> #include<string> #include<cstdlib> #include<queue> #define maxn1 1005 #define maxn2 1005 using namespace std; struct Work{ int daat,score; double va; }ha[maxn1]; bool operator < (struct Work a,struct Work b){ if(a.daat != b.daat){ if(a.daat > b.daat) return true; else return false; }else{ if(a.score < b.score) return true; else return false; } } int main(){ int t,n; int wor; scanf("%d",&t); while(t--){ priority_queue<struct Work> lis; scanf("%d",&n); for(int i = 0;i < n;i++){ scanf("%d",&ha[i].daat); } for(int i = 0;i < n;i++){ scanf("%d",&ha[i].score); } int fuck = 0; for(int i = 0;i < n;i++){ ha[i].va = ha[i].score*1.0 /ha[i].daat; lis.push(ha[i]); fuck += ha[i].score; } int useddate = 1; while(!lis.empty() && fuck >= 0){ struct Work t; t = lis.top(); if(t.daat == useddate){ lis.pop(); fuck -= t.score; useddate++; } else if(t.daat < useddate){ lis.pop(); } else if(t.daat > useddate){ //此循环内的问题出在如何找到剩余元素中的最大值。 struct Work temp; temp = t; int blw = t.score; while(){ } fuck -= blw; useddate++; } } printf("%d\n",fuck); } return 0; }