题目链接 

题意

工程师要安装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. Repair U Disk logo unvisiable in task bar on windows XP

    Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersio ...

  2. Windows 安装 docker 以及1709的简单使用

    PS C:\> Install-Module -Name DockerMsftProvider -Repository PSGallery -Force PS C:\> Install-P ...

  3. WebAssembly是什么?

    现在的JavaScript代码要进行性能优化,通常使用一些常规手段,如:延迟执行.预处理.setTimeout等异步方式避免处理主线程,高大上一点的会使用WebWorker.即使对于WebWorker ...

  4. array与List之间相互转化

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/5/28 16:05 # @Author : zhang chao # @Fi ...

  5. link & auto cards

    link & auto cards a link to card link https://docs.embed.ly/docs/cards DD WX <blockquote clas ...

  6. js實現

    js的代碼寫在<script></script>中: <script></script>可以放在body中或者head中,如果放在body中,一般放在b ...

  7. NOIP2018滚粗记

    NOIP2018滚粗记 day 0 上午,说是可以休息,然后睡到快9点起来吃个早饭去了机房.刷了几个板子就十二点了 下午大概就是看别人总结,颓知乎,完全没心思写代码. 晚上不要求,然后在寝室颓了一下, ...

  8. wamp下php报错session_start(): open(d:/wamp/tmp\sess_ku776hvb06ko4lv9d11e7mnfj1, O_RDWR) failed: No such file or directory

    报错提示 session_start(): open(d:/wamp/tmp\sess_ku776hvb06ko4lv9d11e7mnfj1, O_RDWR) failed: No such file ...

  9. Uva10474-STL水题-白书

    白书的一道水题.话说好久没认真做难题了.今天出了排名,所有队伍里倒数第一啊! 代码没什么可说的了. #include <algorithm> #include <cstring> ...

  10. Web api dynamic

    参考: ajax调用实例     <script>         var user = { name_CN: "haha", pwd: "123" ...