UVALive 4850 Installations
题目大意:有若干个任务,每个任务耗时si,期限为di,同一时间只能做一个任务。对于一个任务,惩罚值为max(0,完成时间-期限)。问怎么安排,使(最大惩罚值+次大惩罚值)最小,O(n^2)。
如果没有次大惩罚值,就是一个很显然的贪心了:把任务按di排序,di相同按si排序,一路平推过去。
这样可以保证最大值最小,但不能保证(最大+次大)最小。好在这题有十分良心的样例:

仔细观察一下样例,你会发现:基本上也是按照上面的逻辑做任务,但J2和J6换了一个位置。
还有一件非常巧合的事情:J2和J6就是答案。
观察一下完全按照上面逻辑做的情况,发现答案是8,最大值是J6(5),次大值是J2(3)。
然后把J2换到J6右边,就变成最大值J2(6),次大值J6(1)。
提示我们可以通过把一个前面的拿出来,扔到原先最大/次大的后一个去。
分析一下这样做的结果:后半截不变,拿出来的变成最大值,原来的最大值、次大值减小并作为次大值,最终结果可能会比原答案小。
看一下数据,枚举,O(n^2)就可以了。
为什么只拿一个就可以了呢?因为拿两个就肯定比不拿要大了(第一次拿出的超过原次大值,第二次的超过原来的最大值)。
要不是有样例我一年都想不出这个
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#include <complex>
#include <stack>
#define LL long long int
#define dob double
#define FILE "4850"
using namespace std; const int N = ;
struct Pair{
int t,r;
bool operator <(const Pair &p)const{
if(r==p.r)return t<p.t;
return r<p.r;
}
}A[N];
int n,id,Ans,L[N],R[N]; inline int gi(){
int x=,res=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-')res*=-;ch=getchar();}
while(ch<=''&&ch>='')x=x*+ch-,ch=getchar();
return x*res;
} inline int getmxp(){
int sum=,mxp=,nmxp=;id=;
for(int i=R[],p;i<=n;i=R[i]){
sum+=A[i].t;p=max(,sum-A[i].r);
if(p>=mxp)nmxp=mxp,mxp=p,id=i;
else if(p>=nmxp)nmxp=p,id=i;
}
return mxp+nmxp;
} inline void solve(){
n=gi();R[]=;L[n+]=n;
for(int i=;i<=n;++i)
A[i].t=gi(),A[i].r=gi(),L[i]=i-,R[i]=i+;
sort(A+,A+n+);Ans=getmxp();
if(!Ans){printf("0\n");return;}
for(int i=R[],j=id;i!=j;i=R[i]){
int l=L[i],r=R[i];
R[l]=r;R[i]=R[j];R[j]=i;
Ans=min(Ans,getmxp());
R[j]=R[i];R[l]=i;R[i]=r;
}
printf("%d\n",Ans);
} int main()
{
freopen(FILE".in","r",stdin);
freopen(FILE".out","w",stdout);
int Case=gi();while(Case--)solve();
fclose(stdin);fclose(stdout);
return ;
}
Installations
UVALive 4850 Installations的更多相关文章
- UVALive 4850 Installations 贪心
题目链接 题意 工程师要安装n个服务,其中服务Ji需要si单位的安装时间,截止时间为di.超时会有惩罚值,若实际完成时间为ci,则惩罚值为max{0,ci-di}.从0时刻开始执行任务,问惩罚值最大 ...
- UVALive - 4108 SKYLINE[线段树]
UVALive - 4108 SKYLINE Time Limit: 3000MS 64bit IO Format: %lld & %llu Submit Status uDebug ...
- UVALive - 3942 Remember the Word[树状数组]
UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- 思维 UVALive 3708 Graveyard
题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...
- UVALive 6145 Version Controlled IDE(可持久化treap、rope)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- UVALive 6508 Permutation Graphs
Permutation Graphs Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit ...
- UVALive 6500 Boxes
Boxes Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Pract ...
- UVALive 6948 Jokewithpermutation dfs
题目链接:UVALive 6948 Jokewithpermutation 题意:给一串数字序列,没有空格,拆成从1到N的连续数列. dfs. 可以计算出N的值,也可以直接检验当前数组是否合法. # ...
随机推荐
- TFboy养成记 简单小程序(Variable & placeholder)
学习参考周莫烦的视频. Variable:主要是用于训练变量之类的.比如我们经常使用的网络权重,偏置. 值得注意的是Variable在声明是必须赋予初始值.在训练过程中该值很可能会进行不断的加减操作变 ...
- web离线应用--dom storage
web离线应用--dom storage dom storage是html5添加的新功能,其实也不是什么新的应用,只不过是cookie的放大版本,由于cookie的大小只有4kb,而且在每次请求一个新 ...
- 崩溃 golang入坑系列
早上(11.30)收到邮件,Vultr东京机房网络故障.当时搭建SS时,考虑到了机房故障.所以特意分出了日本和香港两条线路.但千算万算,忘记数据库还在东京机房中. 现在网络故障,SS服务器无法读取数据 ...
- ShoneSharp语言(S#)的设计和使用介绍—数值Double
ShoneSharp语言(S#)的设计和使用介绍 系列(5)- 数值Double 作者:Shone 声明:原创文章欢迎转载,但请注明出处,https://www.cnblogs.com/ShoneSh ...
- Django的分页器(paginator)
先导入模块: from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger 分页器paginator 下面的所有方法 ...
- jquery类数组结构学习笔记
大家都知道我们使用$()产生的jquery对象可以使用下标去获取对应下标的元素. 举个栗子,一个页面有5个div标签,我们使用$("div")去获取到这些元素,然后我们就可以使用$ ...
- thinkphp框架知识点
基本配置 define('APP_DEBUG',true);//开启debug模式 //记录日志 'LOG_RECORD' => true, //系统日志在记录的时候需要开启debug调试模式, ...
- 实践作业1:测试管理工具实践 Day3
1.Vertrigoserv启动后,首先要配置apache,则需要修改监听端口,不要出现端口冲突2.配置mysql,在mysql console中输入密码vertrigo3.在浏览器中输入http:/ ...
- mysql 系统性浅聊 myisam 存储引擎【原创】
>>思维导图 >>介绍 mysql中的存储引擎都是以插件的形式存在,目前用的最多存储引擎就是innodb和myisam.MySQL5.5.5以后(包括5.5.5)默认使用Inn ...
- tensorflow 安装升级
对于已经安装过的tensorflow,执行以下命令升级到最新版: pip3 install -U tensorflow 目前最新版本1.4