模拟&贪心 G HDU-1789 Doing Homework Again

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. InputThe 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.
OutputFor 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

Ignatius有很多作业要做(和我一样),每个作业都需要一天完成,并且都有一个deadline,如果超过deadline完成会有相应扣分,不同作业所扣分数不同,我们需要计算怎样安排才能够扣最少的分数。运用贪心的思想,先安排扣分多的作业,安排在最晚需要完成的时候:根据扣分排序,然后自ddl向前遍历直到可以在某天完成,或者无法完成分数要扣去。这保证了在各个ddl之前扣最少的分数。注:临界值的确定及判断。
#include <cstdio>
#include <algorithm>
using namespace std;

struct hw{
    int ddl;
    int score;
}homework[1000];

bool cmp(const hw&homework1,const hw&homework2);

int main()
{
    int t,i;

    scanf("%d",&t);

    int date[1001],mgrade[t],n,j,k;

    for(i=0;i<t;i++){
        mgrade[i]=0;
        scanf("%d",&n);
        for(j=0;j<n;j++){
            scanf("%d",&homework[j].ddl);
            date[j+1]=0;
        }
        for(j=0;j<n;j++)
            scanf("%d",&homework[j].score);
        sort(homework,homework+n,cmp);    //按所扣分数由大到小排序
        for(j=0;j<n;j++){
            for(k=homework[j].ddl;k>0;k--)
                if(date[k]==0){
                    date[k]=1;
                    break;
                }    //自ddl从后往前遍历看能否安排下这份作业
            if(k==0)
                mgrade[i]+=homework[j].score;
        }
    }

    for(i=0;i<t;i++)
        printf("%d\n",mgrade[i]);

    return 0;
}

bool cmp(const hw&homework1,const hw&homework2)
{
    if(homework1.score==homework2.score)
        return homework1.ddl<homework2.ddl;//>or<have an effect?
    else
        return homework1.score>homework2.score;
}


猜你喜欢

转载自blog.csdn.net/daddy_hong/article/details/79222890