HDU1789(Doing Homework again)题解

时间:2022-03-01 05:39:08

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】参见代码下方的注释。