hdoj-1789-Doing Homework again

来源:互联网 发布:林俊杰 睡粉 知乎 编辑:程序博客网 时间:2024/05/22 14:39


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

有t组测试数据,每组测试数据中有n门功课,第一排完成它们的时间限制,第二排是未在限制的时间内完成的要扣除的分数,然后是需要求扣的分数最少。

很经典的贪心啊

对分数按从大到小排次序,然后枚举限定的时间,若是某一天没有被标记,就用这一天来完成这一门作业,若是枚举到0了,说明在限定时间内没有哪一天可以完成这门作业,那么就扣除这门课的分数

#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>using namespace std;struct ss{    int time,p;}t[100000];int f[100000];int cmp(const ss a,const ss b){    if(a.p>b.p)        return 1;    else if(a.p==b.p&&a.time<b.time)        return 1;    else        return 0;}int main(){    int text,n;    scanf("%d",&text);    while(text--)    {        scanf("%d",&n);        memset(f,0,sizeof(f));        int i,j;        for(i=1;i<=n;i++)            scanf("%d",&t[i].time);        for(i=1;i<=n;i++)            scanf("%d",&t[i].p);        sort(t+1,t+1+n,cmp);        int sum=0;        for(i=1;i<=n;i++)        {            for(j=t[i].time;j>=1;j--)                if(!f[j])                {                    f[j]=1;                    break;                }            if(j==0)                sum+=t[i].p;        }        printf("%d\n",sum);    }    return 0;}


0 0
原创粉丝点击