动态规划:HDU1789-Doing Homework again

来源:互联网 发布:win7 64位安装sql 编辑:程序博客网 时间:2024/05/16 18:18

Doing Homework again

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1221    Accepted Submission(s): 715

Problem Description

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

 



解题心得:

1、这个题和普通的动态规划还是有差别的,它每一个元素都有一个截止日期,所以不能按照普通的动态规划来做。主要是想办法解决这个截止日期的问题。怎么在截止日期之内或得最多的分。


2、首先可以按照截止日期拍一个序,按照贪心的思想,肯定 是截止日期越前面的越先考虑。然后每一门课程从当前的截止日期开始往前面规划。在往前面规划的时候需要考虑一个问题,那就是当前是否已经安排满了。如果没有安排满,例如:第三天只安排了两天的作业,那么那门课程可以直接加在第三天上面就可以了,但是如果当天已经安排满了的情况下,就需要比较安排在当天的课程得分最少的那个和现在这门等待安排的课程比较哪个得分更多。如果当前得分大于已经安排了的的最少的得分,那么就将最少得分的那个减去,把现在这个安排进去。这个实现方法我想了一下,好像也只有使用优先队列,然后控制一下优先队列,这个题就很容易弄出来了


#include<bits/stdc++.h>using namespace std;const int maxn = 1100;struct sub{    int dead;    int score;    friend bool operator <(sub a,sub b)    {        return a.score > b.score;    }} su[maxn];struct DP{    int num;    int sum;    priority_queue <sub> qu;//dp数组里面的优先队列拿来记录这一天中安排的作业有哪些}dp[maxn];bool cmp(sub a,sub b){    return a.dead < b.dead;}int main(){    int t;    scanf("%d",&t);    while(t--)    {        int Max = 0;        int sum = 0;        int n,time = 0;                //这个输入有点烦        scanf("%d",&n);        for(int i=1; i<=n; i++)        {            scanf("%d",&su[i].dead);            if(su[i].dead > time)                time =su[i].dead;        }        for(int i=0;i<=time;i++)        {            while(!dp[i].qu.empty())                dp[i].qu.pop();            dp[i].num = 0;            dp[i].sum = 0;        }        for(int i=1;i<=n;i++)        {            scanf("%d",&su[i].score);            sum += su[i].score;        }                        sort(su + 1,su + n + 1,cmp);//按照贪心的思想,从截止日期的先后进行排序        for(int i=1;i<=n;i++)        {            for(int j=su[i].dead;j>=1;j--)            {                if(dp[j].num < j && dp[j].sum < dp[j-1].sum + su[i].score)//当天没有安排满,就将这个作业直接安排在这一天                {                    dp[j].sum = dp[j-1].sum + su[i].score;                    dp[j].qu = dp[j-1].qu;                    dp[j].qu.push(su[i]);                    dp[j].num = dp[j-1].num + 1;                }                else if(dp[j].num == j && dp[j].sum  < dp[j].sum - dp[j].qu.top().score + su[i].score)//当天安排满了,并且等待安排的这个作业比已经安排在这一天的作业中的最小的那个得分更高                {                    dp[j].sum = dp[j].sum - dp[j].qu.top().score + su[i].score;                    dp[j].qu.pop();                    dp[j].qu.push(su[i]);                }                if(dp[j].sum > Max)//记录一下获得的最高的分                    Max = dp[j].sum;            }        }        printf("%d\n",sum-Max);//总分减去最高的分就是被扣的最少的分    }    return 0;}


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 手表秒针走得声音很大怎么办 顾客总找老板剪头发怎么办 马来西亚国籍想去中国怎么办 有限元学位课挂了怎么办 五百丁简历导出后不一样怎么办 面试早到了1小时怎么办 头顶头发会越来越少怎么办 vr游戏过后想吐怎么办 win10电脑不能打汉字怎么办 手机看vr特别烫怎么办 苹果手机浏览器横屏怎么办 苹果笔记本浏览器没有了怎么办 剑与家园魔镜带错兵怎么办 橙子vr上下反了怎么办 我的恐龙手机不支持ar怎么办 被小人陷害又无计可施怎么办 蚂蚁借呗2万逾期怎么办 蚂蚁借呗还款金额受限怎么办 蚂蚁借呗不能借钱了怎么办 美柚手机号换了怎么办 美柚他他圈被禁言了怎么办? 被蝎子蜇了屁股怎么办 被蝎子蛰了该怎么办 孕妇让蝎子蛰了怎么办 脸被蝎子蛰了怎么办 皮肤看着有点老怎么办 王者铭文被卖了怎么办 游戏cp送我皮肤怎么办 消防改革武警学院的学员怎么办 几把毛掉的厉害怎么办 从公务员调入事业单位的怎么办 电车被城管扣了怎么办 超变战陀发射器绳坏了怎么办 家里人总打击我怎么办干啥都要骂 欧陆风云4破产后怎么办 车被别人喷了漆怎么办 龙分期绑银行卡维护钱还不上怎么办 去维和要是伤了怎么办 头盔镜片刮花了怎么办 小牛u1钥匙丢了怎么办 非牛顿流体干了怎么办