HDU1789(Doing Homework again)题解
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】参见代码下方的注释。
HDU1789(Doing Homework again)题解的更多相关文章
- HDU1789 Doing Homework again 【贪心】
Doing Homework again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- 解题报告 HDU1789 Doing Homework again
Doing Homework again Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
- Hdoj 1789 Doing Homework again 题解
Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of h ...
- HDU1789 Doing Homework again 做作业【贪心】
题目链接:https://vjudge.net/problem/HDU-1789 题目大意: 给出N个作业的截至日期,和N个作业不交所扣掉的分数,要求输出扣除分数做少的方案. 解析: 与上一道销售商品 ...
- hdu1789 Doing Homework again(贪心+排序)
Doing Homework again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- HDU-1789 Doing Homework again 贪心问题 有时间限制的最小化惩罚问题
题目链接:https://cn.vjudge.net/problem/HDU-1789 题意 小明有一大堆作业没写,且做一个作业就要花一天时间 给出所有作业的时间限制,和不写作业后要扣的分数 问如何安 ...
- HDU1789 Doing Homework again
基础单调队列: #include<cstdio> #include<cstdlib> #include<iostream> #include<algorith ...
- hdu1789 Doing Homework again---(经典贪心)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1789 题目大意: 给出N个作业的截至日期,和N个作业不交所扣掉的分数,要求输出扣除分数做少的方案. ...
- HDU 6343.Problem L. Graph Theory Homework-数学 (2018 Multi-University Training Contest 4 1012)
6343.Problem L. Graph Theory Homework 官方题解: 一篇写的很好的博客: HDU 6343 - Problem L. Graph Theory Homework - ...
随机推荐
- ASP.NET在实际开发中验证码的用法
在网上有看到很多关于验证码的代码,很多都只是生成一张验证码图片,然而在实际登陆验证模块,验证码要怎么添加进去或者说怎么运用.和实际项目开发中要怎么使用验证码,我自己总结了几点. 一.在实际开发登陆模块 ...
- 为 IIS 7.0 配置 <system.webServer>
Web.config 文件中的 system.webServer 节用于指定适用于 Web 应用程序的 IIS 7.0 设置.system.WebServer 是 configuration 节的子级 ...
- for循环删除集合陷阱
首先看下面的代码: import java.util.LinkedList;import java.util.List; public class DeleteCollection { ...
- Lowest Common Ancestor of a Binary Search Tree、Lowest Common Ancestor of a Binary Search Tree
1.Lowest Common Ancestor of a Binary Search Tree Total Accepted: 42225 Total Submissions: 111243 Dif ...
- mysql学习(补充)
修改表名 rename table olderName to newerName; \c 结束不执行 设置字符集 set names gbk; mysql类型 数值型 属性修饰符 zerofill u ...
- 【Lucene4.8教程之一】使用Lucene4.8进行索引及搜索的基本操作
在Lucene对文本进行处理的过程中,可以大致分为三大部分: 1.索引文件:提取文档内容并分析,生成索引 2.搜索内容:搜索索引内容,根据搜索关键字得出搜索结果 3.分析内容:对搜索词汇进行分析,生成 ...
- 同一个View双击事件&&单击事件
final long[] mHits = new long[2]; // iv_flaw_flow.setOnClickListener(new View.OnClickListener() { // ...
- Number Sequence (HDoj1005)
Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1 ...
- 阅读express的感悟
在github上看了半天的源码,也是云里雾里,勉强也算看完了,通过查看很多人的讲解也方便了我的理解,今天记录下来,也算是做个笔记. 进入express的源码文件里我们可以看到8个文件:middlewa ...
- vi全局替换方法
来自:http://blog.sina.com.cn/s/blog_736f1c59010136ry.html 1. 基本的替换 :s/vivian/sky/ 替换当前行第一个 vivian 为 sk ...