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));
}

猜你喜欢

转载自blog.csdn.net/qq_43649416/article/details/109105514