AGC023D - Go Home

AGC023D - Go Home

题目描述

Solution

我们首先把这些人分成在 s s s左边和在 s s s右边两类,考虑最左边的一群人 A A A和最右边的一群人 B B B,这两群人中必然有其中一群人是最后到家的。而是否最后到家取决于这两群人的人数(因为倘若只剩下两群人,一个要往左,一个要往右,必然会往人多的一个方向去)。

设两群人的人数分别为 a , b a,b a,b,且 a < b a<b a<b
A A A的人必然会选择投票去 B B B的方向,因为无论如何它们都会比 B B B的这些人慢,反而不如让 B B B尽可能的早到。

于是我们可以合并 A , B A,B A,B,并加上 A − > B A->B A>B的贡献(若与上一次合并的方向相同,就不需要增加这一次的贡献)。
这样一直合并最两边的人,知道只剩下一边的人的时候,就能求出答案了。

时间复杂度 O ( n l g n ) O(nlgn) O(nlgn)

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <cassert>
#include <string.h>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>

#define MP(A,B) make_pair(A,B)
#define PB(A) push_back(A)
#define SIZE(A) ((int)A.size())
#define LEN(A) ((int)A.length())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define fi first
#define se second

using namespace std;

template<typename T>inline bool upmin(T &x,T y) { return y<x?x=y,1:0; }
template<typename T>inline bool upmax(T &x,T y) { return x<y?x=y,1:0; }

typedef long long ll;
typedef unsigned long long ull;
typedef long double lod;
typedef pair<int,int> PR;
typedef vector<int> VI;

const lod eps=1e-11;
const lod pi=acos(-1);
const int oo=1<<30;
const ll loo=1ll<<62;
const int mods=998244353;
const int MAXN=600005;
const int INF=0x3f3f3f3f;//1061109567
/*--------------------------------------------------------------------*/
inline int read()
{
	int f=1,x=0; char c=getchar();
	while (c<'0'||c>'9') { if (c=='-') f=-1; c=getchar(); }
	while (c>='0'&&c<='9') { x=(x<<3)+(x<<1)+(c^48); c=getchar(); }
	return x*f;
}
ll s[MAXN];
struct anode{ ll x,y; } a[MAXN];
int compare(anode x,anode y){ return x.x<y.x; }
int main()
{
	int n=read(),S=read();
	for (int i=1;i<=n;i++) a[i]=(anode){read(),read()};
	sort(a+1,a+n+1,compare);
	int l=1,r=n,flag=-1;
	while (a[l].x<S&&a[r].x>S)
	{
		if (a[l].y>=a[r].y) a[l].y+=a[r].y,s[l]+=s[r]+((flag!=1)?(a[r].x-a[l].x):0),flag=1,r--;
		else a[r].y+=a[l].y,s[r]+=s[l]+((flag!=0)?(a[r].x-a[l].x):0),flag=0,l++;
	}
	printf("%lld\n",(a[l].x>S)?s[r]+a[r].x-S:s[l]+S-a[l].x);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值