HDU 1789 贪心

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
3
3
3 3 3
10 5 1
3
1 3 1
6 2 3
7
1 4 6 4 2 4 3
3 2 1 7 6 5 4 

Sample Output
0
3
5
题意:

一个娃娃做作业,没做完的要扣分,每门作业都有最后期限。问如何安排扣分最少。

输入第一行是几组测试样例,样例第一行是有几门作业,第二行是对应分数。输出最小的扣分。


思路:
先处理分数大的以减少扣分。
将分数按照从大到小进行排序,按排好的顺序填入数组,当天未被占用就填入,若被占用则访问上一天,直到访问完毕,若依然未填入即未完成作业,将分数记录下来,重复操作,最后输出。
#include<bits/stdc++.h>
using namespace std;
int use[1010];//记录该天是否做作业 
typedef struct node
{
    int deadline;
    int score;
}ss;//便于储存、排序 
int cmp(ss a,ss b){
    if(a.score!=b.score)
    return a.score>b.score;
    else
    return a.deadline<b.deadline;
}
int main()
{
    int N;
    cin>>N;
    while(N--)
    {
        int n;
        cin>>n;
        ss a[1010];
        for(int i=0;i<n;i++)
        cin>>a[i].deadline;
        for(int i=0;i<n;i++)
            cin>>a[i].score;
        sort(a,a+n,cmp);//按照分数进行排序 
        int sum=0,j; //sum记录未完成分数 
        memset(use,0,sizeof(use));
        for(int i=0;i<n;i++)//按照分数从大到小的顺序进行循环 
        {
            for(j=a[i].deadline;j>0;j--)//天数递减,即确保最大分数优先完成 
            {
                if(!use[j])// 如果该天还没有任务 
                {
                use[j]=1;//标记改为1 即该天有任务 
                break;
                }
            }
            if(j==0)//如果未能完成 加入sum中 
            {
                sum+=a[i].score;
//                cout<<sum<<endl;
            }
        }
        cout<<sum<<endl;
    }
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务