E - Doing Homework again hd 1789

E - Doing Homework again

Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit

Status
Description
zichen 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 zichen 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 zichen 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


  1. 题意:有n个任务,每个任务都有结止日期和逾期相应的处罚,问你如何选择使处罚最少,每项任务消耗的时间都为1天。
  2. 思路:对处罚的分数进行贪心选择,应该尽量考虑将处分大的做完,第一个要考虑的就是一定要把处分最大的做完,用递归思想,将第一个元素去除,对剩下的元素进行选择,所以第二步要把第二大的做完,但是否能做完呢,还要具体判断,因为当前最优选择可能会受上一步最优选择的影响,即当前最优选择会对后面的选择造成影响。例如:对于第一个处分最大的任务,你无论如何都要在他的截止日期之前将其做完,因为都是一天一个任务,要使处分最低,应该尽量让处分高的做完;对于第二个要考虑的是处分次大的,也要考虑在他的截止时间之前将其完成,但此时处分最大的可能会对其造成影响,如果有一天已被第一个占据,则第二个不能再考虑这天了…… 以此类推,第i个处分大的是否能完成就要看在他的截止日期之前是否有空的一天。对于[1,i)区间的任务所占据的日期,不应再考虑。 可以看出此过程为贪心过程,每一次选择都为当前的最优选择,符合递归思想。
  3. 失误:选择依据没想好,第i天做第i大的任务。对于此类题,应该用递归的思想,从第一步选择开始,选择一个然后在对余下的进行选择,多进行几次,推断出贪心选择的依据,思路明白了,就好写了。如这个题是最大区间不相交类型的:将截止日期与第一天看成时间区间,那么对于所有的任务都可以在时间轴上标出他的可以做的时间区间如果两个区间相交,则相交部分对于处罚小的无效,多个相交只对最大的有效。但是,这种可能不太·好转化,尽可能的举例子来验证自己的想法。
  4. 代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

struct node{
    int ded;
    int sco;
}stu[1000+10];

bool cmp(node a,node b)
{
    if(a.sco !=b.sco )
    return a.sco >b.sco;
    return a.ded <b.ded;
}

int main()
{
    int t,n,i,j,redu,day[1000+10];
    cin>>t;
    while(t--)
    {
        cin>>n;
        for(i=1;i<=n;++i)
        {
            cin>>stu[i].ded;
        }
        for(i=1;i<=n;++i)
        {
            cin>>stu[i].sco;
        }

        sort(stu+1,stu+n+1,cmp); 

        redu=0;
        memset(day,0,sizeof(day));//初始阶段每一天都没任务 
        for(i=1;i<=n;++i)  //从处罚最大的开始查找 
        {
            for(j=stu[i].ded;j>0;--j)//从当前任务的截止日期开始一直到第一天 
            {
                if(day[j]==0)//如果查到有没被之前任务占据的则此任务就在这一天做 
                {
                    day[j]=1;
                    break;
                }
            }

            if(j==0)//如果没查找到 这个任务就要被处罚 
            {
                redu+=stu[i].sco;
            }
        }

        cout<<redu<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值