HDU1789 解题报告

来源:互联网 发布:环信 服务端开发 java 编辑:程序博客网 时间:2024/05/16 23:54

Doing Homework again

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 14505    Accepted Submission(s): 8452


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.

 

Output
For each test case, you should output the smallest total reduced score, one line per test case.
 

Sample Input
333 3 310 5 131 3 16 2 371 4 6 4 2 4 33 2 1 7 6 5 4
 

Sample Output
035
 

Author
lcy
 

Source
2007省赛集训队练习赛(10)_以此感谢DOOMIII
 

Recommend
lcy   |   We have carefully selected several similar problems for you:  1257 1978 1421 2159 1159 

解法:贪心

将所有的作业按照ddl从大到小排列
从最大的ddl开始,天数依次递减,直到第一天为止
举个例子
假设当前的ddl为5,则将ddl大于或者等于5的,而且没做过的作业中reduce最大的取出来做掉
然后将ddl减小为4,重复上述步骤……

#include<iostream>#include<algorithm>using namespace std;struct homework{    int ddl;    int rdc;    bool used;};const int maxn=1000+10;homework hk[maxn];int select[maxn];int n;int rdc_all;bool cmp(const homework &hk1,const homework &hk2);void solve();int main(){    int t;    cin>>t;    while(t--)    {        cin>>n;        rdc_all=0;        for(int i=0;i<n;i++) cin>>hk[i].ddl;        for(int i=0;i<n;i++) cin>>hk[i].rdc;        for(int i=0;i<n;i++)        {            rdc_all+=hk[i].rdc;            hk[i].used=false;        }        solve();    }    return 0;}bool cmp(const homework &hk1,const homework &hk2){    if(hk1.ddl!=hk2.ddl) return hk1.ddl>hk2.ddl;    else return hk1.rdc>hk2.rdc;}void solve(){    sort(hk,hk+n,cmp);    int max_ddl=hk[0].ddl;    while(max_ddl)    {        int max_rdc=0;        int max_rdc_index;        for(int i=0;i<n;i++)        {            if(hk[i].ddl<max_ddl) break;            if(!hk[i].used&&hk[i].rdc>max_rdc)            {                max_rdc=hk[i].rdc;                max_rdc_index=i;            }        }        hk[max_rdc_index].used=true;        rdc_all-=max_rdc;        max_ddl--;    }    cout<<rdc_all<<endl;}

原创粉丝点击