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

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = 1100;
  4. struct sub
  5. {
  6. int dead;
  7. int score;
  8. friend bool operator <(sub a,sub b)
  9. {
  10. return a.score > b.score;
  11. }
  12. } su[maxn];
  13. struct DP
  14. {
  15. int num;
  16. int sum;
  17. priority_queue <sub> qu;//dp数组里面的优先队列拿来记录这一天中安排的作业有哪些
  18. }dp[maxn];
  19. bool cmp(sub a,sub b)
  20. {
  21. return a.dead < b.dead;
  22. }
  23. int main()
  24. {
  25. int t;
  26. scanf("%d",&t);
  27. while(t--)
  28. {
  29. int Max = 0;
  30. int sum = 0;
  31. int n,time = 0;
  32.  
  33. //这个输入有点烦
  34. scanf("%d",&n);
  35. for(int i=1; i<=n; i++)
  36. {
  37. scanf("%d",&su[i].dead);
  38. if(su[i].dead > time)
  39. time =su[i].dead;
  40. }
  41. for(int i=0;i<=time;i++)
  42. {
  43. while(!dp[i].qu.empty())
  44. dp[i].qu.pop();
  45. dp[i].num = 0;
  46. dp[i].sum = 0;
  47. }
  48. for(int i=1;i<=n;i++)
  49. {
  50. scanf("%d",&su[i].score);
  51. sum += su[i].score;
  52. }
  53.  
  54. sort(su + 1,su + n + 1,cmp);//按照贪心的思想,从截止日期的先后进行排序
  55. for(int i=1;i<=n;i++)
  56. {
  57. for(int j=su[i].dead;j>=1;j--)
  58. {
  59. if(dp[j].num < j && dp[j].sum < dp[j-1].sum + su[i].score)//当天没有安排满,就将这个作业直接安排在这一天
  60. {
  61. dp[j].sum = dp[j-1].sum + su[i].score;
  62. dp[j].qu = dp[j-1].qu;
  63. dp[j].qu.push(su[i]);
  64. dp[j].num = dp[j-1].num + 1;
  65. }
  66. else if(dp[j].num == j && dp[j].sum < dp[j].sum - dp[j].qu.top().score + su[i].score)//当天安排满了,并且等待安排的这个作业比已经安排在这一天的作业中的最小的那个得分更高
  67. {
  68. dp[j].sum = dp[j].sum - dp[j].qu.top().score + su[i].score;
  69. dp[j].qu.pop();
  70. dp[j].qu.push(su[i]);
  71. }
  72. if(dp[j].sum > Max)//记录一下获得的最高的分
  73. Max = dp[j].sum;
  74. }
  75. }
  76. printf("%d\n",sum-Max);//总分减去最高的分就是被扣的最少的分
  77. }
  78. return 0;
  79. }

动态规划:HDU1789-Doing Homework again的更多相关文章

  1. 解题报告 HDU1789 Doing Homework again

    Doing Homework again Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  2. HDU1789 Doing Homework again 做作业【贪心】

    题目链接:https://vjudge.net/problem/HDU-1789 题目大意: 给出N个作业的截至日期,和N个作业不交所扣掉的分数,要求输出扣除分数做少的方案. 解析: 与上一道销售商品 ...

  3. hdu1789 Doing Homework again(贪心+排序)

    Doing Homework again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  4. HDU1789 Doing Homework again 【贪心】

    Doing Homework again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  5. HDU-1789 Doing Homework again 贪心问题 有时间限制的最小化惩罚问题

    题目链接:https://cn.vjudge.net/problem/HDU-1789 题意 小明有一大堆作业没写,且做一个作业就要花一天时间 给出所有作业的时间限制,和不写作业后要扣的分数 问如何安 ...

  6. HDU1789 Doing Homework again

    基础单调队列: #include<cstdio> #include<cstdlib> #include<iostream> #include<algorith ...

  7. hdu1789 Doing Homework again---(经典贪心)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1789 题目大意: 给出N个作业的截至日期,和N个作业不交所扣掉的分数,要求输出扣除分数做少的方案. ...

  8. HDU1789(Doing Homework again)题解

    HDU1789(Doing Homework again)题解 以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定任务分数和其截止日期,每日可完成一任务,输出当罚分尽可能小时 ...

  9. HDU 1074 Doing Homework (动态规划,位运算)

    HDU 1074 Doing Homework (动态规划,位运算) Description Ignatius has just come back school from the 30th ACM/ ...

随机推荐

  1. SQLServer2008 开启远程连接

    关闭防火墙 基本的设置可以参考下面的链接: http://wenku.baidu.com/link?url=qjZKZCCoa5T3EGd_rqSjl6Tuhb1wYjIHyXri630QxuAIKu ...

  2. 显示单位px、dip以及sp的区别

    dip: Device Independent Pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA.HVGA和QVGA推荐使用这个,不依赖像素. p ...

  3. axios拦截器+mockjs

    //main.js中 //引入你mock.js文件 require('./mock.js') //封装api请求 //src/axios/api.js import axios from 'axios ...

  4. 用xaml画的带阴影3D感的圆球

    <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center"> <E ...

  5. OpenCV之cvAddWeighted直接C语言实现版addWeighted,应对上下平滑融合拼接

    关于OpenCV中的cvAddWeighted的介绍可参见<opencv中的cvAddWeighted函数> cvAddWeighted有个问题,它只能实现两张图片的直接融合,往往产生明显 ...

  6. Android中intent相关,setFlag(xx);

    intent.setFlags(参数)://参数用法如下 :FLAG_ACTIVITY_CLEAR_TOP: 例如现在的栈情况为:A B C D .D此时通过intent跳转到B,如果这个intent ...

  7. SQLserver2005描述对数据的调用

    SQL Server2005 采用了下面的4部分结构 服务器名称.数据库名称.架构名称.数据对象名称

  8. python3基础04(requests常见请求)

    #!/usr/bin/env python# -*- coding:utf-8 -*- import requestsimport jsonimport reimport urllib3from ur ...

  9. .Net创建Windows服务完成批量导出功能(错误速查)

    无法打开计算机“.”上的服务控制管理器.此操作可能需要其他特权. 无法将类型为“Microsoft.Office.Interop.Word.ApplicationClass”的 COM 对象强制转换为 ...

  10. php之header的不同用法

    1.header()函数的作用是:发送一个原始 HTTP 标头[Http Header]到客户端. header(string,replace,http_response_code) /*string ...