HDU 1789

来源:互联网 发布:华语乐坛现状知乎 编辑:程序博客网 时间:2024/06/05 12:50

Doing Homework again

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


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

 


说一下题目的思路(做法):

肯定是先做分数高的,所以对分数进行由小到大的排序,完成分数最高的之后找分数第二高的,往分数最高的前面排,再是第三高的,,在往前排若该天有未完成的任务,则放弃该天的任务,算作扣的分数里,再以此类推。


解释一下最后一组测试数据:

排好序后就是 7 6  5  4  3  2    1,对应的天数为:4   2    4    1    4   6;

是这么排的: 1   2  3   4   5       6

                                      7(第四天完成)

1   2     3    4    5    6

             6   7     6(第三天完成)

1    2     3     4    5    6

       5    6      7(5第二天完成)

1     2     3    4    5     6

4    5     6    7(4第一天完成)

1    2   3   4   5   6

4     5   6   7         6(其中再是3,2,但是3,2应该完成的时间被占了,所以不能完成,最后剩下了1,最后一天完成)


已经AC过的代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
struct M
{
    int date,score;
} A[1005];//定义一个结构体,把天数和分数放在一起
int bigger(const M &a,const M &b)
{
    return a.score>b.score;
}使用结构体排序
int main()
{
    int t,n;
    int in[1005];
    scanf("%d",&t);
    while(t--)
    {
        int ans=0;
        memset(in,0,sizeof(in));
        scanf("%d",&n);
        for(int i=0; i<n; i++)
        {
            scanf("%d",&A[i].date);
        }
        for(int i=0; i<n; i++)
            scanf("%d",&A[i].score);
        sort(A,A+n,bigger);//分数进行由大到小排序
        for(int i=0; i<n; i++)//先从最大的开始,逐渐递减
        {
            int j;
            for(j=A[i].date; j>=1; j--)//最大的在该天完成,接着天数递减
            {
                if(!in[j])//如果该天没有被其他的任务,则为真。若该天有任务,但是时间已经被其他的科目占据,则为假,若没有,则为真。
                {
                    in[j]=true;
                    break;//如果没有被占据,则跳出循环
                }
            }
            if(!j)
                ans+=A[i].score;//如果该天完成了其他作业,而应该在这天写的作业未完成,则加上去
        }
        cout<<ans<<'\n';
    }
    return 0;
}

0 0