HDU1789(Doing Homework again)题解

以防万一,题目原文和链接均附在文末。那么先是题目分析:

【一句话题意】

给定任务分数和其截止日期,每日可完成一任务,输出当罚分尽可能小时的最小罚分。

【题目分析】

由于写的时候就知道是贪心了(专项练习= =||),所以要设计贪心策略,但是应该先处理数据以便使用。
由于要求罚分尽可能小,那么我们就根据罚分来排序。根据罚分从大到小排序,如果罚分相同则根据日期从小到大排序。(现在想想觉得似乎日期排不排都行。。)
那么我们的贪心策略应该尽可能保证分数高的作业完成。于是我们从排好的序列依次访问,并根据其作业指定的截止日期来决定这个作业啥时候写就行了。
如果出现该日已经安排有作业了,那么只需要让这次作业提前一天写就是了。如果还是有之前安排的作业,就再提前一天。如果全满了,那么这项作业可以扔掉了。由于之前排序是根据作业分数多少排序的,所以如果这项作业被扔掉了,那么这项作业的分数一定是很低的。符合舍弃目的。

【算法流程】

结构体:

“作业”结构具有的属性为

  • 截止时间;
  • 作业权重;
  • 标记;//标记则做此作业

贪心策略:

我们先按照作业的分数来进行排序,
权重处理后根据作业时间进行处理
如果该日已有作业,则该作业提前一天写

 #include <iostream>
#include <stdio.h>
#include <algorithm> using namespace std; typedef struct SubjectType{
int deadline;
int mark;
bool isSelected;
} subject; bool cmp(SubjectType a,SubjectType b){
if (a.mark != b.mark) return a.mark > b.mark;
return a.deadline < b.deadline;
} int main()
{
int dataCount,subjectCount;
int i,j,k,flag[];//flag用以标记该日要做的作业编号 ,0为最远天数
subject arr[];
scanf("%d",&dataCount);
while(dataCount--){
memset(flag,,sizeof(flag));
scanf("%d",&subjectCount);
arr[].deadline = ;
arr[].mark = ;
arr[].isSelected = false;
for(i = ;i<=subjectCount;i++){
scanf("%d",&arr[i].deadline);
if (arr[i].deadline>flag[])
flag[] = arr[i].deadline;
}
for(i = ;i<=subjectCount;i++){
scanf("%d",&arr[i].mark);
arr[i].isSelected = false;
}
sort(arr+,arr+subjectCount+,cmp);
for(i = ;i<=subjectCount;i++){
j = arr[i].deadline;
while(j>=){
if (flag[j] != ) j--;
else break;
}
if (j == ) continue;
flag[j] = i;
}
for(i = ;i<=flag[];i++){
arr[flag[i]].isSelected = true;
}
int sum;
sum = ;
for(i = ;i<=subjectCount;i++){
if (!arr[i].isSelected)
sum+= arr[i].mark;
}
//for(i = 1;i<=flag[0];i++)printf("%d_",flag[i]);
printf("%d\n",sum);
}
return ;
}
/*
TestData(IN)
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
TestData(OUT)
0
3
5
*/

这里是附带的题目信息:

题目链接:(HDU 1789)Doing Homework again
题目属性:贪心(当然,也可以用dp写)
相关题目:1009、1045、1049、1050、1051、1052、1257、1800、2037、2111、2124、2187、2391、2570
题目原文:
【Desc】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.
【In】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.
【Out】For each test case, you should output the smallest total reduced score, one line per test case.
【SampIn/Out】参见代码下方的注释。

最新文章

  1. android onNewIntent调用时机
  2. [Excel] WorkBook.SaveAs
  3. Wireshark设置interface 时提示“There are no interfaces on which a capture can be done ”
  4. [未完成]plugin.xml文件
  5. opencv 在工业中的应用:blob分析
  6. 总结&amp;计划
  7. Java基础知识强化97:final、finally、finally区别
  8. Ubuntu系统下搭建Python开发环境
  9. TLV----Demo讲解
  10. win7_32位安装MySQL_5.6以及密码修改方法
  11. Python中执行系统命令常见的几种方法--转载
  12. 论事件驱动与异步IO
  13. electron入坑指南
  14. CMDB服务器管理系统【s5day91】:如何实现允许临时修改主机名
  15. Java 关键字详解(持续更新中)
  16. Codeforces Round #500 (Div. 2) [based on EJOI]
  17. shell 余弦值转角度
  18. 人脸检测——MTCNN
  19. 用户IP地址的三个属性的区别(HTTP_X_FORWARDED_FOR,HTTP_VIA,REM_addr)
  20. 【sql绕过】Bypass waf notepad of def

热门文章

  1. Ext布局
  2. Opencv--HoughCircles源码剖析
  3. Mac系统杂项 (持续更新)
  4. REST总结
  5. C学习-fgets()篇1
  6. JS 中刷新页面的方法
  7. JQ----树杈型导航
  8. 前端利器,如何使用fiddle拦截在线css进行先下调试
  9. DOMDocument类文件
  10. win32 清空ListBox所有内容