Doing homework again
Time Limit: 1000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 6925 accepted submission (s): 4124
Problem descriptionignatius 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 specified CED score.
Inputthe 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 specified CED scores.
Outputfor each test case, You shoshould output the smallest total balanced CED score, one line per test case.
Sample input333 3 310 5 131 3 16 2 371 4 6 4 2 33 2 1 7 6 5 4
Sample output035
Note that unlike doing homework...
Greedy: sorts jobs by scores and schedules each job to the end time. If the job is occupied, the job will continue to be blank and the score will be deducted if it is not found.
Why? Because the minimum deduction is required, and each job is completed in one day, it is better to give up a smaller score than to give up a larger score.
#include <iostream>#include <algorithm>#include <cstring>using namespace std;#define N 1010struct course{ int deadtime; int score; bool operator <(const course &t)const { if(score!=t.score) return score>t.score; return deadtime<t.deadtime; }}s[N];int main(){ int vis[N]; int T,n,i,j; scanf("%d",&T); while(T--) { scanf("%d",&n); memset(vis,0,sizeof(vis)); for(i=1;i<=n;i++) { scanf("%d",&s[i].deadtime); } for(i=1;i<=n;i++) { scanf("%d",&s[i].score); } sort(s+1,s+n+1); int ans=0; for(i=1;i<=n;i++) { for(j=s[i].deadtime;j>=1;j--) { if(!vis[j]) { vis[j]=1; break; } } if(j==0) ans+=s[i].score; } cout<<ans<<endl; } return 0;}
Greedy [HDU 1789] Doing homework again