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

   题目大意:

   在规定的时间内上交作业,让损失的成绩最少.

   注意时间问题,后面的作业可以提前做,如果提前做可以顶掉前面得分少的作业,毕竟一天只能做一次作业

   用贪心解决,这个算法类似dijkstra算法类似(实际dijkstra算法用的就是是贪心的一种策略)

扫描二维码关注公众号,回复: 2355698 查看本文章
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>

struct node
{
    int x,y;
}p[1024];

bool cmp(node a,node b)
{
    if(a.x==b.x)
        return a.y>b.y;
    else return a.x<b.x;
}

using namespace std;
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        int a[1024]={0},b[1024]={0},c[1024]={0},mini,mins,sum=0,maxs=0,sum2=0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            maxs=max(maxs,a[i]);
            p[i].x=a[i];
        }
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&b[i]);
            sum+=b[i];
            p[i].y=b[i];
        }
        sort(p+1,p+n+1,cmp);  ///长的和dijkstra一个样子
        for(int j=1;j<=n;j++)
        {
            if(c[p[j].x]==0){   ///如果这个时间没有作业,呢么当前时间先保存这个作业
                c[p[j].x]=p[j].y;
                continue;
            }
            mins=p[j].y,mini=0;
            for(int i=1;i<=p[j].x;i++)///找到之前的时间的一个比这个作业分数少的作业标记
                if(c[i]<mins)
                    mins=c[i],mini=i;
            if(mini)
                c[mini]=p[j].y;       ///用成绩高的代替原来成绩低的
        }
        for(int i=1;i<=maxs;i++)
            sum2+=c[i];
        printf("%d\n",sum-sum2);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_41183791/article/details/81070523