题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1789

题目意思:
有n个作业,每个作业有一个截止日期,每个作业如果超过截止日期完成的时候有一个惩罚值,问怎样安排作业,使惩罚值最小。

解题思路:

贪心。

先按惩罚值从大到小排序,惩罚值越大,就应该尽量安排改作业在截止日期之前完成,而怎样保证既在截止日期之前完成,又保证其他的作业的惩罚值总和较小呢,应该安排在离该截止日期最近的可安排那个日期,这样就保证了惩罚值最小。

代码:

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#define eps 1e-6
#define INF 0x1f1f1f1f
#define PI acos(-1.0)
#define ll __int64
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std; /*
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
*/
struct Inf
{
int pun,dl;
}inf[1100];
bool vis[1100]; bool cmp(struct Inf a,struct Inf b)
{
if(a.pun!=b.pun) //按惩罚值从大到小排序
return a.pun>b.pun;
return a.dl<b.dl; //惩罚值相同的话,截止日期靠前的先安排
} int main()
{
int n,t; scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&inf[i].dl);
for(int i=1;i<=n;i++)
scanf("%d",&inf[i].pun); sort(inf+1,inf+n+1,cmp);
memset(vis,false,sizeof(vis));
int ans=0;
for(int i=1;i<=n;i++)
{
int t=inf[i].dl,j;
for(j=t;j>=1;j--) //找到最靠近的那天,
{
if(!vis[j])
{
vis[j]=true;
break;
}
}
if(j<1) //如果前面都放了,只能接受惩罚,此时的惩罚值是比较小的
ans+=inf[i].pun;
}
printf("%d\n",ans); }
return 0;
}

贪心-hdu-1789-Doing Homework again的更多相关文章

  1. hdu 1789 Doing HomeWork Again (贪心算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1789 /*Doing Homework again Time Limit: 1000/1000 MS ...

  2. HDU 1789 - Doing Homework again - [贪心+优先队列]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1789 Time Limit: 1000/1000 MS (Java/Others) Memory Li ...

  3. HDU 1789 Doing Homework again(非常经典的贪心)

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

  4. 题解报告:hdu 1789 Doing Homework again(贪心)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1789 Problem Description Ignatius has just come back ...

  5. HDU 1789 Doing Homework again (贪心)

    Doing Homework again http://acm.hdu.edu.cn/showproblem.php?pid=1789 Problem Description Ignatius has ...

  6. HDU 1789 Doing Homework again(贪心)

    Doing Homework again 这只是一道简单的贪心,但想不到的话,真的好难,我就想不到,最后还是看的题解 [题目链接]Doing Homework again [题目类型]贪心 & ...

  7. HDU 1789 Doing Homework again(贪心)

    在我上一篇说到的,就是这个,贪心的做法,对比一下就能发现,另一个的扣分会累加而且最后一定是把所有的作业都做了,而这个扣分是一次性的,所以应该是舍弃扣分小的,所以结构体排序后,往前选择一个损失最小的方案 ...

  8. HDU - 1789 Doing Homework again(贪心) ~~~学了一波sort对结构体排序

    题目中因为天数和分数是对应的,所以我们使用一个结构体来存分数和截止如期. 一开始做这道题的时候,很自然的就想到对天数排序,然后天数一样的分数从大到小排序,最后WA了之后才发现没有做到"舍小取 ...

  9. HDU 1789 Doing Homework again【贪心】

    题意:给出n个作业的截止时间,和该作业没有完成会被扣掉的分数.问最少会被扣掉多少分. 第一次做这一题是好久之前,当时不会(不会处理两个关键字关系@_@)---现在还是不会---看了题解---原来是这样 ...

  10. hdu 1789 Doing Homework again (Greedy)

    Problem - 1789 继续贪心.经典贪心算法,如果数据比较大就要用线段树来维护了. 思路很简单,只要按照代价由大到小排序,然后靠后插入即可.RE了一次,是没想到deadline可以很大.如果d ...

随机推荐

  1. cocos2dx 利用CCOrbitCamera实现扑克牌翻牌效果

    [cpp] view plaincopy #include "HelloWorldScene.h" #include "SimpleAudioEngine.h" ...

  2. Parsing XML in J2ME

    sun的原文,原文地址是http://developers.sun.com/mobility/midp/articles/parsingxml/. by Jonathan KnudsenMarch 7 ...

  3. [Angular 2] Using Promise to Http

    You can also use Promise for http: So for the service, you need to call toPromise() method: getVehic ...

  4. Error:Could not determine Java version-- 关于Android Studio JDK设置和JVM version设置

    最近在装AS的时候遇到一个问题,新建工程后,编译报错,Error:Could not determine Java version 不言而喻:可定是JDK的问题,网上查到2中可能性 第一:就是JDK路 ...

  5. Android material design support library -- CollapsingToolbarLayout简介

    本文是codetrick上material design support library教程的第三篇,主要讲的是CollapsingToolbarLayout的概念和应用方法. 原文链接:Materi ...

  6. SqlServer死锁与阻塞检测脚本

    IF EXISTS (SELECT * FROM sysobjects WHERE [name] = 'sp_Lock_Scan') DROP PROCEDURE sp_Lock_Scan GO CR ...

  7. 解析c语言背后的汇编代码

    源码 很简单的c语言代码,作用是交换两个数: #include <stdio.h> void swap(int * a, int * b) { *a = *a + *b - (*b = * ...

  8. iOS 中实现随机颜色

    开发中为了测试能够快速看到效果很多时候我们对颜色采用随机颜色 代码块如下 UIColor * randomColor= [UIColor colorWithRed:((float)arc4random ...

  9. (原)windows8.1上使用opencv for python

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6204100.html 参考网址: http://www.docs.opencv.org/master/ ...

  10. shell中对于命令的搜寻顺序

    当你在shell命令行输入一条命令时,shell的搜寻顺序是如何的呢?当你的脚本名字和shell中的函数名字重名,shell是如何决定运行哪一个的? 在shell中,shell对于命令的搜寻优先级为: ...