BZOJ3807 : Neerc2011 Lanes
左右与右左是两个独立的问题
设f[i]表示i时刻左右车道减少一条的答案
g[i]表示i时刻右左车道增加一条的答案
ans=min(f[i]+g[i+r])
计算f[i]:
首先暴力计算出f[m+1],同时记录下每个时刻刚开走的车的数量now[i]
从m到1计算f[i],如果该时刻开走的车数不足n1+1,则无影响
否则多了一辆车,选取后面最早的有空位的时刻j开走
用一个栈维护空位,复杂度为$O(m)$
计算g[i]:
首先暴力计算出g[0],同时记录下每个时刻刚开走的车的数量now[i]
从1到m+r计算g[i],如果该时刻开走的车数不足n2+1,则无影响
否则多了一辆车,选取后面最早的有空位的时刻j开走
因为j是单调变化的,所以复杂度为$O(m)$
#include<cstdio>
#define N 200010
typedef long long ll;
struct P{int t,k;P(){}P(int _t,int _k){t=_t,k=_k;}}q[N];
int n1,n2,m,r,i,a[N],b[N],now[N],rem,t,mx,fin;ll ans,f[N],g[N];
inline void read(int&a){char c;while(!(((c=getchar())>='0')&&(c<='9')));a=c-'0';while(((c=getchar())>='0')&&(c<='9'))(a*=10)+=c-'0';}
inline int min(int a,int b){return a<b?a:b;}
int main(){
for(read(n1),read(n2),read(m),read(r),i=1;i<=m;i++)read(a[i]),read(b[i]);
for(rem=ans=0,i=1;i<=m;i++)rem+=a[i],t=min(rem,n1+1),now[i]=t,rem-=t,ans+=rem;
mx=m+rem/n1+1,fin=rem%n1;
if(fin)ans+=(ll)(fin+rem-n1)*(rem/n1)/2;else ans+=(ll)rem*(rem/n1-1)/2;
for(q[t=1]=P(mx,fin),i=m;i;i--){
if(now[i]>n1){
while(t&&q[t].k==n1)t--;
if(!t)q[t=1]=P(++mx,0);
q[t].k++,ans+=q[t].t-i;
}else q[++t]=P(i,now[i]);
f[i]=ans;
}
for(rem=ans=0,i=1;i<=m+r;i++)rem+=b[i],t=min(rem,n2+1),now[i]=t,rem-=t,ans+=rem;
mx=m+r+rem/(n2+1)+1,fin=rem%(n2+1);
if(fin)ans+=(ll)(fin+rem-n2-1)*(rem/(n2+1))/2;else ans+=(ll)rem*(rem/(n2+1)-1)/2;
for(i=t=1;i<=m+r;i++){
g[i]=ans;
if(now[i]>n2){
while(t<=m+r&&(t<i||now[t]>n2))t++;
if(t<=m+r)now[t]++,ans+=t-i;else{
if(fin>n2)mx++,fin=0;
fin++,ans+=mx-i;
}
}
}
for(ans=1LL<<62,i=1;i<=m;i++)if(f[i]+g[i+r]<ans)t=i,ans=f[i]+g[i+r];
return printf("%d",t),0;
}
BZOJ3807 : Neerc2011 Lanes的更多相关文章
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- 2632: [neerc2011]Gcd guessing game
2632: [neerc2011]Gcd guessing game Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 144 Solved: 84[S ...
- POJ 3895 Cycles of Lanes (dfs)
Description Each of the M lanes of the Park of Polytechnic University of Bucharest connects two of t ...
- BZOJ2646 : neerc2011 flight
答案由$3$部分构成: $1$.抛物线的极值. $2$.询问区间的左端点在抛物线上的值. $3$.询问区间的右端点在抛物线上的值. 对于$1$,就是某个矩形范围内最大值查询,使用KD-Tree可以在$ ...
- BZOJ3806: Neerc2011 Dictionary Size
题解: 这题搞得我真是酸(dan)爽(teng) 原来一直不会,一定会用到什么神奇的东西.因为重复的不知道如何计算. 今天中午睡起来忽然想到好像可以在正trie上故意走无出边,因为这样就保证了这次统计 ...
- POJ 3377 Ferry Lanes
虽然它出现在dp专场里···但是我第一反应是一道最短路题···不过幸好它出现在dp专场里···因为我不怎么会dijstra什么的··· 题意:一条河上有N+1对码头,每个相邻码头之间需要一定时间到达, ...
- poj-3895-Cycles of Lanes 简单DFS
题目链接: http://poj.org/problem?id=3895 题目意思: 在无向连通图中图中找一个经过边数最多的环. 解题思路: 从任意一点直接DFS,不用回溯,注意构成环的话至少有3条边 ...
- BZOJ2647 : [Neerc2011]Journey
$|x|+|y|=\max(x+y,x-y,-x+y,-x-y)$,设$f[i][j]$表示在$(0,0)$,朝向方向$j$,执行第$i$条指令后的信息: $cir$:是否陷入循环 $d$:朝向 $x ...
- bzoj4232: [Neerc2011 Northern]Kids Like Cakes
Description 给定一个n个点的严格凸多边形(各个内角<180°),现在要切出两个非退化三角形(三点不共线),要求两个三角形顶点必须是凸多边形的顶点,且三角形不可相交(但是点或边可以重合 ...
随机推荐
- Linux查看和结束进程命令详解
在ubuntu中,终止一个进程或终止一个正在运行的程序,一般是通过 kill .killall.pkill.xkill 等进行. ----------------------------------- ...
- HDOJ 1233
还是畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- Watering the Fields(irrigation)
#include <cstdio> #include <algorithm> struct edge{ int f,t,w; } ed[5000000]; int pl,n,c ...
- 通过eclipse配置Spring MVC项目
上一篇刚建立了一个简单的Spring项目,其实Spring MVC是一个和Struts2一样的基于MVC设计模式的web框架,并且继承了MVC的优点,是基于请求驱动的轻量级的web框架,spring ...
- Java for LeetCode 045 Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Java for LeetCode 025 Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
- mybatisforeach循环,传入多个参数
上代码: controller: @RequestMapping(value = "/findPage", method = RequestMethod.POST) @Respon ...
- 【USACO】pprime
开始看第一眼题就觉得问题会在超时上,果然写了个小代码运行到test 9时超时了 #include <stdio.h> #include <math.h> int isprime ...
- Android之解析GML并显示
本例主要实现在APP中解析GML数据并显示 GML,地理标记语言(外语全称:Geography MarkupLanguage.外语缩写:GML),它由开放式地理信息系统协会(外语缩写:OGC)于199 ...
- ScrollView与TableView实现选择效果
在cocos2dx中,ScrollView与TableView都可以实现选择效果,其中ScrollView较为原始,TableView的格子大小可以不与winSize一样大. ScrollView实现 ...