hdu 1789 Doing Homework again (贪心)

来源:互联网 发布:ubuntu 文本输入设置 编辑:程序博客网 时间:2024/05/16 11:20

Doing Homework again

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


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


思路:
先按照期限(de)从小到大、罚分(re)从大到小排序,然后依次扫描过去,发现de<t(当前天数),则该作业肯定不能完成,但是不能就直接加上它的re,因为可以将前面的某个作业不做(则空出来一天)来做这个作业,所以再扫描前面做过的作业,找到最小的re,如果re<当前re的,则将那个作业不做来做当前这个作业。这样贪心就能保证得到最优解了。

代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#define maxn 1005#define mod 1000000007#define INF 0x3f3f3f3fusing namespace std;int n,m,ans;int vis[maxn];struct Node{    int de,re;}p[maxn];bool cmp(Node xx,Node yy){    if(xx.de!=yy.de) return xx.de<yy.de;    return xx.re>yy.re;}void solve(){    int i,j,t=1,flag,mi,k;    ans=0;    memset(vis,0,sizeof(vis));    for(i=1;i<=n;i++)    {        if(p[i].de<t)        {            mi=p[i].re;            flag=0;            for(j=1;j<i;j++)            {                if(!vis[j]) continue ;                if(mi>p[j].re)                {                    flag=1;                    mi=p[j].re;                    k=j;                }            }            if(flag)            {                vis[k]=0;                vis[i]=1;                ans+=p[k].re;            }            else ans+=p[i].re;        }        else t++,vis[i]=1;    }}int main(){    int i,j,t;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        for(i=1;i<=n;i++)        {            scanf("%d",&p[i].de);        }        for(i=1;i<=n;i++)        {            scanf("%d",&p[i].re);        }        sort(p+1,p+n+1,cmp);        solve();        printf("%d\n",ans);    }    return 0;}




 
原创粉丝点击