hdu 1789 Doing Homework again(簡單貪心)

果7發表於2014-01-23

Doing Homework again

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


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
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
 


題目大意:給你n件事情,然後給這n件事情的截止日期和如果不在截至日期之前完成會被扣掉的分數,問你被扣分數的最小值。

解題思路:感覺貪心靠得住,首先對事情的分數排個序,分數大的儘量先排,如果當天排了就往前面排。我自己在下面證明了覺得沒問題。實在排不進去就扣分。

題目地址:Doing Homework again

AC程式碼:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;

int visi[1005];
struct node
{
    int time;
    int score;
}nod[1005];

int cmp(node a,node b)
{
    if(a.score>b.score) return 1;
    if(a.score==b.score&&a.time>b.time) return 1;
    return 0;
}

int main()
{
    int tes,n;
    cin>>tes;

    int i,j;
    int res;
    while(tes--)
    {
        res=0;
        cin>>n;
        for(i=0;i<n;i++)
            scanf("%d",&nod[i].time);
        for(i=0;i<n;i++)
            scanf("%d",&nod[i].score);

        sort(nod,nod+n,cmp);
        memset(visi,0,sizeof(visi));

        for(i=0;i<n;i++)
        {
            int flag=0;
            j=nod[i].time;
            while(j)
            {
                if(!visi[j])
                {
                    visi[j]=1;
                    flag=1;
                    break;
                }
                j--;
            }
            if(!flag)
                res+=nod[i].score;
        }

        cout<<res<<endl;
    }
    return 0;
}

/*
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
*/



相關文章