Doing Homework

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

百度翻译:

Ignatius刚从第三十届ACM/ICPC中学毕业。现在他有很多作业要做。每个老师给他一个交作业的最后期限。如果Ignatius在截止日期后交作业,老师会降低他的期末考试成绩。现在我们假设每个人做作业总是要花一天时间。所以Ignatius希望你帮他安排做作业的顺序,以尽量减少分数。
输入
输入包含多个测试用例。输入的第一行是单个整数T,这是测试用例的数目。t试验病例随访。
每个测试用例以正整数n(1<n<=1000)开始,表示作业数量。接着2行。第一行包含指示对象的最后期限的n个整数,并且下一行包含指示减少的分数的n个整数。

输出
对于每个测试用例,您应该输出最小的总减少分数,每个测试用例一行。

DP+贪心:

定义一个结构体数组来记录截止日期和所扣分数

struct Count
{
    int day,score;
}node[MAXN];

定义一个数组来记录某一天是否已经被占用

bool finshed[MAXN];

定义一种排序方法用于sort函数

按所扣分数从大到小排列,分数相同按截止日期从小到大排列

int great(Count a,Count b)
{
    if(a.score==b.score)
    {
        return a.day<b.day;
    }
    return a.score>b.score;
}

sort(node,node+n,great);

AC代码:

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=1011;

struct Count
{
    int day,score;
}node[MAXN];

bool finshed[MAXN];

int great(Count a,Count b)
{
    if(a.score==b.score)
    {
        return a.day<b.day;
    }
    return a.score>b.score;
}
int main()
{
    int T,n,j;
    int ans;
    cin>>T;
    while(T--)
    {
        ans=0;
        cin>>n;
        for(int i=0;i<n;i++)
        {
            cin>>node[i].day;
        }
        for(int i=0;i<n;i++)
        {
            cin>>node[i].score;
        }
        sort(node,node+n,great);
        memset(finshed,false,sizeof(finshed));
        for(int i=0;i<n;i++)
        {
            for(j=node[i].day;j>0;j--)
            {
                if(!finshed[j])         //判断当天是否被占用
                {
                    finshed[j]=true;    //占用当天
                    break;      
                }
            }
            if(j==0)                    //当天已经被占用
            {
                ans+=node[i].score;     //统计最小无法避免要扣分数
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq1013459920/article/details/81318984