HDU 1789 Doing Homework again

来源:互联网 发布:美国指数型基金数据 编辑:程序博客网 时间:2024/05/18 10:12


Description

zichen 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 zichen 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 zichen 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

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

Sample Output

0
35 

分析:使用标记的思想,由于本题的目的是使所扣的分数最低,可看做贪心,先把分数由高到低排序,再把对应的天数依次标记,做过做业标记为1,具体解析见程序中注释

<span style="font-family:Times New Roman;font-size:18px;">#include<stdio.h>#include<algorithm>#include<string.h>using namespace std;struct stu{    int day,score;} a[1010];bool cmp(stu A,stu B){    if(A.score!=B.score)       //可以只将分数从大到小排序,若将天数也进行排序的话,必须也按照从大到小排,因为你                                            的天数是一直再往前递减的。    return A.score>B.score;    else    return A.day>B.day;}int main(){    int i,j,m,n;    scanf("%d",&n);    while(n--)    {        scanf("%d",&m);        for(i=0; i<m; i++)            scanf("%d",&a[i].day);        for(i=0; i<m; i++)            scanf("%d",&a[i].score);        sort(a,a+m,cmp);        int d[1010]= {0},sum=0;        for(i=0; i<m; i++)        {            while(d[a[i].day]==1)  //  若某一天做过作业了,就将天数往前递减,一直减到没有做过作业的那一天,                a[i].day--;            if(a[i].day>=1)                d[a[i].day]=1;  //</span><span style="font-family: 'Times New Roman'; font-size: 18px;">判断这一天的天数是否大于等于一,若是,说明仍有时间                                          做,将该天标记为1,证明该天有 作业做</span><span style="font-family:Times New Roman;font-size:18px;">            else                sum+=a[i].score;  //若不是,就说明没有时间做某一科作业,说明这一科不得不放弃,将其扣的分数加起来        }        printf("%d\n",sum);        memset(d,0,sizeof(d)); // 每循环一次,都将d数组进行清零操作。    }    return 0;}</span>



0 0