【贪心】Doing Homework again

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

题目大意:

有两个状态描述:作业完成时间和如果没完成扣分情况, 问最佳作业顺序排序后被扣的最小分数。 所以对于cmp函数也要进行判定, 时间需求最多的应该最先被完成,所以花费时间小的在前面; 然后再考虑损失分数最小的;扣分大的排前面; 


AC代码:

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
    int time;
    int so ;
} a[100000];
int dp[100000];
int cmp( node  a, node  b)
{
    if(a.so == b.so)
        return a.time < b.time;
    else return a.so > b.so ;
}
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        memset(dp,0,sizeof(dp));
        memset(a,  0 , sizeof(a));
        int i,  j;
        for(i=1; i<=n; i++)
            scanf("%d",&a[i].time);
        for(i=1; i<=n; i++)
            scanf("%d",&a[i].so);
        sort(a+1,a+n+1,cmp);
        int sum=0;
        for(i=1; i<=n; i++)
        {
            for( j=a[i].time; j>=1; j--)
            {
                if(!dp[j])
                {
                    dp[j]=1;
                    break;
                }
            }
            if(j==0)
                sum+=a[i].so;
        }
        printf("%d\n",sum);
    }
    return 0;
}


你可能感兴趣的:(【贪心】Doing Homework again)