agc023D Go Home

agc023D Go Home

  • 一条街上有 N N N栋楼,第 i i i栋位置为 X i X_i Xi,第 i i i栋楼里有 P i P_i Pi个人。

  • 在位置 S S S有一座公司,员工下班时乘坐公司的员工班车回家。

  • 每一个时刻车上的人投票,向左或者向右,如果票数相同则向左。

  • 每一个员工都绝顶聪明,会使得投票下自己回到家的时间最早,如果左右都一样则投向左的,如果班车经过他们的家,他们就会下车。

  • 求最后一个人到家的时间。

  • N ≤ 1 e 5 , X i , P i ≤ 1 e 9 N\le1e5,X_i,P_i\le1e9 N1e5,Xi,Pi1e9

Solution

  • 首先考虑对于第一个和最后一个人,如果 P [ 1 ] ≥ P [ n ] P[1]\ge P[n] P[1]P[n],那么第 n n n个人只能等 1 1 1到了之后才能回家,所以第 n n n个人肯定是优先帮 1 1 1回家,所以考虑把最后一个人的票加到第一个人上,变成一个 [ 1 , n − 1 ] [1,n-1] [1,n1]的子问题即可。
#include<cstdio>
#include<cstring>
#include<cstring>
#include<algorithm>
#include<vector>
#define maxn 100005
#define ll long long
using namespace std;

int n,i,j,k;
ll ans,c[maxn],x[maxn],now;

ll dg(int l,int r){
	ll s;
	if (x[l]>=now) {s=x[r]-now,now=x[r];return s;}
	if (x[r]<=now) {s=now-x[l],now=x[l];return s;}
	if (c[l]>=c[r]) 
		c[l]+=c[r],s=dg(l,r-1),s+=x[r]-now,now=x[r];
	else c[r]+=c[l],s=dg(l+1,r),s+=now-x[l],now=x[l];
	return s;
}

int main(){
	freopen("ceshi.in","r",stdin);
	scanf("%d%lld",&n,&now);
	for(i=1;i<=n;i++) scanf("%lld%lld",&x[i],&c[i]);
	printf("%lld",dg(1,n));
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值