贪心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
#include
#include
#include
using namespace std;
class Node{
public:
    int d,s;
    Node(int deadLine,int score):d(deadLine),s(score){}
    bool operator<(const Node &N){
        if(sreturn false;
        else if(s==N.s&&dreturn false;
        else return true;
    }
};
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        int N;
        scanf("%d",&N);
        if(N==0)cout<<0<else {
        vector v;
        int dead;
        for(int i=0;iscanf("%d",&dead);
            v.push_back(Node(dead,0));
        }
        int score;
        for(int i=0;iscanf("%d",&score);
            v[i].s=score;
        }
        sort(v.begin(),v.end());
        vector<int> temp;
        int sum=0;
        for(int i=0;iif(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<return 0;
}

你可能感兴趣的:(Algorithm,In,Action)