贪心5 HDU - 1789 题解

Description
gnatius 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<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
class Node{
public:
    int d,s;
    Node(int deadLine,int score):d(deadLine),s(score){}
    bool operator<(const Node &N){
        if(s<N.s)return false;
        else if(s==N.s&&d<N.d)return false;
        else return true;
    }
};
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        int N;
        scanf("%d",&N);
        if(N==0)cout<<0<<endl;
        else {
        vector<Node> v;
        int dead;
        for(int i=0;i<N;i++){
            scanf("%d",&dead);
            v.push_back(Node(dead,0));
        }
        int score;
        for(int i=0;i<N;i++){
            scanf("%d",&score);
            v[i].s=score;
        }
        sort(v.begin(),v.end());
        vector<int> temp;
        int sum=0;
        for(int i=0;i<v.size();i++){
            if(v[i].d>temp.size()){
                temp.resize(v[i].d,0);
                temp[v[i].d-1]=v[i].s;
            }
            else{
                bool bl=true;
                for(int j=v[i].d;j>0;j--)
                    if(temp[j-1]==0){
                        temp[j-1]=v[i].s;
                        bl=false;
                        break;
                    }
                if(bl)sum+=v[i].s;

            }
        }
        cout<<sum<<endl;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值