题目链接 

题意

工程师要安装n个服务,其中服务Ji需要si单位的安装时间,截止时间为di。超时会有惩罚值,若实际完成时间为ci,则惩罚值为max{0,ci-di}。从0时刻开始执行任务,问惩罚值最大的两个服务的惩罚值之和的最小是多少?

分析

乍一看似乎要二分,但实际上并不是。贪心来做,按di从小到大安排任务,当di相等时,让完成时间短的排前,这样安排任务一定时更优的,但是并不能满足题目的要求。于是需要从两个最大惩罚值的前面挑选一个任务,并将它放置在后面,然后更新答案即可。

#include<cstdio>
#include<algorithm>
using namespace std;
#define maxn 510
struct Task{
int s, d;
bool operator <(const Task t) const{
if(t.d == d)
return s < t.s;
else
return d < t.d;
}
}T[maxn];
int pos, n; int solve(int cur) {
int MAX1 = , MAX2 = , sum = ;
for(int i = ; i <= pos; i++) {
if(i == cur)
continue;
sum += T[i].s;
if(sum - T[i].d >= MAX2)
MAX2 = sum - T[i].d;
if(MAX2 > MAX1)
swap(MAX1,MAX2);
} sum += T[cur].s;
if(sum - T[cur].d >= MAX2)
MAX2 = sum - T[cur].d;
if(MAX2 > MAX1)
swap(MAX1,MAX2); for(int i = pos + ; i < n; i++) {
sum += T[i].s;
if(sum - T[i].d >= MAX2)
MAX2 = sum - T[i].d;
if(MAX2 > MAX1)
swap(MAX1,MAX2);
}
return MAX2 + MAX1;
} int main() {
int test;
scanf("%d",&test);
while(test--) {
scanf("%d",&n);
for(int i = ; i < n; i++)
scanf("%d%d",&T[i].s, &T[i].d);
sort(T,T+n); int cur = , MAX1 = , MAX2 = , t = ;
for(int i = ; i < n; i++) {
cur += T[i].s;
if(cur - T[i].d >= MAX2) {
MAX2 = cur - T[i].d;
pos = i;
}
if(MAX2 > MAX1)
swap(MAX1,MAX2);
} int ans = MAX1 + MAX2;
for(int i = ; i < pos; i++)
ans = min(ans,solve(i));
printf("%d\n",ans);
}
return ;
}

UVALive 4850 Installations 贪心的更多相关文章

  1. UVALive 4850 Installations

    题目大意:有若干个任务,每个任务耗时si,期限为di,同一时间只能做一个任务.对于一个任务,惩罚值为max(0,完成时间-期限).问怎么安排,使(最大惩罚值+次大惩罚值)最小,O(n^2). 如果没有 ...

  2. UVAlive 2911 Maximum(贪心)

    Let x1, x2,..., xm be real numbers satisfying the following conditions: a) -xi ; b) x1 + x2 +...+ xm ...

  3. uvalive 2911 Maximum(贪心)

    题目连接:2911 - Maximum 题目大意:给出m, p, a, b,然后xi满足题目中的两个公式, 要求求的 xp1 + xp2 +...+ xpm 的最大值. 解题思路:可以将x1 + x2 ...

  4. UVALive - 4225(贪心)

    题目链接:https://vjudge.net/contest/244167#problem/F 题目: Given any integer base b ≥ 2, it is well known ...

  5. UVALive 4863 Balloons 贪心/费用流

    There will be several test cases in the input. Each test case will begin with a line with three inte ...

  6. UVALive - 6268 Cycling 贪心

    UVALive - 6268 Cycling 题意:从一端走到另一端,有T个红绿灯,告诉你红绿灯的持续时间,求最短的到达终点的时间.x 思路:

  7. UVALive 4731 dp+贪心

    这个题首先要利用题目的特性,先贪心,否则无法进行DP 因为求期望的话,越后面的乘的越大,所以为了得到最小值,应该把概率值降序排序,把大的数跟小的系数相乘 然后这种dp的特性就是转移的时候,由 i推到i ...

  8. UVALive 3835:Highway(贪心 Grade D)

    VJ题目链接 题意:平面上有n个点,在x轴上放一些点,使得平面上所有点都能找到某个x轴上的点,使得他们的距离小于d.求最少放几个点. 思路:以点为中心作半径为d的圆,交x轴为一个线段.问题转换成用最少 ...

  9. UVALive 6911---Double Swords(贪心+树状数组(或集合))

    题目链接 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

随机推荐

  1. windows下net命令失败

    D:\apache-tomcat-7.0.57\bin>net start mysql57发生系统错误 5. 拒绝访问. 以管理员身份运行 run as administrator 打开cmd. ...

  2. 二叉搜索树(BST)

    (第一段日常扯蛋,大家不要看)这几天就要回家了,osgearth暂时也不想弄了,毕竟不是几天就能弄出来的,所以打算过完年回来再弄.这几天闲着也是闲着,就掏出了之前买的算法导论看了看,把二叉搜索树实现了 ...

  3. OneZero产品视频

    产品视频地址:http://v.youku.com/v_show/id_XMTU1MDMwOTk2OA==.html

  4. 微信小程序入門學習資料鏈接

    https://blog.csdn.net/github_38847071/article/details/73250258 https://blog.csdn.net/lily2016n/artic ...

  5. html 塊 div span

    塊級元素:元素結束后,另起一行:如p,table,h1,ul,ol 內聯元素:元素結束后,不會另起一行:如a,td.image.b <div>是塊級元素,div本身沒有含義,div是htm ...

  6. spring cloud实战与思考(一) spring config全局配置方案设计

    “spring cloud”的配置中心工具“spring cloud config”提供了分布式系统配置文件集中管理解决方案.该工具功能强大,实现也很简单.网上可以搜索到很多开发教程和用例.本文并不是 ...

  7. python之itemgetter函数:对字典列表进行多键排序

    itemgetter函数:对字典列表进行多键排序 from operator import itemgetter list_people = [ {'name': 'Mike', 'age': 22, ...

  8. selenium之安装和登陆操作举例

    安装selenium: python -m pip install selenium-3.4.3-py2.py3-none-any.whl 下载对应浏览器版本的驱动,且在环境变量PATH中指定驱动程序 ...

  9. js脚本 将本地图片路径转换为html

    公司业务类似于电商, 因此有很多纯图片展示的商品详情页, 类似淘宝店商品页面下的多个图片组成的商品详情页, 页面很简单, 就是一大串img标签, 但是每次做详情页都要配合emmet一顿操作( 如下图 ...

  10. jdk1.8 HashMap的扩容resize()方法详解

    /** * Initializes or doubles table size. If null, allocates in * accord with initial capacity target ...