AGC023D - Go Home
生活随笔
收集整理的這篇文章主要介紹了
AGC023D - Go Home
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
AGC023D - Go Home
題目描述
Solution
我們首先把這些人分成在sss左邊和在sss右邊兩類,考慮最左邊的一群人AAA和最右邊的一群人BBB,這兩群人中必然有其中一群人是最后到家的。而是否最后到家取決于這兩群人的人數(因為倘若只剩下兩群人,一個要往左,一個要往右,必然會往人多的一個方向去)。
設兩群人的人數分別為a,ba,ba,b,且a<ba<ba<b。
則AAA的人必然會選擇投票去BBB的方向,因為無論如何它們都會比BBB的這些人慢,反而不如讓BBB盡可能的早到。
于是我們可以合并A,BA,BA,B,并加上A?>BA->BA?>B的貢獻(若與上一次合并的方向相同,就不需要增加這一次的貢獻)。
這樣一直合并最兩邊的人,知道只剩下一邊的人的時候,就能求出答案了。
時間復雜度O(nlgn)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 secondusing 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; }總結
以上是生活随笔為你收集整理的AGC023D - Go Home的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 蝉蛹的功效与作用、禁忌和食用方法
- 下一篇: 闹羊花的功效与作用、禁忌和食用方法