[POI2014]Freight
题目大意:
有两个城镇$A$和$B$,有$n(n\le10^6)$辆车从$A$地出发前往$B$再返回$A$地。两地之间的行驶时间为$s(s\le10^9)$,每辆车从$A$地出发的最早时间是$t_i(0\le t_1\le t_2\le\ldots\le t_n\le10^9)$。行驶过程中需要保证每辆车出发时间至少相差$1$,且每个时刻运行的车都是同一个方向,问所有车都返回$A$地至少需要多少时间?
思路:
“行驶过程中需要保证每辆车出发时间至少相差$1$”相当于令$t_i=\max(t_i,t_{i-1}+1)$。
“每个时刻运行的车都是同一个方向”说明我们只需要对这些车分批,一批往返结束后下一批开始往返(一批车留在$B$地,另一批从$A$地过来一定存在一种等价的方案使得第一批往返完成后第二批出发)。
有了以上两个信息,不难得到状态转移方程$f_i=\min\{\max(t_i,f_j+i-j-1)+2s+i-j-1\}$。
由于$f_j+i-j-1$是单调递增的,所以$\max(t_i,f_j+i-j-1)$中一定可以找到一个转折点$j$使得$j$的左边取到$t_i-i$,右边取到$f_j-j-1$。左边是定值因此$j$越大越好,右边相当于求区间$[j,i)$的最小值,用树状数组维护即可,时间复杂度$O(n\log n)$。已经能AC了。
事实上,因为$f_i$的增长速度$\ge2i$,所以取最左边即转折点的一定更优,这样就可以省掉树状数组。考虑到转折点位置是单调递增的,因此只需要每次把上一次的转折点位置记录下来,累加到当前转折点即可。时间复杂度$O(n)$。
#include<cstdio>
#include<cctype>
#include<climits>
#include<algorithm>
typedef long long int64;
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
const int N=1e6+;
int t[N]={-};
int64 f[N];
int main() {
const int n=getint(),s=getint();
for(register int i=,j=;i<=n;i++) {
f[i]=LLONG_MAX;
t[i]=std::max(getint(),t[i-]+);
for(j--;j<i&&std::max((int64)t[i],f[j]+i-j-)+s*+i-j-<f[i];j++) {
f[i]=std::max((int64)t[i],f[j]+i-j-)+s*+i-j-;
}
}
printf("%lld\n",f[n]);
return ;
}
[POI2014]Freight的更多相关文章
- BZOJ3830 : [Poi2014]Freight
首先为了保证发车时间都不同,T[i]=max(T[i],T[i-1]+1) 然后设f[i]表示前i列车回来的最早时刻 f[i]=min(max(T[i],f[j]+i-j-1)+s*2+i-j-1) ...
- bzoj 3830: [Poi2014]Freight【dp】
参考:https://blog.csdn.net/zqh_wz/article/details/52953516 妙啊 看成分段问题,因为火车只能一批一批的走(易证= =)设f[i]为到i为止的车都走 ...
- POI2014题解
POI2014题解 [BZOJ3521][Poi2014]Salad Bar 把p当作\(1\),把j当作\(-1\),然后做一遍前缀和. 一个合法区间\([l,r]\)要满足条件就需要满足所有前缀和 ...
- Work at DP
转载请注明出处:http://www.cnblogs.com/TSHugh/p/8858805.html Prepared: (无notes的波兰题目的notes见我的波兰题目补全计划)BZOJ #3 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- BZOJ 3524: [Poi2014]Couriers [主席树]
3524: [Poi2014]Couriers Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1892 Solved: 683[Submit][St ...
- BZOJ 3524: [Poi2014]Couriers
3524: [Poi2014]Couriers Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1905 Solved: 691[Submit][St ...
- [BZOJ3872][Poi2014]Ant colony
[BZOJ3872][Poi2014]Ant colony 试题描述 There is an entrance to the ant hill in every chamber with only o ...
- Simulating a Freight robot in Gazebo
Installation Before installing the simulation environment, make sure your desktop is setup with a st ...
随机推荐
- 【志银】#define lowbit(x) ((x)&(-x))原理详解
分析下列语句 #define lowbit(x) ((x)&(-x)) 可写成下列形式: int Lowbit(x) { return x&(-x); } 例1:x = 1 十进制转二 ...
- JavaScript各种数据类型
(一)JavaScript跟Java.Python等语言一样,也是一门编程语言,配合着html,css等可以让画面动起来, 在页面中导入方式主要有两种,如图 可以自己写在文件里面,一般写在body标签 ...
- php连接数据库增删改查----多条件查询
关于查询,可以直接写在主页面上 来进行查询 首先,先建立一个表单 <form method="post" action="crud.php"> &l ...
- HDU 4661 Message Passing ( 树DP + 推公式 )
参考了: http://www.cnblogs.com/zhsl/archive/2013/08/10/3250755.html http://blog.csdn.net/chaobaimingtia ...
- 【计算机网络基础】用 telnet 检查 VPS 的某个端口是否处于监听状态
命令 telnet x.x.x.x p 其中 x.x.x.x 代表 VPS 的 IP 地址,p 代表要检查的端口号. 起因是我的 ss 不好使了.IP 没被封,查了一下,可能是 ssserver 的端 ...
- cf 834 E. Ever-Hungry Krakozyabra
cf 834 E. Ever-Hungry Krakozyabra(爆搜+数位dp) 题意: 定义一种inedible tail为一个数把每一位数字按不降的顺序排列后,去掉前导0组成的序列 比如570 ...
- 命令__shell变量$#,$@,$0,$1,$2的含义解释
linux中shell变量$#,$@,$0,$1,$2的含义解释:变量说明:$$ Shell本身的PID(ProcessID)$! Shell最后运行的后台Process的PID$? 最后运行的命令的 ...
- linux后端跑redis
http://blog.csdn.net/ksdb0468473/article/details/52126009
- code forces 994B
B. Knights of a Polygonal Table time limit per test 1 second memory limit per test 256 megabytes inp ...
- Html5学习进阶一 视频和音频
HTML5 规定了一种通过 video 元素来包含视频的标准方法. 视频格式 当前,video 元素支持两种视频格式: Internet Explorer Firefox 3.5 Opera 10 ...