F - Doing Homework again

本文介绍了一个ACM/ICPC竞赛背景下的编程问题,目标是在规定的期限内完成作业以最小化成绩损失。通过使用贪心算法,类似于Dijkstra算法的策略,文章详细解释了如何安排作业顺序,并提供了完整的C++代码实现。
摘要由CSDN通过智能技术生成

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算法用的就是是贪心的一种策略)

 

#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;
}

 

转载于:https://www.cnblogs.com/wangtao971115/p/10358367.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值