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

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

题意:

给你个T,代表有T组测试数据,给你一个N,代表有N项作业需要完成,然后有两行数据分表代表作业要完成的最晚时间和不能完成作业的罚时,要求最短的罚时;

思路:

这是一道贪心题,我们要求最少的罚时,那么我们做作业时就按照罚时多的作业先做,罚时少的作业后做这个思路做题就可以了。

代码如下:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

int t,n;

struct node
{
    int time;
    int fs;
}a[1001];

int book[1001];

bool cmp(node a,node b)
{
    if(a.fs>b.fs)
        return true;
    else if(a.fs==b.fs)
    {
        if(a.time<b.time)
            return true;
    }
    else
        return false;
}

int main()
{
    scanf("%d",&t);
    while(t--)
    {
        int i,j,sum=0;
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            scanf("%d",&a[i].time);
        }
        for(i=0;i<n;i++)
        {
            scanf("%d",&a[i].fs);
        }
        memset(book,0,sizeof book);
        sort(a,a+n,cmp);//这是一道贪心题,所以按照罚时由大到小进行排序;
        for(i=0;i<n;i++)
        {
            j=a[i].time;
            for(j;j>0;j--)//对于每一道题的完成时间进行安排,安排的时间尽量靠后,为别的题尽量留下时间;
            {
                if(book[j]==0)//book数组用来标记这一天是否有安排;
                {
                    book[j]=1;
                    break;
                }
            }
            if(j==0)//没有找到能够完成这道题的时间,被罚时;
            sum=sum+a[i].fs;
        }
        printf("%d\n",sum);
    }
    return 0;
}

 

猜你喜欢

转载自blog.csdn.net/titi2018815/article/details/81108790