Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include<bits/stdc++.h> #define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++) using namespace std; const int N=2005; int a[N][N],b[N][N],c[N][N]; int n,D; int calc(int x){ int l=0,r=400,ans=-1; while (l<=r){ int mid=(l+r)/2; if (mid*mid<x) ans=mid,l=mid+1; else r=mid-1; } return ans; } int SUMB(int l,int r,int x,int y){ if (l>r||x>y) return 0; return b[r][y]-(l?b[l-1][y]:0)-(x?b[r][x-1]:0)+(l&&x?b[l-1][x-1]:0); } int SUMC(int l,int r,int x,int y){ if (l>r||x>y) return 0; return c[r][y]-(l?c[l-1][y]:0)-(x?c[r][x-1]:0)+(l&&x?c[l-1][x-1]:0); } bool check(int mid,int v){ memset(b,0,sizeof(b)); memset(c,0,sizeof(c)); int sum=0; For(i,0,D-1) For(j,0,D-1) if (a[i][j]) { if (a[i][j]<=v) b[i][j]++; else c[i][j]++,sum++; } For(i,0,2*D-1) For(j,0,2*D-1) b[i][j]=b[i%D][j%D],c[i][j]=c[i%D][j%D]; For(i,1,2*D-1) For(j,0,2*D-1) b[i][j]+=b[i-1][j],c[i][j]+=c[i-1][j]; For(i,0,2*D-1) For(j,1,2*D-1) b[i][j]+=b[i][j-1],c[i][j]+=c[i][j-1]; For(i,0,D-1) For(j,0,D-1){ int s1=SUMB(i+mid,i+D-1,j+mid,j+D-1); int s2=SUMC(i,i+mid-1,j,j+mid-1); if (s1==0&&s2==sum) return 1; } return 0; } int main(){ scanf("%d%d",&n,&D); For(i,1,n){ int x,y; scanf("%d%d",&x,&y); a[x%D][y%D]++; } int mx=0; For(i,0,D-1) For(j,0,D-1) mx=max(mx,calc(a[i][j])); For(i,0,D-1) For(j,0,D-1) a[i][j]=max(0,a[i][j]-mx*mx); int l=0,r=D,ans=23333; while (l<=r){ int mid=(l+r)/2; if (check(mid,mx)) ans=mid,r=mid-1; else l=mid+1; } printf("%d\n",mx*D+ans-1); }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include<bits/stdc++.h> using namespace std; #define int long long #define rep(i,n) for(int i=0;i<(n);i++) #define pb push_back #define all(v) (v).begin(),(v).end() #define fi first #define se second typedef vector<int>vint; typedef pair<int,int>pint; typedef vector<pint>vpint; template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;} template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;} int N,D; int cnt[1111][1111]; int acc[2222][2222]; int uku[111111]; signed main(){ for(int i=1;i<111111;i++){ uku[i]=uku[i-1]; if(uku[i]*uku[i]<i)uku[i]++; } cin>>N>>D; int L=0; rep(i,N){ int x,y; cin>>x>>y; x%=D;y%=D; cnt[y][x]++; chmax(L,uku[cnt[y][x]]); } vpint f,h; rep(i,D)rep(j,D){ if((L-1)*(L-1)>=cnt[i][j])continue; if(L*(L-1)>=cnt[i][j])h.pb({i,j}); else f.pb({i,j}); } int lb=0,ub=D; while(ub-lb>1){ int mid=(lb+ub)/2; memset(acc,0,sizeof(acc)); for(auto &p:f){ int y,x; tie(y,x)=p; acc[y][x]++; acc[y+mid][x]--; acc[y][x+mid]--; acc[y+mid][x+mid]++; } for(auto &p:h){ int y,x; tie(y,x)=p; acc[y][x]++; acc[y+mid][x]--; acc[y][x+D]--; acc[y+mid][x+D]++; acc[y][x]++; acc[y+D][x]--; acc[y][x+mid]--; acc[y+D][x+mid]++; acc[y][x]--; acc[y+mid][x]++; acc[y][x+mid]++; acc[y+mid][x+mid]--; } rep(i,D*2)rep(j,D*2){ acc[i+1][j]+=acc[i][j]; acc[i][j+1]+=acc[i][j]; acc[i+1][j+1]-=acc[i][j]; } bool ok=false; rep(i,D)rep(j,D)if(acc[i][j]+acc[i+D][j]+acc[i][j+D]+acc[i+D][j+D]==h.size()+f.size())ok=true; if(ok)ub=mid; else lb=mid; } cout<<(L-1)*D+ub-1<<endl; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> #define ADD(a, b) a = (a + ll(b)) % mod #define MUL(a, b) a = (a * ll(b)) % mod #define MAX(a, b) a = max(a, b) #define MIN(a, b) a = min(a, b) #define rep(i, a, b) for(int i = int(a); i < int(b); i++) #define rer(i, a, b) for(int i = int(a) - 1; i >= int(b); i--) #define all(a) (a).begin(), (a).end() #define sz(v) (int)(v).size() #define pb push_back #define sec second #define fst first #define debug(fmt, ...) Debug(__LINE__, ":", fmt, ##__VA_ARGS__) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pi; typedef pair<ll, ll> pl; typedef pair<int, pi> ppi; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<vl> mat; typedef complex<double> comp; void Debug() {cout << '\n'; } template<class FIRST, class... REST>void Debug(FIRST arg, REST... rest){ cout<<arg<<" ";Debug(rest...);} template<class T>ostream& operator<<(ostream& out,const vector<T>& v) { out<<"[";if(!v.empty()){rep(i,0,sz(v)-1)out<<v[i]<<", ";out<<v.back();}out<<"]";return out;} template<class S, class T>ostream& operator<<(ostream& out,const pair<S, T>& v){ out<<"("<<v.first<<", "<<v.second<<")";return out;} const int MAX_N = 300010; const int MAX_V = 100010; const double eps = 1e-6; const ll mod = 1000000007; const int inf = 1 << 30; const ll linf = 1LL << 60; const double PI = 3.14159265358979323846; /////////////////////////////////////////////////////////////////////////////////////////////////// int N, D; int S[1010][1010]; int E[2010][2010]; int F[2010][2010]; bool ok(ll m) { ll sv = m / D, lv = m / D + 1; ll l = 0; rep(i, 0, D) if(i + D * sv < m) l++; memset(E, 0, sizeof(E)); memset(F, 0, sizeof(F)); int lcnt = 0; rep(i, 0, D) { rep(j, 0, D) { if(S[i][j] > lv * lv) return false; } } rep(i, 0, D * 2) { rep(j, 0, D * 2) { if(S[i % D][j % D] > lv * sv) { lcnt++; E[i + 1][j + 1]++; } if(S[i % D][j % D] > sv * sv) { F[i + 1][j + 1]++; } E[i + 1][j + 1] += E[i][j + 1] + E[i + 1][j] - E[i][j]; F[i + 1][j + 1] += F[i][j + 1] + F[i + 1][j] - F[i][j]; } } lcnt /= 4; // debug(sv, lv, l); // rep(i, 0, D) debug(vi(S[i], S[i] + D)); // rep(i, 0, 2 * D + 1) debug(vi(E[i], E[i] + 2 * D + 1)); // rep(i, 0, 2 * D + 1) debug(vi(E[i], E[i] + 2 * D + 1)); rep(i, 0, D) { rep(j, 0, D) { if(E[i + l][j + l] - E[i][j + l] - E[i + l][j] + E[i][j] == lcnt) { int x = i + l, y = j + l; int nx = i + D, ny = j + D; if(F[nx][ny] - F[nx][y] - F[x][ny] + F[x][y] == 0) return true; } } } return false; } void solve() { cin >> N >> D; rep(i, 0, N) { int x, y; cin >> x >> y; S[x % D][y % D]++; } ll lv = 0, rv = 1000000000; while(rv - lv > 1) { ll m = (lv + rv) / 2; if(ok(m)) rv = m; else lv = m; } cout << rv - 1 << "\n"; } int main() { #ifndef LOCAL ios::sync_with_stdio(false); cin.tie(0); #endif cout << fixed; cout.precision(20); srand((unsigned int)time(NULL)); #ifdef LOCAL //freopen("in.txt", "wt", stdout); //for tester freopen("in.txt", "rt", stdin); #endif solve(); #ifdef LOCAL cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; #endif return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> typedef long long ll; #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define REP(i,a) FOR(i,0,a) using namespace std; const int MAX_N=1e5,MAX_D=1e3; int N,D; int zip[MAX_D][MAX_D],psm[2][MAX_D*2+1][MAX_D*2+1]; bool C(int b){ bool ans=false; REP(i,D){ REP(j,D){ if (psm[0][D+i][D+j]-psm[0][D+i][b+1+j]-psm[0][b+1+i][D+j]+psm[0][b+1+i][b+1+j]==0 && psm[1][D+i][b+1+j]-psm[1][D+i][j]-psm[1][b+1+i][b+1+j]+psm[1][b+1+i][j]==0 && psm[1][b+1+i][D+j]-psm[1][i][D+j]-psm[1][b+1+i][b+1+j]+psm[1][i][b+1+j]==0){ ans=true; } } } return ans; } int main(){ cin>>N>>D; REP(i,N){ int x,y; cin>>x>>y; zip[x%D][y%D]++; } int a; { int mx=0; REP(i,D){ REP(j,D){ mx=max(mx,zip[i][j]); } } a=0; while(mx>(a+1)*(a+1)){ a++; } } REP(i,D){ REP(j,D){ if (zip[i][j]>a*a){ REP(s,2)REP(t,2){ psm[0][i+1+D*s][j+1+D*t]++; } } if (zip[i][j]>a*(a+1)){ REP(s,2)REP(t,2){ psm[1][i+1+D*s][j+1+D*t]++; } } } } REP(i,D*2)REP(j,D*2){ REP(k,2){ psm[k][i+1][j+1]+=psm[k][i+1][j]+psm[k][i][j+1]-psm[k][i][j]; } } int lb=-1,ub=D-1; while(ub-lb>1){ int m=(lb+ub)/2; if (C(m)){ ub=m; }else{ lb=m; } } cout<<a*D+ub<<endl; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> #define int long long using namespace std; int dp[1000][1000]; void add(deque<int> &d, int value){ while (d.size() && value > d.back()){ d.pop_back(); } d.push_back(value); } void dlt(deque<int> &d, int value){ if (d.front() == value) d.pop_front(); } bool checker(int num, int others, int k){ bool row[k], zap[k]; for (int i=0; i < k; i++) row[i] = false; for (int i=0; i < k; i++) for (int j=0; j < k; j++) if (dp[i][j] > (num-1)*num) row[i] = true; vector<deque<int> > v(k); for (int i=0; i < k; i++) for (int j=0; j < others; j++){ add(v[i], dp[i][j]); } //exit(0); int now = 0; for (int i=0; i < k;i++){ //cout << i << " "; for (int j=0; j < k; j++) zap[j] = row[j]; int mmx = 0; for (int j=0; j < k; j++) if (v[j].front() > (num-1)*(num-1)) zap[j] = true; for (int j=0; j < k; j++) mmx = max(mmx, v[j].front()); bool abl = (mmx <= num*(num-1)); int tet = 0; int sum = 0; for (int p=0;p<others;p++) sum+=zap[p]; for (int p=0;p<k;p++){ if (sum==0 && abl) return true; sum -= zap[tet%k]; sum += zap[(tet+others)%k]; tet++; } for (int j=0;j<k;j++){ dlt(v[j], dp[j][now%k]); add(v[j], dp[j][(now+others)%k]); } now++; } return false; } signed main(){ int n, k; cin >> n >> k; for (int i=0; i < n; i++){ int x, y; cin >> x >> y; dp[x%k][y%k]++; } int mx = 0; for (int i=0;i<k;i++) for (int j=0;j<k;j++) mx = max(mx, dp[i][j]); int num = 1; while (num*num < mx) num++; int L = 0, R = k; while (R-L>1){ int M = (L+R)/2; if (checker(num, M, k)) L = M; else R = M; //return 0; } cout << num*k-L-1 << endl; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <vector> #include <iostream> #include <algorithm> #include <functional> using namespace std; int main() { int N, D; cin >> N >> D; vector<int> X(N), Y(N); vector<vector<int> > C(D, vector<int>(D)); for (int i = 0; i < N; ++i) { cin >> X[i] >> Y[i]; ++C[X[i] % D][Y[i] % D]; } int maxnum = 0; for (int i = 0; i < D; ++i) { for (int j = 0; j < D; ++j) { if (C[i][j] == 0) { C[i][j] = -1; } else { int num = 0; while ((num / 2 + 1) * (num - num / 2 + 1) < C[i][j]) ++num; C[i][j] = num; maxnum = max(maxnum, num); } } } int L = -1, R = (maxnum - maxnum / 2 + 1) * D; while (R - L > 1) { int M = (L + R) >> 1; vector<vector<int> > S(2 * D + 1, vector<int>(2 * D + 1)); function<void(int, int, int, int, int)> rangeadd = [&](int xl, int yl, int xr, int yr, int val) { if (xl >= xr || yl >= yr) return; S[xl + D][yl + D] += val; S[xr + D][yr + D] += val; S[xl + D][yr + D] -= val; S[xr + D][yl + D] -= val; }; bool ok = true; int num = 0; for (int i = 0; i < D && ok; ++i) { for (int j = 0; j < D && ok; ++j) { if (C[i][j] == -1) continue; ++num; if (C[i][j] % 2 == 0) { if (C[i][j] / 2 * D > M) ok = false; else { rangeadd(max(i - (M - C[i][j] / 2 * D), i - D + 1), max(j - (M - C[i][j] / 2 * D), j - D + 1), i + 1, j + 1, 1); } } else { if ((C[i][j] / 2 + 1) * D > M) ok = false; else { rangeadd(i - D + 1, j - D + 1, i + 1, j + 1, 1); rangeadd(i - D + 1, j - D + 1, i - (M - (C[i][j] / 2 + 1) * D), j - (M - (C[i][j] / 2 + 1) * D), -1); } } } } if (ok) { for (int i = 0; i < 2 * D; ++i) { for (int j = 0; j <= 2 * D; ++j) { S[i + 1][j] += S[i][j]; } } for (int i = 0; i <= 2 * D; ++i) { for (int j = 0; j < 2 * D; ++j) { S[i][j + 1] += S[i][j]; } } ok = false; for (int i = 0; i < D; ++i) { for (int j = 0; j < D; ++j) { int val = S[i][j] + S[i + D][j] + S[i][j + D] + S[i + D][j + D]; if (val == num) ok = true; } } } if (ok) R = M; else L = M; } cout << R << endl; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include<cstdio> #include<vector> int x[100005], y[100005]; std::vector<int>need[1005]; int fixX[1005], fixY[1005]; int tag[1005]; long long pair[1005][1005]; long long getNumber(long long l,long long r,long long d,long long left){ if(l%d<=left) l = l-l%d+left; else l = l+d-l%d+left; return (r-l+d)/d; } int isValid(long long lx,long long rx,long long ly,long long ry,long long d){ for(int i = 0; i < d; i++){ for(int j = 0; j < d; j++){ long long have = getNumber(lx,rx,d,i)*getNumber(ly,ry,d,j); //printf("i = %d, j = %d: %lld\n",i,j,have); if(have<pair[i][j]) return 0; } } return 1; } //length that can't cover int isGood(int len,int d){ int find = 0; for(int i = 0; i<d; i++){ int ok = 1; for(int j = 0; j < d; j++){ if(fixY[j]!=-1) tag[j] = 1; else tag[j] = 0; } for(int j = 0; j < len; j++){ int p = (j+i)%d; if(fixX[p]!=-1) ok = 0; if(need[p].size()!=0){ for(int k = 0; k < (int)need[p].size(); k++) tag[need[p][k]] = 1; } } if(!ok) continue; else{ int sum = 0; for(int j = 0; j < len; j++) sum += tag[j]; if(sum==0) find = 1; //printf("sum = %d\n",sum); for(int j = 1; j<d; j++){ sum -= tag[j-1]; int p = (j+len-1)%d; sum += tag[p]; if(sum==0){ //printf("i = %d, j = %d\n",i,j); find = 1; } } } } return find; } int main(){ int n,d; scanf("%d%d",&n,&d); for(int i = 1; i <= n; i++){ scanf("%d%d",&x[i],&y[i]); pair[x[i]%d][y[i]%d]++; } /*for(int i = 0; i < d; i++){ for(int j = 0; j < d; j++) printf("pair[%d][%d] = %lld\n",i,j,pair[i][j]); }*/ long long L = 1, R = n*d; long long res = -1; while(L<=R){ long long M = (L+R)/2; long long cnt = (M+1)/d; int ok = 1; for(int i = 0; i < d; i++) fixX[i] = fixY[i] = -1, need[i].clear(); for(int i = 0; i < d; i++){ for(int j = 0; j < d; j++){ if(pair[i][j]>cnt*cnt){ if(pair[i][j]<=cnt*(cnt+1ll)){ need[i].push_back(j); } else if(pair[i][j]<=(cnt+1ll)*(cnt+1ll)) fixX[i] = fixY[j] = 1; else ok = 0; } } } if(!ok) L = M+1; else{ //for(int i = 0; i < d; i++) printf("fx[%d] = %d, fy[%d] = %d\n",i,fixX[i],i,fixY[i]); int len = d-(M+1)%d; //printf("len = %d\n",len); if(isGood(len,d)){ res = M; R = M-1; } else L = M+1; } } printf("%lld\n",res); return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include<iostream> #include<vector> #include<set> #include<queue> #include<map> #include<algorithm> #include<cstring> #include<string> #include<cassert> #include<cmath> #include<climits> #include<iomanip> #include<stack> #include<unordered_map> #include<bitset> #include<limits> #include<complex> #include<array> #include<numeric> #include<functional> using namespace std; #define ll long long #define ull unsigned long long #define rrep(i,m,n) for(ll (i)=(ll)(m);(i)>=(ll)(n);(i)--) #define rep(i,m,n) for(ll (i)=(ll)(m);i<(ll)(n);i++) #define REP(i,n) rep(i,0,n) #define FOR(i,c) for(decltype((c).begin())i=(c).begin();i!=(c).end();++i) #define all(hoge) (hoge).begin(),(hoge).end() typedef pair<ll, ll> P; constexpr long double m_pi = 3.1415926535897932L; constexpr ll MOD = 1000000007; constexpr ll INF = 1LL << 61; constexpr long double EPS = 1e-10; template<typename T> using vector2 = vector<vector<T>>; template<typename T> using vector3 = vector<vector2<T>>; typedef vector<ll> Array; typedef vector<Array> Matrix; string operator*(const string& s, int k) { if (k == 0) return ""; string p = (s + s) * (k / 2); if (k % 2 == 1) p += s; return p; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } struct Edge {//グラフ int to, rev; ll cap; Edge(int _to, ll _cap, int _rev) { to = _to; cap = _cap; rev = _rev; } }; typedef vector<Edge> Edges; typedef vector<Edges> Graph; void add_edge(Graph& G, int from, int to, ll cap, bool revFlag, ll revCap) {//最大フロー求める Ford-fulkerson G[from].push_back(Edge(to, cap, (ll)G[to].size())); if (revFlag)G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1));//最小カットの場合逆辺は0にする } ll max_flow_dfs(Graph& G, ll v, ll t, ll f, vector<bool>& used) { if (v == t) return f; used[v] = true; for (int i = 0; i < G[v].size(); ++i) { Edge& e = G[v][i]; if (!used[e.to] && e.cap > 0) { ll d = max_flow_dfs(G, e.to, t, min(f, e.cap), used); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } //二分グラフの最大マッチングを求めたりも出来る また二部グラフの最大独立集合は頂点数-最大マッチングのサイズ ll max_flow(Graph& G, ll s, ll t)//O(V(V+E)) { ll flow = 0; for (;;) { vector<bool> used(G.size()); REP(i, used.size())used[i] = false; ll f = max_flow_dfs(G, s, t, INF, used); if (f == 0) { return flow; } flow += f; } } void BellmanFord(Graph& G, ll s, Array& d, Array& negative) {//O(|E||V|) d.resize(G.size()); negative.resize(G.size()); REP(i, d.size())d[i] = INF; REP(i, d.size())negative[i] = false; d[s] = 0; REP(k, G.size() - 1) { REP(i, G.size()) { REP(j, G[i].size()) { if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) { d[G[i][j].to] = d[i] + G[i][j].cap; } } } } REP(k, G.size() - 1) { REP(i, G.size()) { REP(j, G[i].size()) { if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) { d[G[i][j].to] = d[i] + G[i][j].cap; negative[G[i][j].to] = true; } if (negative[i] == true)negative[G[i][j].to] = true; } } } } void Dijkstra(Graph& G, ll s, Array& d) {//O(|E|log|V|) d.resize(G.size()); REP(i, d.size())d[i] = INF; d[s] = 0; priority_queue<P, vector<P>, greater<P>> q; q.push(make_pair(0, s)); while (!q.empty()) { P a = q.top(); q.pop(); if (d[a.second] < a.first)continue; REP(i, G[a.second].size()) { Edge e = G[a.second][i]; if (d[e.to] > d[a.second] + e.cap) { d[e.to] = d[a.second] + e.cap; q.push(make_pair(d[e.to], e.to)); } } } } void WarshallFloyd(Graph& G, Matrix& d) {//O(V^3) d.resize(G.size()); REP(i, d.size())d[i].resize(G.size()); REP(i, d.size()) { REP(j, d[i].size()) { d[i][j] = ((i != j) ? INF : 0); } } REP(i, G.size()) { REP(j, G[i].size()) { chmin(d[i][G[i][j].to], G[i][j].cap); } } REP(i, G.size()) { REP(j, G.size()) { REP(k, G.size()) { chmin(d[j][k], d[j][i] + d[i][k]); } } } } bool tsort(Graph& graph, Array& order) {//トポロジカルソートO(E+V) int n = graph.size(), k = 0; Array in(n); for (auto& es : graph) for (auto& e : es)in[e.to]++; priority_queue<ll, Array, greater<ll>> que; REP(i, n) if (in[i] == 0)que.push(i); while (que.size()) { int v = que.top(); que.pop(); order.push_back(v); for (auto& e : graph[v]) if (--in[e.to] == 0)que.push(e.to); } if (order.size() != n)return false; else return true; } class Lca { public: const int n = 0; const int log2_n = 0; std::vector<std::vector<int>> parent; std::vector<int> depth; Lca() {} Lca(const Graph& g, int root) : n(g.size()), log2_n(log2(n) + 1), parent(log2_n, std::vector<int>(n)), depth(n) { dfs(g, root, -1, 0); for (int k = 0; k + 1 < log2_n; k++) { for (int v = 0; v < (int)g.size(); v++) { if (parent[k][v] < 0) parent[k + 1][v] = -1; else parent[k + 1][v] = parent[k][parent[k][v]]; } } } void dfs(const Graph& g, int v, int p, int d) { parent[0][v] = p; depth[v] = d; for (auto& e : g[v]) { if (e.to != p) dfs(g, e.to, v, d + 1); } } int get(int u, int v) { if (depth[u] > depth[v]) std::swap(u, v); for (int k = 0; k < log2_n; k++) { if ((depth[v] - depth[u]) >> k & 1) { v = parent[k][v]; } } if (u == v) return u; for (int k = log2_n - 1; k >= 0; k--) { if (parent[k][u] != parent[k][v]) { u = parent[k][u]; v = parent[k][v]; } } return parent[0][u]; } }; void visit(const Graph& g, int v, vector<vector<int>>& scc, stack<int>& S, vector<int>& inS, vector<int>& low,vector<int>& num, int& time) { low[v] = num[v] = ++time; S.push(v); inS[v] = true; FOR(e, g[v]) { int w = e->to; if (num[w] == 0) { visit(g, w, scc, S, inS, low, num, time); low[v] = min(low[v], low[w]); } else if (inS[w]) low[v] = min(low[v], num[w]); } if (low[v] == num[v]) { scc.push_back(vector<int>()); while (1) { int w = S.top(); S.pop(); inS[w] = false; scc.back().push_back(w); if (v == w) break; } } } void stronglyConnectedComponents(const Graph& g, vector<vector<int>>& scc) {//強連結成分分解 O(E+V) const int n = g.size(); vector<int> num(n), low(n); stack<int> S; vector<int> inS(n); int time = 0; REP(u, n) if (num[u] == 0) visit(g, u, scc, S, inS, low, num, time); } class UnionFind { vector<int> data; ll num; public: UnionFind(int size) : data(size, -1), num(size) { } bool unite(int x, int y) {//xとyの集合を統合する x = root(x); y = root(y); if (x != y) { if (data[y] < data[x]) swap(x, y); data[x] += data[y]; data[y] = x; } num -= (x != y); return x != y; } bool findSet(int x, int y) {//xとyが同じ集合か返す return root(x) == root(y); } int root(int x) {//xのルートを返す return data[x] < 0 ? x : data[x] = root(data[x]); } ll size(int x) {//xの集合のサイズを返す return -data[root(x)]; } ll numSet() {//集合の数を返す return num; } }; template<typename T, typename F> class SegmentTree { private: T identity; F merge; ll n; vector<T> dat; public: SegmentTree(F f, T id,vector<T> v) :merge(f), identity(id) { int _n = v.size(); n = 1; while (n < _n)n *= 2; dat.resize(2 * n - 1, identity); REP(i, _n)dat[n + i - 1] = v[i]; for (int i = n - 2; i >= 0; i--)dat[i] = merge(dat[i * 2 + 1], dat[i * 2 + 2]); } SegmentTree(F f, T id, int _n) :merge(f), identity(id) { n = 1; while (n < _n)n *= 2; dat.resize(2 * n - 1, identity); } void set_val(int i, T x) { i += n - 1; dat[i] = x; while (i > 0) { i = (i - 1) / 2; dat[i] = merge(dat[i * 2 + 1], dat[i * 2 + 2]); } } T query(int l, int r) { T left = identity, right = identity; l += n - 1; r += n - 1; while (l < r) { if ((l & 1) == 0)left = merge(left, dat[l]); if ((r & 1) == 0)right = merge(dat[r - 1], right); l = l / 2; r = (r - 1) / 2; } return merge(left, right); } }; template< typename T > class FenwickTree { vector< T > data; int n; int p; public: FenwickTree(int n) :n(n) { data.resize(n + 1LL, 0); p = 1; while (p < data.size())p *= 2; } T sum(int k) { T ret = 0; for (; k > 0; k -= k & -k) ret += data[k]; return (ret); } T sum(int a, int b) { return sum(b) - sum(a); }//[a,b) void add(int k, T x) { for (++k; k <= n; k += k & -k) data[k] += x; } int lower_bound(ll w) { if (w <= 0)return -1; int x = 0; for (int k = p / 2; k > 0; k /= 2) { if (x + k <= n && data[x + k] < w)w -= data[x + k], x += k; } return x; } }; //約数求める //約数 void divisor(ll n, vector<ll>& ret) { for (ll i = 1; i * i <= n; i++) { if (n % i == 0) { ret.push_back(i); if (i * i != n) ret.push_back(n / i); } } sort(ret.begin(), ret.end()); } void prime_factorization(ll n, vector<P>& ret) { for (ll i = 2; i * i <= n; i++) { if (n % i == 0) { ret.push_back({ i,0 }); while (n % i == 0) { n /= i; ret[ret.size() - 1].second++; } } } if (n != 1)ret.push_back({ n,1 }); } ll mod_pow(ll x, ll n, ll mod) { ll res = 1; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } ll mod_inv(ll x, ll mod) { return mod_pow(x, mod - 2, mod); } class Combination { public: Array fact; Array inv; ll mod; ll mod_inv(ll x) { ll n = mod - 2LL; ll res = 1LL; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } //if n >= mod use lucas ll nCr(ll n, ll r) { if (n < r)return 0; if (n < mod)return ((fact[n] * inv[r] % mod) * inv[n - r]) % mod; ll ret = 1; while (n || r) { ll _n = n % mod, _r = r % mod; n /= mod; r /= mod; (ret *= nCr(_n, _r)) %= mod; } return ret; } ll nPr(ll n, ll r) { return (fact[n] * inv[n - r]) % mod; } ll nHr(ll n, ll r) { return nCr(r + n - 1, r); } Combination(ll _n, ll _mod) { mod = _mod; ll n = min(_n + 1, mod); fact.resize(n); fact[0] = 1; REP(i, n - 1) { fact[i + 1] = (fact[i] * (i + 1LL)) % mod; } inv.resize(n); inv[n - 1] = mod_inv(fact[n - 1]); for (int i = n - 1; i > 0; i--) { inv[i - 1] = inv[i] * i % mod; } } }; ll popcount(ll x) { x = (x & 0x5555555555555555) + (x >> 1 & 0x5555555555555555); x = (x & 0x3333333333333333) + (x >> 2 & 0x3333333333333333); x = (x & 0x0F0F0F0F0F0F0F0F) + (x >> 4 & 0x0F0F0F0F0F0F0F0F); x = (x & 0x00FF00FF00FF00FF) + (x >> 8 & 0x00FF00FF00FF00FF); x = (x & 0x0000FFFF0000FFFF) + (x >> 16 & 0x0000FFFF0000FFFF); x = (x & 0x00000000FFFFFFFF) + (x >> 32 & 0x00000000FFFFFFFF); return x; } template <class T>class RectangleSum { public: vector<vector<T>> sum; T GetSum(int top, int bottom, int left, int right) { //[left, right], [top, bottom] T res = sum[bottom][right]; if (left > 0) res -= sum[bottom][left - 1]; if (top > 0) res -= sum[top - 1][right]; if (left > 0 && top > 0) res += sum[top - 1][left - 1]; return res; } RectangleSum(const vector<vector<T>>& s, int h, int w) { sum.resize(h, vector<T>(w, 0)); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { sum[y][x] = s[y][x]; if (y > 0) sum[y][x] += sum[y - 1][x]; if (x > 0) sum[y][x] += sum[y][x - 1]; if (y > 0 && x > 0) sum[y][x] -= sum[y - 1][x - 1]; } } } }; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll n, d; cin >> n >> d; Matrix xy(d, Array(d, 0)); ll mx = 0; REP(i, n) { ll x, y; cin >> x >> y; x %= d; y %= d; xy[x][y]++; chmax(mx, xy[x][y]); } ll mn = 1; while (mn * mn < mx)mn++; mn--; Array x, y; Matrix a(2 * d, Array(2 * d, 0)); Matrix b = a; REP(i, 2 * d)REP(j,2 * d) { if (xy[i%d][j%d] <= mn * mn)continue; if (xy[i%d][j%d] > mn* (mn + 1))a[i][j] = 1; else b[i][j] = 1; } // REP(i, 2 * d)REP(j, 2 * d)cout << a[i][j] << (j == 2 * d - 1 ? "\n" : " "); // REP(i, 2 * d)REP(j, 2 * d)cout << b[i][j] << (j == 2 * d - 1 ? "\n" : " "); RectangleSum<ll> sum1(a, 2 * d, 2 * d), sum2(b, 2 * d, 2 * d); ll ans = INF; REP(i, d) REP(j, d) { ll l = -1, r = d; while(r - l > 1) { ll mid = (r + l) / 2; if (sum1.GetSum(i, i + mid, j, j + mid) != sum1.GetSum(i, i + d - 1, j, j + d - 1))l = mid; else if (sum2.GetSum(i + mid + 1 , i + d - 1, j + mid + 1, j + d - 1))l = mid; else r = mid; } chmin(ans, r); } cout << ans + d * mn << "\n"; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i,n) for(ll i=0;i<(ll)(n);i++) ll c[2][2222][2222],a[1111][1111]; void init(ll n,ll m) { rep(k,2) { rep(i,n+1)rep(j,m) c[k][i][j+1]+=c[k][i][j]; rep(j,m+1)rep(i,n) c[k][i+1][j]+=c[k][i][j]; } } ll calc(ll x1,ll y1,ll x2,ll y2,ll k) {return c[k][x2][y2]-c[k][x1][y2]-c[k][x2][y1]+c[k][x1][y1];} int main() { ll n,d,M=0; cin >> n >> d; rep(i,n) { ll x,y; cin >> x >> y; a[x%d][y%d]++; } rep(i,d)rep(j,d) M=max(M,(ll)ceil(sqrt(a[i][j]))); rep(i,d)rep(j,d)rep(k,2) { if((M-k)*(M-1)<a[i][j]) { c[k][i+1][j+1]++; c[k][i+1][j+d+1]++; c[k][i+d+1][j+1]++; c[k][i+d+1][j+d+1]++; } } init(d*2,d*2); ll k=d; rep(i,d)rep(j,d) { while(k>0&&!calc(i+k,j,i+d,j+d,0)&&!calc(i,j+k,i+d,j+d,0)&&!calc(i+k,j+k,i+d,j+d,1)) k--; } cout << (M-1)*d+k << endl; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC target("avx") #define ll long long #define INF 1000000005 #define MOD 1000000007 #define EPS 1e-10 #define rep(i,n) for(int i=0;i<(int)(n);++i) #define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i) #define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i) #define each(a,b) for(auto& (a): (b)) #define all(v) (v).begin(),(v).end() #define len(v) (int)(v).size() #define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end()) #define cmx(x,y) x=max(x,y) #define cmn(x,y) x=min(x,y) #define fi first #define se second #define pb push_back #define show(x) cout<<#x<<" = "<<(x)<<endl #define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl #define sar(a,n) cout<<#a<<":";rep(pachico,n)cout<<" "<<a[pachico];cout<<endl #define svec(v) cout<<#v<<":";rep(pachico,v.size())cout<<" "<<v[pachico];cout<<endl #define svecp(v) cout<<#v<<":";each(pachico,v)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl #define sset(s) cout<<#s<<":";each(pachico,s)cout<<" "<<pachico;cout<<endl #define smap(m) cout<<#m<<":";each(pachico,m)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl using namespace std; typedef pair<int,int> P; typedef pair<ll,ll> pll; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<double> vd; typedef vector<P> vp; typedef vector<string> vs; const int MAX_N = 1005; int cnt[MAX_N][MAX_N]; int n, D, mx; int sm[2*MAX_N][2*MAX_N][2]; #define comp(a, b, c, d, k) (sm[c+1][d+1][k] - sm[c+1][b][k] - sm[a][d+1][k] + sm[a][b][k]) bool possible(int b) { rep(i,D){ rep(j,D){ if(comp(i+b+1,j+b+1,i+D-1,j+D-1,0) == 0 && comp(i+b+1,j,i+D-1,j+b,1) == 0 && comp(i,j+b+1,i+b,j+D-1,1) == 0) return true; } } return false; } #define getchar getchar_unlocked inline int in() { int n = 0; short c; while ((c = getchar()) >= '0') n = n * 10 + c - '0'; return n; } int main() { n = in(), D = in(); rep(i,n){ int x = in(), y = in(); cnt[x%D][y%D]++; } rep(i,D){ rep(j,D){ cmx(mx,(int)ceil(sqrt(cnt[i][j]))-1); } } rep(i,D){ rep(j,D){ sm[i+1][j+1][0] = sm[i+D+1][j+1][0] = sm[i+1][j+D+1][0] = sm[i+D+1][j+D+1][0] = (cnt[i][j] > mx*mx); sm[i+1][j+1][1] = sm[i+D+1][j+1][1] = sm[i+1][j+D+1][1] = sm[i+D+1][j+D+1][1] = (cnt[i][j] > mx*(mx+1)); } } rep(i,2*D+1){ rep(j,2*D){ rep(k,2){ sm[i][j+1][k] += sm[i][j][k]; } } } rep(j,2*D+1){ rep(i,2*D){ rep(k,2){ sm[i+1][j][k] += sm[i][j][k]; } } } int l = -1, r = D-1; while(r-l>1){ int mid = (l+r)/2; if(possible(mid)){ r = mid; }else{ l = mid; } } cout << (ll)mx*D+r << "\n"; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> using namespace std; #define rep(i, N) for (int i = 0; i < (N); i++) #define all(a) (a).begin(), (a).end() #define pb push_back using ll = long long; using i_i = tuple<int, int>; int f(vector<vector<int>>& sum, int yl, int yr, int xl, int xr) { return sum[yr][xr] - sum[yl][xr] - sum[yr][xl] + sum[yl][xl]; } int main() { int N, D; cin >> N >> D; vector<vector<int>> a(D, vector<int>(D)); while (N--) { int y, x; scanf("%d%d", &y, &x); a[y % D][x % D]++; } int mi = INT_MAX, ma = INT_MIN; rep(y, D) rep(x, D) { mi = min(mi, a[y][x]); ma = max(ma, a[y][x]); } int lb = 0, ub = 1e6; while (ub - lb > 1) { int mid = (lb + ub) / 2; int k = mid / D, d = mid % D; ll z1 = (ll)k * k, z2 = (ll)k * (k + 1), z3 = (ll)(k + 1) * (k + 1); if (ma <= z1) { ub = mid; continue; } if (ma > z3) { lb = mid; continue; } vector<vector<int>> sum3(D * 2 + 1, vector<int>(D * 2 + 1)); vector<vector<int>> sum2(D * 2 + 1, vector<int>(D * 2 + 1)); rep(y, D * 2) rep(x, D * 2) { int z = a[y % D][x % D]; sum3[y + 1][x + 1] = sum3[y + 1][x] + sum3[y][x + 1] - sum3[y][x] + (z > z2); sum2[y + 1][x + 1] = sum2[y + 1][x] + sum2[y][x + 1] - sum2[y][x] + (z > z1); } bool ok = false; rep(y, D) rep(x, D) { if (f(sum3, y, y + d, x, x + d) == sum3[D][D]) if (f(sum2, y, y + d, 0, D) + f(sum2, 0, D, x, x + d) - f(sum2, y, y + d, x, x + d) == sum2[D][D]) ok = true; } if (ok) ub = mid; else lb = mid; } cout << ub - 1 << endl; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include "bits/stdc++.h" using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define LL long long int n,d; LL x,y; int cnt[1000][1000]; int counter[3][2001][2001]; LL nums(int sp,int x,int y,int z,int w){ return counter[sp][z][w]+counter[sp][x][y]-counter[sp][x][w]-counter[sp][z][y]; } bool test(LL len){ LL a = len/d+1; int b = (int)(len%d)+1; LL mask[3] = {a*a,a*(a-1),(a-1)*(a-1)}; REP(sp,3){ REP(i,2*d){ REP(j,2*d){ if(cnt[i%d][j%d] > mask[sp]) counter[sp][i][j]=1; else counter[sp][i][j]=0; if(i>0)counter[sp][i][j]+=counter[sp][i-1][j]; if(j>0)counter[sp][i][j]+=counter[sp][i][j-1]; if(i>0&&j>0)counter[sp][i][j]-=counter[sp][i-1][j-1]; } } } /* REP(i,2*d){ REP(j,2*d){ cerr<<counter[1][i][j]<<" "; } cerr<<endl; } REP(i,2*d){ REP(j,2*d){ cerr<<counter[2][i][j]<<" "; } cerr<<endl; } */ REP(i,d){ REP(j,d){ if(nums(0,i,j,i+b,j+b)>0)continue; if(nums(1,i+b,j,i+d,j+b)>0)continue; if(nums(1,i,j+b,i+b,j+d)>0)continue; if(nums(2,i+b,j+b,i+d,j+d)==0){ return true; } } } return false; } int solve(){ LL lb=1,ub=n*d; LL mid; while(lb<ub){ mid = (lb+ub)/2; if(test(mid)){ ub=mid; }else{ lb=mid+1; } } return lb; } int main(){ cin>>n>>d; REP(i,d){ REP(j,d) cnt[i][j]=0; } REP(i,n){ cin>>x>>y; cnt[x%d][y%d]+=1; } cout<<solve()<<endl; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cctype> #include<cstdlib> #include<algorithm> #include<bitset> #include<vector> #include<list> #include<deque> #include<queue> #include<map> #include<set> #include<stack> #include<cmath> #include<sstream> #include<fstream> #include<iomanip> #include<ctime> #include<complex> #include<functional> #include<climits> #include<cassert> #include<iterator> #include<random> #include<unordered_set> #include<unordered_map> using namespace std; #define MAX 100002 int n; int d; int cnt[1002][1002]; inline int rng(int a, int b, int c) { //count [a,b] x%d==c int below = (a / d)*d + c; while (below < a) { below += d; } int up = (b / d)*d + c; while (up > b) { up -= d; } if (below > up)return 0; return (up - below) / d + 1; } vector<pair<int, pair<int, int> > > vv; bool valid(int x, int y, int len) { long long int guarantee = len / d; guarantee *= guarantee; for (int i1 = 0; i1 < vv.size(); i1++) { int i = vv[i1].second.first; int j = vv[i1].second.second; long long int validx = rng(x, x + len, i); long long int validy = rng(y, y + len, j); validx *= validy; if (validx < cnt[i][j]) { return false; } if (cnt[i][j] == 0)break; if (cnt[i][j] <= guarantee)break; } return true; } int main() { cin >> n >> d; for (int i = 0; i < n; i++) { int x, y; scanf("%d%d", &x, &y); cnt[x%d][y%d]++; } for (int i = 0; i < d; i++) { for (int j = 0; j < d; j++) { vv.push_back(make_pair(cnt[i][j], make_pair(i, j))); } } sort(vv.begin(), vv.end()); reverse(vv.begin(), vv.end()); int ans = 1000000; for (int ii = 0; ii < vv.size(); ii++) { int i = vv[ii].second.first; int j = vv[ii].second.second; int mint = 0; int maxt = ans; if (valid(i, j, ans - 1) == false) { } else { maxt = ans - 1; if (mint > maxt)continue; while (mint + 1 < maxt) { int mid = (mint + maxt) / 2; if (valid(i, j, mid)) { maxt = mid; } else { mint = mid + 1; } } if (valid(i, j, mint)) { maxt = mint; } else { mint = maxt; } ans = min(ans, mint); } if (ii % 40 == 0 && d > 30 && clock() / (double)(CLOCKS_PER_SEC) > 2.2)break; } cout << ans << endl; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> using namespace std; typedef signed long long ll; #undef _P #define _P(...) (void)printf(__VA_ARGS__) #define FOR(x,to) for(x=0;x<(to);x++) #define FORR(x,arr) for(auto& x:arr) #define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++) #define ALL(a) (a.begin()),(a.end()) #define ZERO(a) memset(a,0,sizeof(a)) #define MINUS(a) memset(a,0xff,sizeof(a)) //------------------------------------------------------- int N,D; int X[101010]; int Y[101010]; int A[2][2110][2110]; int S[2][2110][2110]; int num[2020][2020]; int sum(int type,int x,int y,int d) { return S[type][x+d][y+d]-S[type][x+d][y]-S[type][x][y+d]+S[type][x][y]; } void solve() { int i,j,k,l,r,x,y; string s; cin>>N>>D; int R=0; FOR(i,N) { cin>>X[i]>>Y[i]; num[X[i]%D][Y[i]%D]++; R=max(R,num[X[i]%D][Y[i]%D]); } x=1; while(x*x<R) x++; R=x; FOR(x,D) FOR(y,D) { if(num[x][y]<=(R-1)*(R-1)) { continue; } else if(num[x][y]<=R*(R-1)) { A[1][x][y]=1; } else { A[0][x][y]=1; } } FOR(i,2) FOR(x,2*D+1) FOR(y,2*D+1) { if(x) S[i][x][y]+=S[i][x-1][y]; if(y) S[i][x][y]+=S[i][x][y-1]; if(x&&y) { S[i][x][y]-=S[i][x-1][y-1]; S[i][x][y]+=A[i][(x-1)%D][(y-1)%D]; } } int mi=101010; FOR(x,D) FOR(y,D) { int cur=D; for(i=10;i>=0;i--) if(cur-(1<<i)>=1) { j=cur-(1<<i); if(sum(0,x,y,D)!=sum(0,x,y,j)) continue; if(sum(1,x+j,y+j,D-j)) continue; cur-=1<<i; } mi=min(mi,cur); } cout<<mi-1+(R-1)*D<<endl; } int main(int argc,char** argv){ string s;int i; if(argc==1) ios::sync_with_stdio(false), cin.tie(0); FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin); cout.tie(0); solve(); return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> using namespace std; /*#include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; template<typename T> using gpp_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template<typename T, typename L> using gpp_map = tree<T, L, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template<typename T> using gpp_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;*/ struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; #define FOR(i, begin, end) for(int i=(begin);i<(end);i++) #define REP(i, n) FOR(i,0,n) #define IFOR(i, begin, end) for(int i=(end)-1;i>=(begin);i--) #define IREP(i, n) IFOR(i,0,n) #define Sort(v) sort(v.begin(), v.end()) #define Reverse(v) reverse(v.begin(), v.end()) #define all(v) v.begin(),v.end() #define SZ(v) ((int)v.size()) #define Lower_bound(v, x) distance(v.begin(), lower_bound(v.begin(), v.end(), x)) #define Upper_bound(v, x) distance(v.begin(), upper_bound(v.begin(), v.end(), x)) #define Max(a, b) a = max(a, b) #define Min(a, b) a = min(a, b) #define bit(n) (1LL<<(n)) #define bit_exist(x, n) ((x >> n) & 1) #define debug(x) cout << #x << "=" << x << endl; #define vdebug(v) cout << #v << "=" << endl; REP(i_debug, v.size()){ cout << v[i_debug] << ","; } cout << endl; #define mdebug(m) cout << #m << "=" << endl; REP(i_debug, m.size()){ REP(j_debug, m[i_debug].size()){ cout << m[i_debug][j_debug] << ","; } cout << endl;} #define pb push_back #define f first #define s second #define int long long #define INF 1000000000000000000 template<typename T> istream &operator>>(istream &is, vector<T> &v){ for (auto &x : v) is >> x; return is; } template<typename T> ostream &operator<<(ostream &os, vector<T> &v){ for(int i = 0; i < v.size(); i++) { cout << v[i]; if(i != v.size() - 1) cout << endl; }; return os; } template<typename T> void Out(T x) { cout << x << endl; } template<typename T1, typename T2> void Ans(bool f, T1 y, T2 n) { if(f) Out(y); else Out(n); } using vec = vector<int>; using mat = vector<vec>; using Pii = pair<int, int>; using PiP = pair<int, Pii>; using PPi = pair<Pii, int>; using bools = vector<bool>; using pairs = vector<Pii>; //int dx[4] = {1,0,-1,0}; //int dy[4] = {0,1,0,-1}; //char d[4] = {'D','R','U','L'}; const int mod = 1000000007; //const int mod = 998244353; //#define Add(x, y) x = (x + (y)) % mod //#define Mult(x, y) x = (x * (y)) % mod int usqrt(int x){ FOR(i, 1, x + 1) if(i * i >= x) return i; return -1; } signed main(){ int N, D; cin >> N >> D; map<Pii, int> cnt; REP(i, N){ int x, y; cin >> x >> y; cnt[Pii(x % D, y % D)] += 1; } int S0 = -1, S1 = bit(30) - 1; mat a(2 * D, vec(2 * D, 0)); while(S1 - S0 > 1){ int S = (S0 + S1) / 2, T = S / D, r = S % D; REP(i, 2 * D) fill(all(a[i]), 0); for(PPi t: cnt){ int x = t.f.f, y = t.f.s, n = t.s; if(T * T >= n){ a[D - 1][D - 1]++; a[2 * D - 1][D - 1]--; a[D - 1][2 * D - 1]--; a[2 * D - 1][2 * D - 1]++; }else if(T * (T + 1) >= n){ a[x - r + D - 1][D - 1]++; a[x + D][D - 1]--; a[x - r + D - 1][2 * D - 1]--; a[x + D][2 * D - 1]++; a[D - 1][y - r + D - 1]++; a[D - 1][y + D]--; a[2 * D - 1][y - r + D - 1]--; a[2 * D - 1][y + D]++; a[x - r + D - 1][y - r + D - 1]--; a[x + D][y - r + D - 1]++; a[x - r + D - 1][y + D]++; a[x + D][y + D]--; }else if((T + 1) * (T + 1) >= n){ a[x - r + D - 1][y - r + D - 1]++; a[x + D][y - r + D - 1]--; a[x - r + D - 1][y + D]--; a[x + D][y + D]++; } } REP(i, 2 * D) REP(j, 2 * D - 1) a[i][j + 1] += a[i][j]; REP(j, 2 * D) REP(i, 2 * D - 1) a[i + 1][j] += a[i][j]; bool f = false; REP(i, D) REP(j, D) if(a[i][j] + a[i + D][j] + a[i][j + D] + a[i + D][j + D] >= SZ(cnt)) f = true; if(f) S1 = S; else S0 = S; } Out(S1); return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> using namespace std; using uint = unsigned int; using ll = long long; using ull = unsigned long long; constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); } template <class T> using V = vector<T>; template <class T> using VV = V<V<T>>; #define rep(i, N) for(int i = 0; i < int(N); i++) #define rep1(i, N) for (int i = 1; i <= int(N); i++) #define fs first #define sc second #define pb push_back #define PB push_back #define MP make_pair #define FOR(i, a, b) for (int i = int(a); i < int(b); i++) #define REP(i, b) FOR(i, 0, b) V<int> slide_max(const V<int>& v, int k) { V<int> idx; int st = 0; auto add = [&](int i) { while (st != int(idx.size()) && v[idx.back()] <= v[i]) idx.pop_back(); idx.push_back(i); }; int n = int(v.size()); for (int i = 0; i < k-1; i++) { add(i); } V<int> res(n - k + 1); for (int i = 0; i <= n - k; i++) { add(i + (k-1)); res[i] = v[idx[st]]; if (idx[st] == i) st++; } return res; } int n; int g[2010][2010]; VV<int> slide_max2d(int r, int c) { int h = n, w = n; static int hol[2010][2010]; //VV<int> hol(w); for (int i = 0; i < w; i++) { V<int> buf(h); for (int j = 0; j < h; j++) { buf[j] = g[j][i]; } auto res = slide_max(buf, r); for (int j = 0; j <= h - r; j++) { hol[i][j] = res[j]; } // hol[i] = slide_max(buf, r); } VV<int> res(h - r + 1); for (int i = 0; i <= h - r; i++) { V<int> buf(w); for (int j = 0; j < w; j++) { buf[j] = hol[j][i]; } res[i] = slide_max(buf, c); } return res; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int m, d; cin >> m >> d; n = 2*d; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; g[x%d][y%d]++; g[x%d+d][y%d]++; g[x%d][y%d+d]++; g[x%d+d][y%d+d]++; } int ma = 0; for (int i = 0; i < d; i++) { for (int j = 0; j < d; j++) { ma = max(ma, g[i][j]); } } /* for (int i = 0; i < d; i++) { for (int j = 0; j < d; j++) { cout << g[i][j] << " "; } cout << endl; }*/ ll lw = max(0LL, ll(sqrt(ma) - 1 + 1e-10) * d), up = ll(ceil(sqrt(ma) - 1e-10)) * d; while (up - lw > 1) { ll md = (lw + up) / 2; ll l = md / d; int rem = md % d; bool ok = false; auto big = slide_max2d(d - rem, d - rem); if (rem == 0) { for (int x = 0; x < d; x++) { for (int y = 0; y < d; y++) { if (big[x][y] > l*l) continue; ok = true; break; } if (ok) break; } } else { auto small = slide_max2d(rem, rem); auto hbig = slide_max2d(d - rem, rem); auto wbig = slide_max2d(rem, d - rem); for (int x = 0; x < d; x++) { for (int y = 0; y < d; y++) { if (big[x + rem][y + rem] > l * l) continue; if (small[x][y] > l * l + l + l + 1) continue; if (hbig[x + rem][y] > l * l + l) continue; if (wbig[x][y + rem] > l * l + l) continue; ok = true; break; } if (ok) break; } } if (!ok) { lw = md; } else { up = md; } } cout << lw << endl; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#if 1 #include <iostream> #include <fstream> #include <string> #include <vector> #include <map> #include <set> #include <unordered_map> #include <unordered_set> #include <queue> #include <stack> #include <array> #include <deque> #include <algorithm> #include <utility> #include <cstdint> #include <functional> #include <iomanip> #include <numeric> #include <assert.h> #include <bitset> #include <list> auto& in = std::cin; auto& out = std::cout; #define all_range(C) std::begin(C), std::end(C) const double PI = 3.141592653589793238462643383279502884197169399375105820974944; int32_t N, D; int32_t num[1000][1000]; int32_t square[1000][1000]; int32_t num_one[2100][2100]; int32_t num_error[2100][2100]; using INT_T = int32_t; template<typename ARR> inline void add_full(ARR& v1, ARR& v2) { v1 += v2; } template<typename ARR, size_t N> inline void add_full(ARR(&v1)[N], ARR(&v2)[N]) { auto iter1 = v1, iter2 = v2, iter1end = v1 + N; while (iter1 != iter1end) { add_full(*iter1++, *iter2++); } } //1-indexed累積和 template<size_t N>void CuSum(INT_T(&arr)[N]) { for (size_t i = 2; i < N; i++) { arr[i] += arr[i - 1]; } } template<typename ARR, size_t N>void CuSum(ARR(&arr)[N]) { for (auto& arr2 : arr) { CuSum(arr2); } for (size_t i = 2; i < N; i++) { add_full(arr[i], arr[i - 1]); } } #if 0 template<size_t N, size_t M>void CuSum(INT_T(&arr)[N][M]) { for (auto& arr2 : arr) { CuSum(arr2); } for (size_t i = 2; i < N; i++) { add_full(arr[i], arr[i - 1]); } } template<size_t N, size_t M, size_t O>void CuSum(INT_T(&arr)[N][M][O]) { for (auto& arr2 : arr) { CuSum(arr2); } for (size_t i = 2; i < N; i++) { add_full(arr[i], arr[i - 1]); } } #endif //1-indexed、(]区間 template<size_t N> INT_T get_sum(INT_T(&arr)[N], int x, int x2) { return (arr[x2] - arr[x]); } //1-indexed、(]区間 template<size_t N, size_t M> INT_T get_sum(INT_T(&arr)[N][M], int x, int y, int x2, int y2) { return (arr[y2][x2] - arr[y][x2] - arr[y2][x] + arr[y][x]); } //1-indexed、(]区間 template<size_t N, size_t M, size_t O> INT_T get_sum(INT_T(&arr)[N][M][O], int x, int y, int z, int x2, int y2, int z2) { return arr(arr[z2][y2][x2] - arr[z][y2][x2] - arr[z2][y][x2] - arr[z2][y2][x] + arr[z2][y][x] + arr[z2][y][x] + arr[z][y][x2] - arr[z][y][x]); } int main() { using std::get; using std::endl; in.sync_with_stdio(false); out.sync_with_stdio(false); in.tie(nullptr); out.tie(nullptr); in >> N >> D; for (size_t i = 0; i < N; i++) { int x, y; in >> x >> y; x %= D; y %= D; num[x][y]++; } int32_t max_square = -1; std::vector<std::tuple<int, int, int>> max_list; for (int32_t i = 0; i < D; i++) { for (int32_t j = 0; j < D; j++) { auto s = (int32_t)std::sqrt(square[i][j]); while ((s*s) < num[i][j]) { ++s; } square[i][j] = s; int min_size = ((s*(s-1)) >= num[i][j])?1:0; if (max_square < s) { max_square = s; max_list.clear(); max_list.emplace_back(i, j, min_size); } else if(max_square == s){ max_list.emplace_back(i, j, min_size); } } } if (max_list.size() == 1) { out << (max_square - 1)*D << endl; return 0; } for (auto& i : max_list) { if (get<2>(i) == 1) { num_one[1+get<0>(i)][1+get<1>(i)]++; num_one[1+get<0>(i)][1+get<1>(i) + D]++; num_one[1+get<0>(i) + D][1 + get<1>(i)]++; num_one[1+get<0>(i)+D][1 + get<1>(i)+D]++; } else { num_error[1+get<0>(i)][1 + get<1>(i)]++; num_error[1+get<0>(i)][1 + get<1>(i) + D]++; num_error[1+get<0>(i) + D][1 + get<1>(i)]++; num_error[1+get<0>(i) + D][1 + get<1>(i) + D]++; } } CuSum(num_one); CuSum(num_error); using BS_INT = int32_t; BS_INT ok_range = D, ng_range = -1; while (std::abs(ok_range - ng_range) > 1) { BS_INT mid = (ok_range + ng_range) / 2; bool is_ok = false; for (int32_t i = 1-1; i <= D - 1; i++) { for (int32_t j = 1-1; j <= D - 1; j++) { if (get_sum(num_one, i + mid, j + mid, i + D, j + D) == 0 && get_sum(num_error,i,j, i + D, j + D) - get_sum(num_error, i, j, i + mid, j + mid) == 0 ) { is_ok = true; goto break_2; } } } break_2:; //ここに書く if (is_ok) { ok_range = mid; } else { ng_range = mid; } } out << (max_square - 1)*D+ok_range-1 << endl; #if 0 std::vector<std::tuple<int, int, int>> max_list2; max_list2.reserve(max_list.size()); for (auto& i : max_list) { max_list2.emplace_back(std::get<1>(i), std::get<0>(i), get<2>(i)); } std::sort(max_list2.begin(), max_list2.end()); int32_t min_diff = D; for (size_t i = 0; i < max_list.size(); i++) { if (i > 0 && get<0>(max_list[i]) == get<0>(max_list2[i-1])) { continue; } for (size_t j = 0; j < max_list2.size(); j++) { if (j > 0 && get<0>(max_list2[j]) == get<0>(max_list2[j-1])) { continue; } //for (auto& k : max_list) //{ // used[get<0>(k)][get<1>(k)] = false; //} auto cir_dec = [&max_list](size_t index) {if (index == 0) { return max_list.size() - 1; } else { return index - 1; }}; auto cir_inc = [&max_list](size_t index) {if ((++index) == max_list.size()) { return (size_t)0; } else { return index; }}; size_t k = cir_dec(i); for (; k != i; k = cir_dec(k)) { if (get<2>(max_list[k]) == 0) { break; } used[get<0>(max_list[k])][get<1>(max_list[k])] = true; } size_t l = cir_dec(j); for (; k != i; k = cir_inc(k)) { int32_t x_diff = get<0>(max_list[k]) - get<0>(max_list[i]); if (x_diff < 0) { x_diff += D; } while (get<2>(max_list2[l]) == 1 && used[get<1>(max_list2[l])][get<0>(max_list2[l])] == false && l != j); int32_t y_diff = get<0>(max_list[l]) - get<0>(max_list[j]); if (y_diff < 0) { y_diff += D; } min_diff = std::min(x_diff, y_diff); used[get<0>(max_list[k])][get<1>(max_list[k])] = false; } #if 0 for (size_t k = cir_dec(i); k != i; k= cir_dec(k)) { int32_t x_diff = get<0>(max_list[k]) - get<0>(max_list[i]); if (x_diff < 0) { x_diff += D; } int32_t y_diff = 0; size_t l = cir_dec(j); while (get<2>(max_list2[l]) == 1 && used[get<1>(max_list2[l])][get<0>(max_list2[l])]==false && l != j); int32_t y_diff = get<0>(max_list[l]) - get<0>(max_list[l]); if (y_diff < 0) { y_diff += D; } if (get<2>(max_list[k]) == 0) { break; } used[get<0>(max_list[k])][get<1>(max_list[k])] = true; } #endif } } out << (max_square - 1)*D + min_diff << endl; #endif return 0; } #endif
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
// need #include <iostream> #include <algorithm> // data structure #include <bitset> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> #include <complex> //#include <deque> #include <valarray> #include <unordered_map> #include <array> // stream //#include <istream> //#include <sstream> //#include <ostream> #include <fstream> // etc #include <cassert> #include <cmath> #include <functional> #include <iomanip> #include <chrono> #include <random> #include <numeric> // input #define INIT std::ios::sync_with_stdio(false);std::cin.tie(0); #define VAR(type, ...)type __VA_ARGS__;MACRO_VAR_Scan(__VA_ARGS__); template<typename T> void MACRO_VAR_Scan(T& t) { std::cin >> t; } template<typename First, typename...Rest>void MACRO_VAR_Scan(First& first, Rest&...rest) { std::cin >> first; MACRO_VAR_Scan(rest...); } #define VEC_ROW(type, n, ...)std::vector<type> __VA_ARGS__;MACRO_VEC_ROW_Init(n, __VA_ARGS__); for(int w=0; w<n; ++w){MACRO_VEC_ROW_Scan(w, __VA_ARGS__);} template<typename T> void MACRO_VEC_ROW_Init(int n, T& t) { t.resize(n); } template<typename First, typename...Rest>void MACRO_VEC_ROW_Init(int n, First& first, Rest&...rest) { first.resize(n); MACRO_VEC_ROW_Init(n, rest...); } template<typename T> void MACRO_VEC_ROW_Scan(int p, T& t) { std::cin >> t[p]; } template<typename First, typename...Rest>void MACRO_VEC_ROW_Scan(int p, First& first, Rest&...rest) { std::cin >> first[p]; MACRO_VEC_ROW_Scan(p, rest...); } #define VEC(type, c, n) std::vector<type> c(n);for(auto& i:c)std::cin>>i; #define MAT(type, c, m, n) std::vector<std::vector<type>> c(m, std::vector<type>(n));for(auto& R:c)for(auto& w:R)std::cin>>w; // output #define OUT(dist) std::cout<<(dist); #define FOUT(n, dist) std::cout<<std::fixed<<std::setprecision(n)<<(dist); #define SOUT(n, c, dist) std::cout<<std::setw(n)<<std::setfill(c)<<(dist); #define SP std::cout<<" "; #define TAB std::cout<<"\t"; #define BR std::cout<<"\n"; #define SPBR(w, n) std::cout<<(w + 1 == n ? '\n' : ' '); #define ENDL std::cout<<std::endl; #define FLUSH std::cout<<std::flush; #define SHOW(dist) {std::cerr << #dist << "\t:" << (dist) << "\n";} #define SHOWVECTOR(v) {std::cerr << #v << "\t:";for(const auto& xxx : v){std::cerr << xxx << " ";}std::cerr << "\n";} #define SHOWVECTOR2(v) {std::cerr << #v << "\t:\n";for(const auto& xxx : v){for(const auto& yyy : xxx){std::cerr << yyy << " ";}std::cerr << "\n";}} #define SHOWQUEUE(a) {auto tmp(a);std::cerr << #a << "\t:";while(!tmp.empty()){std::cerr << tmp.front() << " ";tmp.pop();}std::cerr << "\n";} // utility #define ALL(a) (a).begin(),(a).end() #define FOR(w, a, n) for(int w=(a);w<(n);++w) #define RFOR(w, a, n) for(int w=(n)-1;w>=(a);--w) #define REP(w, n) for(int w=0;w<int(n);++w) #define RREP(w, n) for(int w=int(n)-1;w>=0;--w) #define FORLL(w, a, n) for(ll w=ll(a);w<ll(n);++w) #define RFORLL(w, a, n) for(ll w=ll(n)-1;w>=ll(a);--w) #define REPLL(w, n) for(ll w=0;w<ll(n);++w) #define RREPLL(w, n) for(ll w=ll(n)-1;w>=0;--w) #define IN(a, x, b) (a<=x && x<b) template<class T> inline T CHMAX(T& a, const T b) { return a = (a < b) ? b : a; } template<class T> inline T CHMIN(T& a, const T b) { return a = (a > b) ? b : a; } #define EXCEPTION(msg) throw std::string("Exception : " msg " [ in ") + __func__ + " : " + std::to_string(__LINE__) + " lines ]" #define TRY(cond, msg) try {if (cond) EXCEPTION(msg);}catch (std::string s) {std::cerr << s << std::endl;} //void CHECKTIME(std::function<void()> f) { auto start = std::chrono::system_clock::now(); f(); auto end = std::chrono::system_clock::now(); auto res = std::chrono::duration_cast<std::chrono::nanoseconds>((end - start)).count(); std::cerr << "[Time:" << res << "ns (" << res / (1.0e9) << "s)]\n"; } // test template<class T> std::vector<std::vector<T>> VV(int n, int m, T init = T()) { return std::vector<std::vector<T>>(n, std::vector<T>(m, init)); } template<typename S, typename T> std::ostream& operator<<(std::ostream& os, std::pair<S, T> p) { os << "(" << p.first << ", " << p.second << ")"; return os; } // type/const #define int ll using ll = long long; using ull = unsigned long long; using ld = long double; using PAIR = std::pair<int, int>; using PAIRLL = std::pair<ll, ll>; constexpr int INFINT = 1 << 30; // 1.07x10^ 9 constexpr int INFINT_LIM = (1LL << 31) - 1; // 2.15x10^ 9 constexpr ll INFLL = 1LL << 60; // 1.15x10^18 constexpr ll INFLL_LIM = (1LL << 62) - 1 + (1LL << 62); // 9.22x10^18 constexpr double EPS = 1e-10; constexpr int MOD = 1000000007; constexpr double PI = 3.141592653589793238462643383279; template<class T, size_t N> void FILL(T(&a)[N], const T& val) { for (auto& x : a) x = val; } template<class ARY, size_t N, size_t M, class T> void FILL(ARY(&a)[N][M], const T& val) { for (auto& b : a) FILL(b, val); } template<class T> void FILL(std::vector<T>& a, const T& val) { for (auto& x : a) x = val; } template<class ARY, class T> void FILL(std::vector<std::vector<ARY>>& a, const T& val) { for (auto& b : a) FILL(b, val); } // ------------>8------------------------------------->8------------ int cnt[1003][1003]; int csum[3][2003][2003]; signed main() { INIT; VAR(int, n, d); VEC_ROW(int, n, x, y); { int mix = INFINT, miy = INFINT; REP(i, n) { x[i] %= d; y[i] %= d; CHMIN(mix, x[i]); CHMIN(miy, y[i]); } REP(i, n) { x[i] -= mix; y[i] -= miy; } } REP(i, n) { ++cnt[y[i]][x[i]]; } auto check = [&](int D) -> bool { FILL(csum, 0LL); int p = D / d; int q = D % d; REP(i, d) REP(j, d) { if (cnt[i][j] <= p * p) { ++csum[0][i + 0 + 1][j + 0 + 1]; ++csum[0][i + 0 + 1][j + d + 1]; ++csum[0][i + d + 1][j + 0 + 1]; ++csum[0][i + d + 1][j + d + 1]; } if (cnt[i][j] <= p * (p + 1)) { ++csum[1][i + 0 + 1][j + 0 + 1]; ++csum[1][i + 0 + 1][j + d + 1]; ++csum[1][i + d + 1][j + 0 + 1]; ++csum[1][i + d + 1][j + d + 1]; } if (cnt[i][j] <= (p + 1) * (p + 1)) { ++csum[2][i + 0 + 1][j + 0 + 1]; ++csum[2][i + 0 + 1][j + d + 1]; ++csum[2][i + d + 1][j + 0 + 1]; ++csum[2][i + d + 1][j + d + 1]; } } REP(k, 3) { REP(i, d * 2) REP(j, d * 2) { csum[k][i + 1][j + 1] += csum[k][i + 1][j + 0]; csum[k][i + 1][j + 1] += csum[k][i + 0][j + 1]; csum[k][i + 1][j + 1] -= csum[k][i + 0][j + 0]; } } // [x1, y1] x (x2, y2) auto getSum = [&](int k, int x1, int y1, int x2, int y2) { int res = 0; res += csum[k][y2][x2]; res -= csum[k][y2][x1]; res -= csum[k][y1][x2]; res += csum[k][y1][x1]; return res; }; REP(i, d) REP(j, d) { int sum = 0; sum += getSum(2, j + 0, i + 0, j + q, i + q); sum += getSum(1, j + q, i + 0, j + d, i + q); sum += getSum(1, j + 0, i + q, j + q, i + d); sum += getSum(0, j + q, i + q, j + d, i + d); if (sum == d * d) return true; } return false; }; int ng = 0, ok = 100000000; while (ok - ng > 1) { int me = (ng + ok) / 2; if (check(me)) ok = me; else ng = me; } OUT(ok - 1)BR; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> using namespace std; #ifdef DEBUG_MODE #define DBG(n) n; #else #define DBG(n) ; #endif #define REP(i,n) for(ll (i) = (0);(i) < (n);++i) #define PB push_back #define MP make_pair #define FI first #define SE second #define SHOW1d(v,n) {for(int W = 0;W < (n);W++)cerr << v[W] << ' ';cerr << endl << endl;} #define SHOW2d(v,i,j) {for(int aaa = 0;aaa < i;aaa++){for(int bbb = 0;bbb < j;bbb++)cerr << v[aaa][bbb] << ' ';cerr << endl;}cerr << endl;} #define ALL(v) v.begin(),v.end() #define Decimal fixed<<setprecision(20) #define INF 1000000000 #define LLINF 1000000000000000000LL #define MOD 1000000007 typedef long long ll; typedef pair<ll,ll> P; int aa[2222][2222]; int aa1[2222][2222]; int a1a1[2222][2222]; int mp[2222][2222]; int n, d, ma; int getImos(int a,int b,int c,int d,int v[2222][2222]){ int ret = v[c][d]; if(a > 0){ ret -= v[a-1][d]; } if(b > 0){ ret -= v[c][b-1]; } if(a > 0 && b > 0){ ret += v[a-1][b-1]; } return ret; } void makeImos() { REP(i,2*d){ REP(j,2*d){ aa[i+1][j] += aa[i][j]; aa1[i+1][j] += aa1[i][j]; a1a1[i+1][j] += a1a1[i][j]; } } REP(i,2*d){ REP(j,2*d){ aa[i][j+1] += aa[i][j]; aa1[i][j+1] += aa1[i][j]; a1a1[i][j+1] += a1a1[i][j]; } } } void makeBoad() { REP(i,d){ REP(j,d){ while((ma+1) * (ma+1) < mp[i][j])ma++; } } REP(i,d){ REP(j,d){ if(mp[i][j] > ma * ma){ aa[i][j]++;aa[i+d][j]++;aa[i][j+d]++;aa[i+d][j+d]++; } if(mp[i][j] > ma * (ma + 1)){ aa1[i][j]++;aa1[i+d][j]++;aa1[i][j+d]++;aa1[i+d][j+d]++; } if(mp[i][j] > (ma + 1) * (ma + 1)){ a1a1[i][j]++;a1a1[i+d][j]++;a1a1[i][j+d]++;a1a1[i+d][j+d]++; } } } } bool check(int mid) { bool flag = false; REP(i,d){ REP(j,d){ int tmp = 0; tmp += getImos(i,j,i+mid,j+mid,a1a1); tmp += getImos(i,j+mid+1,i+mid,j+d-1,aa1); tmp += getImos(i+mid+1,j,i+d-1,j+mid,aa1); tmp += getImos(i+mid+1,j+mid+1,i+d-1,j+d-1,aa); if(tmp == 0)flag = true; } } return flag; } void input() { cin >> n >> d; REP(i,n){ int x, y;cin >> x >> y; x %= d;y %= d; mp[y][x]++; } } int BinarySearch() { int l = -2,r = d-1; while(r - l > 1){ int mid = (l + r) / 2; if(check(mid))r = mid; else l = mid; } return r; } int main(){ input(); makeBoad(); makeImos(); int ans = BinarySearch(); cout << d * ma + ans << endl; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
// includes {{{ #include<iostream> #include<iomanip> #include<algorithm> #include<vector> #include<stack> #include<queue> #include<deque> #include<map> #include<set> #include<tuple> #include<cmath> #include<random> #include<cassert> // }}} // #include<bits/stdc++.h> using namespace std; using ll = long long; ll n, d; const int N = 1e5; int r[N + 1]; int c[1000][1000]; int s[1000][1000]; int t1[1000][1000]; int t2[1000][1000]; // range2D accum2D {{{ template < class T > ll range2D(T &val, int y1, int x1, int y2, int x2, int h, int w) { if(y1 < 0) y1 = 0; if(x1 < 0) x1 = 0; if(y2 >= h) y2 = h - 1; if(x2 >= w) x2 = w - 1; if(y1 > y2 || x1 > x2) return ll(0); ll res(val[y2][x2]); if(y1 - 1 >= 0) res -= val[y1 - 1][x2]; if(x1 - 1 >= 0) res -= val[y2][x1 - 1]; if(y1 - 1 >= 0 && x1 - 1 >= 0) res += val[y1 - 1][x1 - 1]; return res; } template < class T > void accum2D(T &val, int h, int w) { for(int i = 1; i < h; i++) for(int j = 0; j < w; j++) val[i][j] += val[i - 1][j]; for(int i = 0; i < h; i++) for(int j = 1; j < w; j++) val[i][j] += val[i][j - 1]; } template < class T > ll range2Dloop(T &val, ll ty1, ll tx1, ll y2, ll x2, int h, int w) { if(ty1 > y2 || tx1 > x2) return ll(0); ll y1 = ty1 % h; if(y1 < 0) y1 += h; ll x1 = tx1 % w; if(x1 < 0) x1 += w; y2 += y1 - ty1; x2 += x1 - tx1; ll gy = y2 / h; ll gx = x2 / w; y2 %= h; x2 %= w; ll res(0); if(gy == 0 && gx == 0) { res += range2D<T>(val, y1, x1, y2, x2, h, w); } else if(gy == 0) { res += range2D<T>(val, y1, x1, y2, w - 1, h, w); res += range2D<T>(val, y1, 0, y2, x2, h, w); res += range2D<T>(val, y1, 0, y2, w - 1, h, w) * (gx - 1); } else if(gx == 0) { res += range2D<T>(val, y1, x1, h - 1, x2, h, w); res += range2D<T>(val, 0, x1, y2, x2, h, w); res += range2D<T>(val, 0, x1, h - 1, x2, h, w) * (gy - 1); } else { res += range2D<T>(val, y1, x1, h - 1, w - 1, h, w); // UL res += range2D<T>(val, 0, x1, y2, w - 1, h, w); // DL res += range2D<T>(val, y1, 0, h - 1, x2, h, w); // UR res += range2D<T>(val, 0, 0, y2, x2, h, w); // DR res += range2D<T>(val, y1, 0, h - 1, w - 1, h, w) * (gx - 1); // U res += range2D<T>(val, 0, 0, y2, w - 1, h, w) * (gx - 1); // D res += range2D<T>(val, 0, x1, h - 1, w - 1, h, w) * (gy - 1); // L res += range2D<T>(val, 0, x1, h - 1, x2, h, w) * (gy - 1); // R res += range2D<T>(val, 0, 0, h - 1, w - 1, h, w) * (gy - 1) * (gx - 1); // C } return res; } // }}} int m(int x, int y) { return (x % y + y) % y; } int main() { std::ios::sync_with_stdio(false), std::cin.tie(0); cin >> n >> d; for(int i = 0; i < n; i++) { int x, y; cin >> x >> y; c[m(x, d)][m(y, d)]++; } r[0] = 0; for(int i = 1; i <= N; i++) { r[i] = r[i - 1]; if(r[i] * r[i] < i) r[i]++; } int ma = 0; int sum1 = 0, sum2 = 0; for(int i = 0; i < d; i++) for(int j = 0; j < d; j++) s[i][j] = r[c[i][j]], ma = max(ma, s[i][j]); for(int i = 0; i < d; i++) for(int j = 0; j < d; j++) { if(s[i][j] == ma) { if(s[i][j] * (s[i][j] - 1) >= c[i][j]) { t2[i][j]++; sum2++; } else { t1[i][j]++; sum1++; } } } accum2D(t1, d, d); accum2D(t2, d, d); // d * (ma - 1) + (1 to d) int ok = d, ng = 0; while(ok - ng > 1) { int mid = (ok + ng) >> 1; int f = 0; for(int i = 0; i < d; i++) for(int j = 0; j < d; j++) { if(range2Dloop(t1, i, j, i + mid - 1, j + mid - 1, d, d) == sum1 && range2Dloop(t2, i + mid, j + mid, i + d - 1, j + d - 1, d, d) == 0) { f = 1; goto b; } } b: if(f) ok = mid; else ng = mid; } cout << d * (ma - 1) + ok - 1 << endl; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> pii; typedef long long ll; typedef vector<int> vi; #define pb push_back #define eb emplace_back #define mp make_pair #define fi first #define se second #define rep(i,n) rep2(i,0,n) #define rep2(i,m,n) for(int i=m;i<(n);i++) #define ALL(c) (c).begin(),(c).end() #define dump(x) cout << #x << " = " << (x) << endl constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); } template<class T, class U> void chmin(T& t, const U& u) { if (t > u) t = u; } template<class T, class U> void chmax(T& t, const U& u) { if (t < u) t = u; } template<class T, class U> ostream& operator<<(ostream& os, const pair<T, U>& p) { os<<"("<<p.first<<","<<p.second<<")"; return os; } template<class T> ostream& operator<<(ostream& os, const vector<T>& v) { os<<"{"; rep(i, v.size()) { if (i) os<<","; os<<v[i]; } os<<"}"; return os; } const int maxd = 1010; int cnt[maxd][maxd]; int r[3][maxd * 2][maxd * 2]; int main() { int N, D; cin >> N >> D; rep(i, N) { int x, y; cin >> x >> y; ++cnt[x % D][y % D]; } int mx = 0; rep(i, D) { rep(j, D) { chmax(mx, cnt[i][j]); } } int p = 0; while ((p + 1) * (p + 1) < mx) ++p; for (int i = 0; i < maxd * 2 - 1; ++i) { for (int j = 0; j < maxd * 2 - 1; ++j) { int x = cnt[i % D][j % D]; rep(k, 3) { r[k][i+1][j+1] = r[k][i+1][j] + r[k][i][j+1] - r[k][i][j]; } if (x <= p * p) { ++r[0][i+1][j+1]; } if (x <= p * (p+1)) { ++r[1][i+1][j+1]; } if (x <= (p+1) * (p+1)) { ++r[2][i+1][j+1]; } } } auto valid = [&](int a, int b, int c, int d, int k) { if (!(a <= c && b <= d)) return true; int num = r[k][c+1][d+1] - r[k][c+1][b] - r[k][a][d+1] + r[k][a][b]; return num == (c - a + 1) * (d - b + 1); }; int ans = TEN(9); rep(i, D) { rep(j, D) { auto ok = [&](int x) { int t = p * D + x; if (!valid(i, j, i + x, j + x, 2)) { return 0; } if (!valid(i + x + 1, j, i + D - 1, j + x, 1)) { return 0; } if (!valid(i, j + x + 1, i + x, j + D - 1, 1)) { return 0; } if (!valid(i + x + 1, j + x + 1, i + D - 1, j + D - 1, 0)) { return 0; } return 1; }; int lo = 0, hi = D; if (ok(lo)) { hi = 0; } while (hi - lo > 1) { int m = (lo + hi) / 2; if (ok(m)) { hi = m; } else { lo = m; } } chmin(ans, p * D + hi); } } cout << ans << endl; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <cstdio> #include <cstring> #include <algorithm> #include <vector> #define P 998244353 #define M 200010 using namespace std; typedef long long ll; struct node { int x, y; node() {} node(int _1, int _2) {x = _1, y = _2;} }; int n, D, S[1111][1111]; int A[2222][2222], B[2222][2222]; node X[M]; ll sqr(ll x) { return x * x; } ll getrg(int (*A)[2222], int l, int r, int u, int d) { if(l > r || u > d) return 0; int ans = A[r][d]; if(l) ans -= A[l - 1][d]; if(u) ans -= A[r][u - 1]; if(l && u) ans += A[l - 1][u - 1]; return ans; } bool check(ll x) { ll tmp1 = sqr(x / D), tmp2 = sqr(x / D + 1), tmp12 = (x / D) * (x / D + 1); memset(A, 0, sizeof A); memset(B, 0, sizeof B); for(int i = 0; i < D * 2; i++) for(int j = 0; j < D * 2; j++) if(S[i % D][j % D] > tmp2) return 0; else {if(S[i % D][j % D] > tmp12) B[i][j] = 1;if(S[i % D][j % D] > tmp1) A[i][j] = 1;} for(int i = 1; i < D * 2; i++) B[0][i] += B[0][i - 1], A[0][i] += A[0][i - 1], B[i][0] += B[i - 1][0], A[i][0] += A[i - 1][0]; for(int i = 1; i < D * 2; i++) for(int j = 1; j < D * 2; j++) { A[i][j] += A[i - 1][j] + A[i][j - 1] - A[i - 1][j - 1]; B[i][j] += B[i - 1][j] + B[i][j - 1] - B[i - 1][j - 1]; } for(int i = D; i < D * 2; i++) for(int j = D; j < D * 2; j++) { int s = x % D; if(!getrg(B, i - D + 1, i - s, j - s + 1, j) && !getrg(B, i - s + 1, i, j - D + 1, j - s) && !getrg(A, i - D + 1, i - s, j - D + 1, j - s)) return 1; } return 0; } int main() { scanf("%d%d", &n, &D); for(int i = 1; i <= n; i++) { int a, b; scanf("%d%d", &a, &b); X[i] = node(a, b); S[a % D][b % D]++; } int ans = 1e9 + 5; for(int l = 1, r = 1e9; l <= r; ) { int md = (l + r) / 2; if(check(md)) ans = md, r = md - 1; else l = md + 1; } printf("%d\n", ans - 1); return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<b;i++) #define rrep(i,a,b) for(int i=a;i>=b;i--) #define fore(i,a) for(auto &i:a) #define all(x) (x).begin(),(x).end() #pragma GCC optimize ("-O3") using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); } typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60; template<class T>bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } //--------------------------------------------------------------------------------------------------- template<typename T, int VW, int VH> struct Ruisekiwa2D { T v[VH][VW]; Ruisekiwa2D() { reset(); } void reset() { rep(y, 0, VH) rep(x, 0, VW) v[y][x] = 0; } void set(int x, int y, T c) { v[y][x] = c; } void build() { rep(y, 0, VH) rep(x, 0, VW) { if (0 < y) v[y][x] += v[y - 1][x]; if (0 < x) v[y][x] += v[y][x - 1]; if (0 < y && 0 < x) v[y][x] -= v[y - 1][x - 1]; } } T get(int sx, int sy, int tx, int ty) { assert(sx <= tx && sy <= ty); T rs = v[ty][tx]; if (0 < sx) rs -= v[ty][sx - 1]; if (0 < sy) rs -= v[sy - 1][tx]; if (0 < sx && 0 < sy) rs += v[sy - 1][sx - 1]; return rs; } }; /*---------------------------------------------------------------------------------------------------             ∧_∧       ∧_∧  (´<_` )  Welcome to My Coding Space!      ( ´_ゝ`) /  ⌒i     /   \    | |     /   / ̄ ̄ ̄ ̄/  |   __(__ニつ/  _/ .| .|____      \/____/ (u ⊃ ---------------------------------------------------------------------------------------------------*/ int N, D, X[101010], Y[101010]; //--------------------------------------------------------------------------------------------------- int cnt[1010][1010]; Ruisekiwa2D<int, 2010, 2010> aa, ab, bb; int check(int r) { aa.reset(); ab.reset(); bb.reset(); int a = r / D; int b = r % D; rep(y, 0, D * 2) rep(x, 0, D * 2) { int xx = x % D, yy = y % D; if (1LL * a * a < cnt[yy][xx]) { aa.set(x, y, 1); } if (1LL * a * (a + 1) < cnt[yy][xx]) { ab.set(x, y, 1); } if (1LL * (a + 1) * (a + 1) < cnt[yy][xx]) { bb.set(x, y, 1); } } aa.build(); ab.build(); bb.build(); rep(sy, 0, D) rep(sx, 0, D) { int ok = 1; if (0 < b) { if (bb.get(sx, sy, sx + b - 1, sy + b - 1)) ok = 0; if (ab.get(sx, sy + b, sx + b - 1, sy + D - 1)) ok = 0; if (ab.get(sx + b, sy, sx + D - 1, sy + b - 1)) ok = 0; } if (aa.get(sx + b, sy + b, sx + D - 1, sy + D - 1)) ok = 0; if (ok) return 1; } return 0; } //--------------------------------------------------------------------------------------------------- void _main() { cin >> N >> D; rep(i, 0, N) cin >> X[i] >> Y[i]; rep(i, 0, N) cnt[Y[i] % D][X[i] % D]++; int ng = 1, ok = inf; while (ng + 1 != ok) { int md = (ng + ok) / 2; if (check(md)) ok = md; else ng = md; } cout << ok - 1 << endl; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
/* [dwacon5th-prelims] D - Square Rotation */ #include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long int ll; typedef pair<int, int> pii; typedef pair<ll, int> pli; const int MAX_N = 1e5; const int MAX_D = 1000; int N, D; int x[MAX_N], y[MAX_N]; int c[MAX_D][MAX_D]; int s[3][MAX_D * 2 + 1][MAX_D * 2 + 1]; bool can(int q) { for (int a = 0; a < D; a++) { for (int b = 0; b < D; b++) { bool is_ok = (s[2][a + q + 1][b + q + 1] - s[2][a + q + 1][b] - s[2][a][b + q + 1] + s[2][a][b] == 0) && (s[1][a + q + 1][b + D] - s[1][a + q + 1][b + q + 1] - s[1][a][b + D] + s[1][a][b + q + 1] == 0) && (s[1][a + D][b + q + 1] - s[1][a + D][b] - s[1][a + q + 1][b + q + 1] + s[1][a + q + 1][b] == 0) && (s[0][a + D][b + D] - s[0][a + D][b + q + 1] - s[0][a + q + 1][b + D] + s[0][a + q + 1][b + q + 1] == 0); if (is_ok) { return true; } } } return false; } ll solve() { int p = 0; for (int i = 0; i < N; i++) { c[x[i] % D][y[i] % D]++; } for (int i = 0; i < D; i++) { for (int j = 0; j < D; j++) { while ((p + 1) * (p + 1) < c[i][j]) { p++; } } } int thresh[3] = {p * p, p * (p + 1), (p + 1) * (p + 1)}; for (int k = 0; k < 3; k++) { for (int i = 0; i < D; i++) { for (int j = 0; j < D; j++) { if (c[i][j] > thresh[k]) { s[k][1 + i][1 + j]++; s[k][1 + i + D][1 + j]++; s[k][1 + i][1 + j + D]++; s[k][1 + i + D][1 + j + D]++; } } } } for (int k = 0; k < 3; k++) { for (int i = 1; i <= 2 * D; i++) { for (int j = 0; j <= 2 * D; j++) { s[k][i + 1][j] += s[k][i][j]; } } for (int i = 0; i <= 2 * D; i++) { for (int j = 1; j <= 2 * D; j++) { s[k][i][j + 1] += s[k][i][j]; } } } int ng = -1, ok = D - 1; while (ok - ng > 1) { int m = (ok + ng) / 2; if (can(m)) { ok = m; } else { ng = m; } } ll ans = p * D + ok; return ans; } int main() { cin >> N >> D; for (int i = 0; i < N; i++) { cin >> x[i] >> y[i]; } cout << solve() << endl; return 0; }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for(long long i=0;i<(long long)(n);i++) #define REP(i,k,n) for(long long i=k;i<(long long)(n);i++) #define all(a) a.begin(),a.end() #define pb emplace_back #define eb emplace_back #define lb(v,k) (lower_bound(all(v),k)-v.begin()) #define ub(v,k) (upper_bound(all(v),k)-v.begin()) #define fi first #define se second #define pi M_PI #define PQ(T) priority_queue<T> #define SPQ(T) priority_queue<T,vector<T>,greater<T>> #define dame(a) {out(a);return 0;} #define decimal cout<<fixed<<setprecision(15); #define dupli(a) a.erase(unique(all(a)),a.end()) typedef long long ll; typedef pair<ll,ll> P; typedef tuple<ll,ll,ll> PP; typedef tuple<ll,ll,ll,ll> PPP; typedef multiset<ll> S; using vi=vector<ll>; using vvi=vector<vi>; using vvvi=vector<vvi>; using vvvvi=vector<vvvi>; using vp=vector<P>; using vvp=vector<vp>; using vb=vector<bool>; using vvb=vector<vb>; const ll inf=1001001001001001001; const ll INF=1001001001; const ll mod=1000000007; const double eps=1e-10; template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;} template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;} template<class T> void out(T a){cout<<a<<'\n';} template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';} template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';} template<class T> void outvvp(T v){rep(i,v.size())outvp(v[i]);} template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';} template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);} template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);} template<class T> void yesno(T b){if(b)out("yes");else out("no");} template<class T> void YesNo(T b){if(b)out("Yes");else out("No");} template<class T> void YESNO(T b){if(b)out("YES");else out("NO");} template<class T> void noyes(T b){if(b)out("no");else out("yes");} template<class T> void NoYes(T b){if(b)out("No");else out("Yes");} template<class T> void NOYES(T b){if(b)out("NO");else out("YES");} void outs(ll a,ll b){if(a>=inf-100)out(b);else out(a);} ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);} ll modpow(ll a,ll b){ll res=1;a%=mod;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;} ll sum1[2000][2000],sum2[2000][2000]; ll get1(int i,int j,int k,int l){ if(k==0||l==0)return 0; ll res=sum1[k-1][l-1]; if(i)res-=sum1[i-1][l-1]; if(j)res-=sum1[k-1][j-1]; if(i&&j)res+=sum1[i-1][j-1]; return res; } ll get2(int i,int j,int k,int l){ if(k==0||l==0)return 0; ll res=sum2[k-1][l-1]; if(i)res-=sum2[i-1][l-1]; if(j)res-=sum2[k-1][j-1]; if(i&&j)res+=sum2[i-1][j-1]; return res; } int main(){ ll n,d;cin>>n>>d; vvi num(d,vi(d)); rep(i,n){ ll x,y;cin>>x>>y; num[x%d][y%d]++; } ll ok=1000000,ng=0; while(ok-ng>1){ ll md=(ok+ng)/2; bool able=false; ll a=md/d,b=md%d; rep(i,d*2)rep(j,d*2){sum1[i][j]=0;sum2[i][j]=0;} rep(i,d)rep(j,d){ if(num[i][j]>(a+1)*(a+1))able=true; if(num[i][j]>a*a){ sum1[i][j]++; sum1[i+d][j]++; sum1[i][j+d]++; sum1[i+d][j+d]++; } if(num[i][j]>a*(a+1)){ sum2[i][j]++; sum2[i+d][j]++; sum2[i][j+d]++; sum2[i+d][j+d]++; } } rep(i,d*2)rep(j,d*2-1){ sum1[i][j+1]+=sum1[i][j]; sum2[i][j+1]+=sum2[i][j]; } rep(j,d*2)rep(i,d*2-1){ sum1[i+1][j]+=sum1[i][j]; sum2[i+1][j]+=sum2[i][j]; } if(able){ ng=md;continue; } rep(i,d){ if(able)break; rep(j,d){ if(able)break; if(get1(i+b,j+b,i+d,j+d)==0&&get2(i,j,i+d,j+d)==get2(i,j,i+b,j+b))able=true; } } if(able)ok=md; else ng=md; } out(ok-1); }
CPP
p03217 Dwango Programming Contest V - Square Rotation
Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor. Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them. In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume). He may perform the following operation arbitrarily many times: * Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point. Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square. Find the minimum scatteredness after he performs arbitrarily many operations. Constraints * 2 \leq N \leq 10^5 * 1 \leq D \leq 1000 * 0 \leq x_i, y_i \leq 10^9 * Given coordinates are pairwise distinct * All numbers given in input are integers Input Input is given from Standard Input in the following format: N D x_1 y_1 : x_N y_N Output Print the answer. Examples Input 3 1 0 0 1 0 2 0 Output 1 Input 19 2 1 3 2 3 0 1 1 1 2 1 3 1 4 4 5 4 6 4 7 4 8 4 8 3 8 2 8 1 8 0 7 0 6 0 5 0 4 0 Output 4 Input 8 3 0 0 0 3 3 0 3 3 2 2 2 5 5 2 5 5 Output 4
6
0
#include<bits/stdc++.h> #define int long long using namespace std; const int N=1e5+2; const int M=1e3+2; const int inf=1e9+7; int cnt[M][M],cnt1[3][M][M],d; int getsum(int idx,int l,int r,int l2,int r2){ int ans=cnt1[idx][l2][r2]; if(l){ ans-=cnt1[idx][l-1][r2]; } if(r){ ans-=cnt1[idx][l2][r-1]; } if(l&&r){ ans+=cnt1[idx][l-1][r-1]; } return ans; } int getrec(int idx,int l,int r,int a,int b){ if(l+a<d&&r+b<d){ return getsum(idx,l,r,l+a,r+b); } else{ if(l+a<d&&r+b>=d){ return getsum(idx,l,r,l+a,d-1)+getsum(idx,l,0,l+a,r+b-d); } else{ if(l+a>=d&&r+b<d){ return getsum(idx,l,r,d-1,r+b)+getsum(idx,0,r,l+a-d,r+b); } else{ return getsum(idx,l,r,d-1,d-1)+getsum(idx,l,0,d-1,r+b-d)+getsum(idx,0,r,l+a-d,d-1)+getsum(idx,0,0,l+a-d,r+b-d); } } } } signed main(){ ios::sync_with_stdio(0); cin.tie(0); int n,m,i,j,k,l,z,num=0,lef,rig,mid; cin>>n>>d; for(i=1;i<=n;i++){ cin>>j>>k; cnt[j%d][k%d]++; } for(i=0;i<d;i++){ for(j=0;j<d;j++){ if(!cnt[i][j]){ continue; } z=sqrt(cnt[i][j])-1; while((z+1)*(z+1)<cnt[i][j]){ z++; } num=max(num,z); } } // cout<<num<<endl; lef=num*d,mid=rig=num*d+d-1; while(lef<rig){ mid=(lef+rig)/2; for(i=0;i<3;i++){ if(i==0){ l=(num+1)*(num+1); } if(i==1){ l=num*(num+1); } if(i==2){ l=num*num; } for(j=0;j<d;j++){ for(k=0;k<d;k++){ cnt1[i][j][k]=(cnt[j][k]>l); if(j){ cnt1[i][j][k]+=cnt1[i][j-1][k]; } if(k){ cnt1[i][j][k]+=cnt1[i][j][k-1]; } if(j&&k){ cnt1[i][j][k]-=cnt1[i][j-1][k-1]; } } } } bool cac=false; l=mid%d; for(i=0;i<d;i++){ for(j=0;j<d;j++){ int cost1=getrec(0,i,j,l,l),cost2=0,cost3=0; if(l<d-1){ cost2=max(getrec(1,(i+l+1)%d,j,d-1-l-1,l),getrec(1,i,(j+l+1)%d,l,d-1-l-1)); // if(i==2&&j==2){ // cout<<(i+l+1)%d<<' '<<j<<' '<<d-1-l-1<<' '<<l<<' '<<getrec(1,(i+l+1)%d,j,d-1-l-1,l)<<endl; // cout<<i<<' '<<(j+l+1)%d<<' '<<l<<' '<<d-1-l-1<<' '<<getrec(1,i,(j+l+1)%d,l,d-1-l-1)<<endl; // } cost3=getrec(2,(i+l+1)%d,(j+l+1)%d,d-l-1-1,d-l-1-1); } // if(i==2&&j==2){ // cout<<cost1<<' '<<cost2<<' '<<cost3<<" CAC"<<endl; // } if(!cost1&&!cost2&&!cost3){ cac=true; break; } } if(cac){ break; } } if(cac){ rig=mid; } else{ lef=mid+1; } } cout<<rig; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <bits/stdc++.h> #define ll long long #define N 1000100 ll n,s,ans,x[N],p[N]; using namespace std; int main(){ scanf("%lld%lld",&n,&s); for (ll i=1;i<=n;i++) scanf("%lld%lld",&x[i],&p[i]); for (ll l=1,r=n;l<=r;){ if (x[l]>=s){ans+=x[r]-s; break;}//所有人都在一边直接开过去 if (x[r]<=s){ans+=s-x[l]; break;} ans+=x[r]-x[l]; //每次来回开一趟 if (p[l]>=p[r]) while (l<r && p[l]>=p[r]) p[l]+=p[r],r--;//相同往左开 else while (l<r && p[l]<p[r]) p[r]+=p[l],l++; } printf("%lld\n",ans); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
import java.io.*; import java.util.*; public class Main { FastScanner in; PrintWriter out; void solve() { int n = in.nextInt(); int s = in.nextInt(); int[] x = new int[n]; long[] cnt = new long[n]; for (int i = 0; i < n; i++) { x[i] = in.nextInt(); cnt[i] = in.nextInt(); } if (s >= x[n - 1]) { out.println(s - x[0]); return; } if (s <= x[0]) { out.println(x[n - 1] - s); return; } long res = x[n - 1] - x[0]; int left = 0, right = n - 1; int end = n - 1; if (cnt[left] >= cnt[right]) { end = 0; } while (true) { if (x[right] < s || x[left] > s) { res += Math.abs(s - x[end]); break; } int nextEnd = -1; if (cnt[left] >= cnt[right]) { nextEnd = left; cnt[left] += cnt[right]; right--; } else { nextEnd = right; cnt[right] += cnt[left]; left++; } res += Math.abs(x[end] - x[nextEnd]); end = nextEnd; } out.println(res); } void solve123() { int n = in.nextInt(); int s = in.nextInt(); int[] x = new int[n]; int[] cnt = new int[n]; for (int i = 0; i < n; i++) { x[i ]= in.nextInt(); cnt[i] = in.nextInt(); } long res = 0; while (true) { long sum = 0; int pos = 0; for (int i = 0; i < n; i++) { sum += cnt[i]; if (s >= x[i]) { pos = i; } } if (sum == 0) { break; } long curSum = 0; int bestPos = 0; for (int right = pos + 1; right < n; right++) { if (cnt[right] > sum - curSum) { bestPos = right; } curSum += cnt[right]; } curSum = 0; for (int left = pos; left >= 0; left--) { if (cnt[left] != 0 && cnt[left] >= sum - curSum) { bestPos = left; } curSum += cnt[left]; } res += Math.abs(x[bestPos] - s); if (bestPos > pos) { for (int i = pos + 1; i <= bestPos; i++) { cnt[i] = 0; } } else { for (int i = pos; i >= bestPos; i--) { cnt[i] = 0; } } s = x[bestPos]; } out.println(res); } void run() { try { in = new FastScanner(new File("Main.in")); out = new PrintWriter(new File("Main.out")); solve(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } } void runIO() { in = new FastScanner(System.in); out = new PrintWriter(System.out); solve(); out.close(); } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } String next() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return null; st = new StringTokenizer(s); } return st.nextToken(); } boolean hasMoreTokens() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return false; st = new StringTokenizer(s); } return true; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } public static void main(String[] args) { new Main().runIO(); } }
JAVA
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <bits/stdc++.h> using namespace std; inline int gi() { char c = getchar(); while(c < '0' || c > '9') c = getchar(); int sum = 0; while('0' <= c && c <= '9') sum = sum * 10 + c - 48, c = getchar(); return sum; } typedef long long ll; const int maxn = 100005; int n, s, x[maxn]; ll ans, p[maxn]; int main() { n = gi(); s = gi(); for (int i = 1; i <= n; ++i) x[i] = gi(), p[i] = gi(); int l = 1, r = n, d = p[1] >= p[n]; while (1) { if (x[l] >= s) {ans += x[r] - s; break;} if (x[r] <= s) {ans += s - x[l]; break;} if (p[l] >= p[r]) { if (d != 0) {ans += x[r] - x[l]; d ^= 1;} p[l] += p[r--]; } else { if (d != 1) {ans += x[r] - x[l]; d ^= 1;} p[r] += p[l++]; } } printf("%lld\n", ans); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include<bits/stdc++.h> #define ll long long using namespace std; const int N=100010; ll n,s,x[N],p[N],cntl,cntr,ans; int main(){ scanf("%lld%lld",&n,&s); for(int i=1;i<=n;i++) scanf("%lld%lld",&x[i],&p[i]),x[i]<s?cntl++:cntr++; int l=1,r=n; bool dir=p[l]>=p[r]; while(cntl&&cntr){ if(p[l]>=p[r]) ans+=dir?x[r]-x[l]:0,p[l]+=p[r],r--,cntr--,dir=0; else ans+=dir?0:x[r]-x[l],p[r]+=p[l],l++,cntl--,dir=1; } cntl?ans+=s-x[l]:ans+=x[r]-s; printf("%lld",ans); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <cstdio> typedef long long ll; ll pos[100005], cnt[100005], ans; void work(int l, int r, int s) { if (pos[r] < s) { ans += s - pos[l]; return; } if (pos[l] > s) { ans += pos[r] - s; return; } ans += pos[r] - pos[l]; if (cnt[l] >= cnt[r]) { while (l < r && cnt[l] >= cnt[r]) cnt[l] += cnt[r--]; } else { while (l < r && cnt[l] < cnt[r]) cnt[r] += cnt[l++]; } work(l, r, s); } int main() { // freopen("AGC023-D.in", "r", stdin); int n, s; scanf("%d%d", &n, &s); for (int i = 0; i < n; i++) scanf("%lld%lld", pos + i, cnt + i); work(0, n - 1, s); printf("%lld\n", ans); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <bits/stdc++.h> using namespace std; #define int long long const int MAXN=100100; int x[MAXN],p[MAXN],lis[MAXN],n,s,pos,cntlis,l,r,ans; signed main() { ios::sync_with_stdio(false),cin.tie(0),cout.tie(0); cin>>n>>s; for(register int i=1;i<=n;i++) { cin>>x[i]>>p[i]; if(x[i]<s) pos=i; } l=1,r=n; while((l<=pos&&r>pos)||(r<=pos&&l>pos)) if(p[l]>=p[r]) lis[++cntlis]=x[r],p[l]+=p[r],r--; else lis[++cntlis]=x[l],p[r]+=p[l],l++; if(r<=pos) for(register int i=l;i<=r;i++) lis[++cntlis]=x[i]; else for(register int i=r;i>=l;i--) lis[++cntlis]=x[i]; lis[++cntlis]=s; for(register int i=1;i<cntlis;i++) ans=ans+abs(lis[i]-lis[i+1]); cout<<ans<<endl; return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 50; bool p[N]; long long a[N], b[N]; int n, s; int main() { scanf("%d%d", &n, &s); for (int i = 1; i <= n; i ++) scanf("%lld%lld", &a[i], &b[i]); long long ans = 0; for (int pl = 1, pr = n; ;) { if (a[pl] > s) { ans += a[pr] - s; break; } else if (a[pr] < s) { ans += s - a[pl]; break; } else { if (b[pl] >= b[pr]) { if (!p[pl]) ans += a[pr] - a[pl]; p[pl] = 1; b[pl] += b[pr]; pr --; } else { if (!p[pr]) ans += a[pr] - a[pl]; p[pr] = 1; b[pr] += b[pl]; pl ++; } } } printf("%lld\n", ans); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <cstdio> typedef long long LL; const int MN = 100005; int N, s, x[MN]; LL p[MN]; LL calc(int l, int r, int t) { if (s < x[l]) return x[r] - s; if (x[r] < s) return s - x[l]; if (p[l] >= p[r]) return p[l] += p[r], calc(l, r - 1, l) + (t == r ? x[r] - x[l] : 0); else return p[r] += p[l], calc(l + 1, r, r) + (t == l ? x[r] - x[l] : 0); } int main() { scanf("%d%d", &N, &s); for (int i = 1; i <= N; ++i) scanf("%d%lld", &x[i], &p[i]); printf("%lld\n", calc(1, N, p[1] < p[N] ? 1 : N)); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <bits/stdc++.h> const int MAXN = 100010; typedef long long LL; int n, X[MAXN], S; LL ans = 0, P[MAXN]; int lst = -1; inline int absx(int x) { return x < 0 ? -x : x; } void add(int x) { if (lst != -1) ans += absx(lst - x); lst = x; } int main() { std::ios_base::sync_with_stdio(false), std::cin.tie(0); std::cin >> n >> S; for (int i = 1; i <= n; ++i) std::cin >> X[i] >> P[i]; int lcur = 1, rcur = n; while (X[lcur] <= S && S <= X[rcur]) { if (P[lcur] >= P[rcur]) { P[lcur] += P[rcur]; add(X[rcur--]); } else { P[rcur] += P[lcur]; add(X[lcur++]); } } if (X[lcur] <= S) for (int i = lcur; i <= rcur; ++i) add(X[i]); else for (int i = rcur; i >= lcur; --i) add(X[i]); add(S); std::cout << ans << std::endl; return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include<cstdio> #include<algorithm> using namespace std; const int N=1e5+5; int n,s,x[N];long long p[N],ans; int main(){ scanf("%d%d",&n,&s); for(int i=1;i<=n;++i)scanf("%d%lld",&x[i],&p[i]); for(int l=1,r=n,las=0;;){ if(s<x[l]){ans+=x[r]-s;break;} if(x[r]<s){ans+=s-x[l];break;} if(p[l]>=p[r]){ if(las!=1)las=1,ans+=x[r]-x[l]; p[l]+=p[r];--r; }else{ if(las!=2)las=2,ans+=x[r]-x[l]; p[r]+=p[l];++l; } } printf("%lld\n",ans); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <bits/stdc++.h> using namespace std; namespace TYC { typedef long long ll; const int N = 1e5 + 5; int n, S, X[N]; ll ans, P[N]; void getans(const int l, const int r) { if (l != r && X[l] <= S && S <= X[r]) { if (P[l] >= P[r]) { P[l] += P[r]; getans(l, r - 1); ans += X[r] - S; S = X[r]; } else { P[r] += P[l]; getans(l + 1, r); ans += S - X[l]; S = X[l]; } } else { if (X[r] <= S) ans += S - X[l], S = X[l]; else ans += X[r] - S, S = X[r]; return; } } void work() { scanf("%d%d", &n, &S); for (int i = 1; i <= n; i++) scanf("%d%lld", &X[i], &P[i]); getans(1, n); printf("%lld\n", ans); } } int main() { TYC::work(); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <bits/stdc++.h> #define rep(i, a, b) for(int i = (a); i <= (b); i++) #define per(i, a, b) for(int i = (a); i >= (b); i--) using namespace std; typedef long long ll; const int N = 100010; int n, a[N], st, now; ll ans, p[N]; int main() { scanf("%d%d", &n, &st); rep(i, 1, n) scanf("%d%lld", &a[i], &p[i]); int l = 1, r = n; for(; l <= r;) { if(st < a[l] || (a[r] > st && p[l] >= p[r])) { if(now) ans += abs(a[r] - now); now = a[r]; p[l] += p[r], --r; } else { if(now) ans += abs(now - a[l]); now = a[l]; p[r] += p[l], ++l; } } ans += abs(now - st), printf("%lld\n", ans); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include<cstdio> inline int read() { int x;char c; while((c=getchar())<'0'||c>'9'); for(x=c-'0';(c=getchar())>='0'&&c<='9';)x=x*10+c-'0'; return x; } #define MN 100000 int x[MN+5],z[MN+5],zn;long long p[MN+5],ans; int main() { int n=read(),s=read(),i,j=0,a,b; for(i=1;i<=n;++i)(x[i]=read())<s?j=i:0,p[i]=read(); for(a=1,b=n;a<=j&&b>j;) if(p[b]>p[a])z[++zn]=a,p[b]+=p[a++]; else z[++zn]=b,p[a]+=p[b--]; while(a<=j)z[++zn]=a++; while(b>j)z[++zn]=b--; for(i=zn;i;--i)ans+=s<x[z[i]]?x[z[i]]-s:s-x[z[i]],s=x[z[i]]; printf("%lld",ans); }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <algorithm> #include <cstdio> #include <cstdlib> #include <cstring> #define MAXN 100010 int n, s, x[MAXN]; long long p[MAXN], ans = 0; int main() { scanf("%d%d", &n, &s); for (int i = 1; i <= n; i++) scanf("%d%lld", &x[i], &p[i]); int l = 1, r = n, last = 0; while (l < r && x[l] < s && x[r] > s) { if (p[l] < p[r]) { ans += last == 1 ? 0 : (x[r] - x[l]); last = 1; p[r] += p[l]; l++; } else { ans += last == 2 ? 0 : (x[r] - x[l]); last = 2; p[l] += p[r]; r--; } } printf("%lld\n", ans + std::max(s - x[l], x[r] - s)); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <bits/stdc++.h> using namespace std; int n, s, l, r; int x[100020]; long long p[100020]; long long z; int main() { scanf("%d%d", &n, &s); for (int i = 0; i < n; i++) { scanf("%d%d", &x[i], &p[i]); } for (int l = 0, r = n - 1; l <= r;) { if (x[l] >= s) { z += x[r] - s; break; } if (x[r] <= s) { z += s - x[l]; break; } z += x[r] - x[l]; if (p[l] >= p[r]) { while (l < r && p[l] >= p[r]) { p[l] += p[r--]; } } else { while (l < r && p[l] < p[r]) { p[r] += p[l++]; } } } printf("%lld\n", z); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <bits/stdc++.h> const int N = 100000; int x[N]; long long p[N]; int main() { int n, s; scanf("%d%d", &n, &s); for (int i = 0, a; i < n; ++ i) { scanf("%d%d", x + i, &a); p[i] = a; } int i = 0; int j = n - 1; long long result = 0; while (x[i] < s && s < x[j]) { result += x[j] - x[i]; if (p[i] >= p[j]) { while (i < j && p[i] >= p[j]) { p[i] += p[j --]; } } else { while (i < j && p[i] < p[j]) { p[j] += p[i ++]; } } } if (x[j] <= s) { result += s - x[i]; } else { result += x[j] - s; } printf("%lld\n", result); }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> using namespace std; const int maxn=1000010; int n; long long st,ans,x[maxn],y[maxn]; int main() { scanf("%d%lld",&n,&st); for (int i=1;i<=n;i++) { scanf("%lld%lld",&x[i],&y[i]); } int l=1; int r=n; while (l<=r) { if (x[l]>=st) { ans=ans+x[r]-st; break; } if (x[r]<=st) { ans=ans+st-x[l]; break; } ans=ans+x[r]-x[l]; if (y[l]>=y[r]) { while (y[l]>=y[r] && l<r) { y[l]=y[l]+y[r]; r--; } } else { while (y[l]<y[r] && l<r) { y[r]=y[r]+y[l]; l++; } } } printf("%lld\n",ans); }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <math.h> using namespace std; typedef long long ll; const int MAXN = 1e5 + 10; int N, S; int X[MAXN]; ll P[MAXN]; ll ans; int main() { register int i; scanf("%d%d", &N, &S); for(i = 1; i <= N; ++i) scanf("%d%lld", X + i, P + i); int l = 1, r = N, las = 0; while(X[l] <= S && S <= X[r]) { if(P[l] >= P[r]) P[l] += P[r], ans += (X[r--] - X[l]) * (las != 1), las = 1; else P[r] += P[l], ans += (X[r] - X[l++]) * (las != 2), las = 2; } if(l <= r) { if(X[l] <= S) ans += S - X[l]; else ans += X[r] - S; } printf("%lld\n", ans); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.IOException; import java.io.Reader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.io.BufferedReader; import java.util.Collections; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; MyInput in = new MyInput(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } static class TaskD { public void solve(int testNumber, MyInput in, PrintWriter out) { int n = in.nextInt(); int s = in.nextInt(); long[] x = new long[n]; long[] p = new long[n]; for (int i = 0; i < n; i++) { x[i] = in.nextLong(); p[i] = in.nextLong(); } // long[] sum = new long[n+1]; // for (int i = 0; i < n; i++) { // sum[i+1] = sum[i] + p[i]; // } long ans = 0; long cur = s; List<Integer> tour = new ArrayList<>(); tour.clear(); for (int l = 0, r = n - 1; l <= r; ) { if (x[l] > cur) { tour.add(r--); continue; } if (x[r] < cur) { tour.add(l++); continue; } if (p[l] < p[r]) { p[r] += p[l]; tour.add(l++); } else { p[l] += p[r]; tour.add(r--); } } Collections.reverse(tour); for (int t : tour) { ans += Math.abs(cur - x[t]); cur = x[t]; } // dump(next); out.println(ans); } } static class MyInput { private final BufferedReader in; private static int pos; private static int readLen; private static final char[] buffer = new char[1024 * 8]; private static char[] str = new char[500 * 8 * 2]; private static boolean[] isDigit = new boolean[256]; private static boolean[] isSpace = new boolean[256]; private static boolean[] isLineSep = new boolean[256]; static { for (int i = 0; i < 10; i++) { isDigit['0' + i] = true; } isDigit['-'] = true; isSpace[' '] = isSpace['\r'] = isSpace['\n'] = isSpace['\t'] = true; isLineSep['\r'] = isLineSep['\n'] = true; } public MyInput(InputStream is) { in = new BufferedReader(new InputStreamReader(is)); } public int read() { if (pos >= readLen) { pos = 0; try { readLen = in.read(buffer); } catch (IOException e) { throw new RuntimeException(); } if (readLen <= 0) { throw new MyInput.EndOfFileRuntimeException(); } } return buffer[pos++]; } public int nextInt() { int len = 0; str[len++] = nextChar(); len = reads(len, isSpace); int i = 0; int ret = 0; if (str[0] == '-') { i = 1; } for (; i < len; i++) ret = ret * 10 + str[i] - '0'; if (str[0] == '-') { ret = -ret; } return ret; } public long nextLong() { int len = 0; str[len++] = nextChar(); len = reads(len, isSpace); int i = 0; long ret = 0; if (str[0] == '-') { i = 1; } for (; i < len; i++) ret = ret * 10 + str[i] - '0'; if (str[0] == '-') { ret = -ret; } return ret; } public char nextChar() { while (true) { final int c = read(); if (!isSpace[c]) { return (char) c; } } } int reads(int len, boolean[] accept) { try { while (true) { final int c = read(); if (accept[c]) { break; } if (str.length == len) { char[] rep = new char[str.length * 3 / 2]; System.arraycopy(str, 0, rep, 0, str.length); str = rep; } str[len++] = (char) c; } } catch (MyInput.EndOfFileRuntimeException e) { } return len; } static class EndOfFileRuntimeException extends RuntimeException { } } }
JAVA
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN = 100010; int n, s; ll x[MAXN], p[MAXN], ans[MAXN]; vector< pair<int, int> > G[MAXN]; inline void add(int u, int v, int w) { G[u].push_back(make_pair(v, w)); } void dfs(int x) { for(int i = 0; i < (int)G[x].size(); ++i) { pair<int, int> y = G[x][i]; ans[y.first] = ans[x] + y.second; dfs(y.first); } } int main() { scanf("%d %d", &n, &s); for(int i = 1; i <= n; ++i) scanf("%lld %lld", &x[i], &p[i]); int l = 1, r = n; while(l < r) { if(x[l] >= s) add(l, l + 1, x[l + 1] - x[l]), ++l; else if(x[r] <= s) add(r, r - 1, x[r] - x[r - 1]), --r; else if(p[l] >= p[r]) add(l, r, x[r] - x[l]), p[l] += p[r], --r; else add(r, l, x[r] - x[l]), p[r] += p[l], ++l; } ans[l] = abs(s - x[l]); dfs(l); printf("%lld\n", max(ans[1], ans[n])); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**15 mod = 10**9+7 def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def pf(s): return print(s, flush=True) def main(): n,s = LI() a = [LI() for _ in range(n)] x = [_[0] for _ in a] p = [_[1] for _ in a] if x[0] > s: return x[-1] - s if x[-1] < s: return s - x[0] r = 0 i = 0 j = n - 1 d = p[:] e = collections.defaultdict(list) sk = set() while i < j: if x[i] > s: break if x[j] < s: break if d[i] >= d[j]: d[i] += d[j] j -= 1 else: d[j] += d[i] i += 1 t = s k = sorted(list(zip(d,range(n))), reverse=True) lx = rx = s for _,i in k: if lx <= x[i] <= rx: continue if lx > x[i]: lx = x[i] else: rx = x[i] r += abs(t-x[i]) t = x[i] return r print(main())
PYTHON3
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <cstdio> typedef long long ll; int n, S, X[100005]; ll P[100005]; int last(int l, int r) { if (S < X[l]) return r; if (S > X[r]) return l; return P[l] >= P[r] ? r : l; } int main() { scanf("%d%d", &n, &S); for (int i = 1; i <= n; ++i) scanf("%d%lld", X + i, P + i); ll ans = 0; for (int l = 1, r = n;;) { if (S < X[l]) { ans += X[r] - S; break; } if (S > X[r]) { ans += S - X[l]; break; } if (P[l] >= P[r]) P[l] += P[r], ans += X[r] - X[last(l, r - 1)], --r; else P[r] += P[l], ans += X[last(l + 1, r)] - X[l], ++l; } printf("%lld\n", ans); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
//21 #include<cstdio> #include<algorithm> #include<cmath> using namespace std; const int maxn = 100001; int n, s; int x[maxn]; long long p[maxn]; int main() { scanf("%d%d", &n, &s); for (int i = 1; i <= n; i++) { scanf("%d %lld", &x[i], &p[i]); } long long ans = 0; int l = 1; int r = n; int last = 0; while (x[l] < s && x[r] > s) { if (p[l] >= p[r]) { if (last) ans += abs(x[r] - x[last]); last = r; p[l] += p[r--]; } else { if (last) ans += abs(x[l] - x[last]); last = l; p[r] += p[l++]; } } if (x[l] > s) { if (last) ans += abs(x[r] - x[last]); ans += x[r] - s; } if (x[r] < s) { if (last) ans += abs(x[l] - x[last]); ans += s - x[l]; } printf("%lld", ans); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <iostream> using namespace std; int n,s; long long a[100005],b[100005],ans; int main() { cin>>n>>s; for (int i=1;i<=n;++i) cin>>a[i]>>b[i]; int l=1,r=n; while (l<=r) { if (s<=a[l]) { ans+=a[r]-s; break; } if (s>=a[r]) { ans+=s-a[l]; break; } if (b[l]>=b[r]) { ans+=a[r]-a[l]; while (l<r && b[l]>=b[r]) b[l]+=b[r--]; } else { ans+=a[r]-a[l]; while (l<r && b[l]<b[r]) b[r]+=b[l++]; } } cout<<ans<<endl; return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <bits/stdc++.h> using namespace std; const int N = 100005; int n, s; int par[N]; int x[N]; long long p[N]; long long res[N]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> s; for (int i = 1; i <= n; ++i) { cin >> x[i] >> p[i]; } stack <int> q; int l = 1, r = n; while(l < r && x[l] < s && x[r] > s) { if (p[l] >= p[r]) { res[r] += x[r] - x[l]; p[l] += p[r]; q.push(r); par[r] = l; --r; } else { res[l] += x[r] - x[l]; p[r] += p[l]; q.push(l); par[l] = r; ++l; } } for (int i = l; i <= r; ++i) res[i] = abs(s - x[i]); while(!q.empty()) { int u = q.top(); q.pop(); res[u] += res[par[u]]; } cout << *max_element(res + 1, res + n + 1) << '\n'; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<map> #include<set> using namespace std; typedef long long ll; typedef double db; #define fo(i,j,k) for(i=j;i<=k;i++) #define fd(i,j,k) for(i=j;i>=k;i--) #define cmax(a,b) (a=(a>b)?a:b) #define cmin(a,b) (a=(a<b)?a:b) const int N=3e5+5,M=6e5+5,mo=998244353; int n,st,x[N],le,ri,lst,i; ll y[N],ans; int main() { scanf("%d %d",&n,&st); fo(i,1,n) scanf("%d %lld",x+i,y+i); ans=0; le=1;ri=n; lst=2; while (x[le]<st&&st<x[ri]) { if (y[le]>=y[ri]) { if (lst!=-1) ans+=x[ri]-x[le]; lst=-1; y[le]+=y[ri]; ri--; }else { if (lst!=1) ans+=x[ri]-x[le]; lst=1; y[ri]+=y[le]; le++; } } ans+=max(abs(x[le]-st),abs(x[ri]-st)); printf("%lld\n",ans); }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include<bits/stdc++.h> #define int long long using namespace std; const int N=1e5+2; int ar[N],num[N]; signed main(){ ios::sync_with_stdio(0); cin.tie(0); int n,i,j,k,l,r,s,ans=0,lst=-1; cin>>n>>s; for(i=1;i<=n;i++){ cin>>ar[i]>>num[i]; } l=1; r=n; while(true){ if(ar[r]<s){ ans+=s-ar[l]; break; } if(ar[l]>s){ ans+=ar[r]-s; break; } if(num[l]>=num[r]){ num[l]+=num[r]; if(lst!=l){ ans+=ar[r]-ar[l]; } r--; lst=l; } else{ num[r]+=num[l]; if(lst!=r){ ans+=ar[r]-ar[l]; } l++; lst=r; } } cout<<ans; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> using namespace std; int n,l,r,t; long long s,dis[1000005],ans; struct node { long long len,num; }q[1000005]; bool cmp(node t1,node t2) { return t1.len<t2.len; } int main() { scanf("%d%lld",&n,&s); for (int i=1;i<=n; i++) scanf("%lld%lld",&q[i].len,&q[i].num); sort(q+1,q+1+n,cmp); l=1,r=n,t=2; while (q[l].len<s && q[r].len>s) { if (q[l].num<q[r].num) q[r].num+=q[l].num,dis[r]+=dis[l]+((t!=0)?(q[r].len-q[l].len):0),t=0,l++; else q[l].num+=q[r].num,dis[l]+=dis[r]+((t!=1)?(q[r].len-q[l].len):0),t=1,r--; } if (q[l].len>s) ans=dis[r]+q[r].len-s;else ans=dis[l]+s-q[l].len; printf("%lld\n",ans); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include<bits/stdc++.h> using namespace std; const int N = 1e5 + 11; #define LL long long int n; LL ans, s, x[N]; LL a[N]; LL sol(int l, int r){ //printf("l=%d r=%d al=%lld ar=%lld\n", l, r, a[l], a[r]); if(x[l] > s){ ans += x[r] - s; return x[r]; } if(x[r] < s){ ans += s - x[l]; return x[l]; } if(a[l] >= a[r]){ a[l] += a[r]; ans += abs(x[r] - sol(l, r - 1)); return x[r]; } else{ a[r] += a[l]; ans += abs(x[l] - sol(l + 1, r)); return x[l]; } } int main(){ cin>>n>>s; for(int i = 1;i <= n; i++){ scanf("%lld%lld", &x[i], &a[i]); } sol(1, n); cout<<ans<<endl; return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 1.1E6; long long pos[MAXN]; long long R[MAXN]; int n, S; long long ans; int getAns(int l, int r) { if (S < pos[l]) { ans += pos[r] - S; return pos[r]; } if (S > pos[r]) { ans += S - pos[l]; return pos[l]; } if (R[l] >= R[r]) { R[l] += R[r], ans += pos[r] - getAns(l, r - 1); return pos[r]; } else { R[r] += R[l], ans += getAns(l + 1, r) - pos[l]; return pos[l]; } } int main(void) { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n >> S; for (int i = 1;i <= n; ++i) cin >> pos[i] >> R[i]; getAns(1, n); cout << ans << '\n'; return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <bits/stdc++.h> #define ll long long using namespace std; const int maxn = 1000100; const int base = 1e9+7; int n; ll x[maxn],p[maxn],S,res=0; int main() { // freopen("in.txt","r",stdin); ios_base::sync_with_stdio(0); cin.tie(0); cin>>n>>S; for (int i=1;i<=n;i++) cin>>x[i]>>p[i]; int s=1,t=n; while (s<=t) { if (S <= x[s]) { res += x[t] - S; break; } if (S >= x[t]) { res += S - x[s]; break; } res += x[t] - x[s]; if (p[s] >= p[t]) { while (s<t && p[s]>=p[t]) { p[s]+=p[t]; t--; } } else { while (s<t && p[t]>p[s]) { p[t]+=p[s]; s++; } } } cout<<res<<endl; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define ll long long #define N 100010 ll ans = 0; int t; struct { int x; ll s; }a[N]; void ct(int l, int r, int o) { if(a[l].x >= t) { ans += a[r].x - t; return; } if(a[r].x <= t) { ans += t - a[l].x; return; } if(a[l].s >= a[r].s) { a[l].s += a[r].s; if(o != 0) ans += a[r].x - a[l].x; ct(l, r - 1, 0); } else { a[r].s += a[l].s; if(o != 1) ans += a[r].x - a[l].x; ct(l + 1, r, 1); } } int main() { int n, i; scanf("%d%d", &n, &t); for(i = 1; i <= n; i++) scanf("%d%lld", &a[i].x, &a[i].s); ct(1, n, -1); printf("%lld", ans); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; int n; ll S, x[100233], p[100233], ans; int main() { scanf("%d%lld", &n, &S); for (int i = 1; i <= n; i++) { scanf("%lld%lld", &x[i], &p[i]); } int l = 1, r = n; while (x[l] < S && x[r] > S) { ans += x[r] - x[l]; if (p[l] >= p[r]) { while (p[l] >= p[r] && x[r] > S) { p[l] += p[r--]; } } else { while (p[l] < p[r] && x[l] < S) { p[r] += p[l++]; } } } ans += max(abs(S - x[l]), abs(S - x[r])); printf("%lld\n", ans); }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include<bits/stdc++.h> using namespace std; long long n,m,s,ans=0,l,r,x[100010],p[100010]; int main() { scanf("%lld%lld",&n,&s); for (int i=1;i<=n;i++) scanf("%lld%lld",&x[i],&p[i]); l=1; r=n; while (x[l]<=s&&s<=x[r]) { ans+=x[r]-x[l]; if (p[l]<p[r]) while (p[l]<p[r]&&x[l]<=s&&s<=x[r]) p[r]+=p[l],l++; else while (p[l]>=p[r]&&x[l]<=s&&s<=x[r]) p[l]+=p[r],r--; } if (x[l]>=s) ans+=x[r]-s; else if (x[r]<=s) ans+=s-x[l]; printf("%lld",ans); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #define ll long long using namespace std; inline int read() { int x = 0, f = 1; char ch = getchar(); while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();} while(ch >= '0' && ch <= '9') {x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar();} return x * f; } const int N = 110000; int n,s,x[N]; ll p[N]; ll calc(int l,int r,int to) { if(s < x[l]) return x[r] - s; if(s > x[r]) return s - x[l]; if(p[l] >= p[r]) {p[l] += p[r]; return calc(l,r - 1,l) + (to == r ? x[r] - x[l] : 0);} else {p[r] += p[l]; return calc(l + 1,r,r) + (to == l ? x[r] - x[l] : 0);} } int main() { n = read(); s = read(); for(int i = 1;i <= n;i ++) x[i] = read(),p[i] = read(); cout << calc(1,n,p[1] < p[n] ? 1 : n); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <bits/stdc++.h> using namespace std; #define LL long long #define rep(i,x,y) for(int i=(x);i<=(y);++i) inline int read(){ char ch=getchar();int x=0,f=1; while(ch<'0'||ch>'9'){ if(ch=='-')f=-1; ch=getchar(); } while('0'<=ch&&ch<='9'){ x=x*10+ch-'0'; ch=getchar(); } return x*f; } int a[100010]; LL p[100010]; int main(){ int n=read(),S=read(); rep(i,1,n){ a[i]=read(),p[i]=read(); } int l=1,r=n; LL ans=0; while(l<=r){ if(a[l]>=S){ ans+=a[r]-S; break; } if(a[r]<=S){ ans+=S-a[l]; break; } ans+=a[r]-a[l]; if(p[l]>=p[r]){ while(p[l]>=p[r]&&l<r){ p[l]+=p[r]; --r; } } else { while(p[l]<p[r]&&l<r){ p[r]+=p[l]; ++l; } } } printf("%lld\n",ans); }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; struct node{ long long num,p; }a[100010]; int s,n; long long ans; bool cmp(node a,node b) { return a.p<b.p; } int main() { scanf("%d%d",&n,&s); for(int i=1;i<=n;i++) scanf("%d%d",&a[i].p,&a[i].num); sort(a+1,a+n+1,cmp); int L=1,R=n; int bef=-1; while(a[L].p<=s && a[R].p>=s) { int now; if(a[L].num>=a[R].num) { now=false; a[L].num+=a[R].num; } else { now=true; a[R].num+=a[L].num; } if(now!=bef) { ans+=a[R].p-a[L].p; bef=now; } if(a[L].num>=a[R].num) R--; else L++; } if(a[L].p<s) ans+=s-a[L].p; else ans+=a[R].p-s; printf("%lld",ans); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include<bits/stdc++.h> #define cs const #define pb push_back using namespace std; typedef long long ll; cs int N = 1e5 + 50; int n; ll x[N], p[N], S; ll work(int l, int r, int tr){ if(x[l]<S&&S<x[r]){ if(p[l]>=p[r]) return p[l]+=p[r], work(l,r-1,l)+(tr==r?x[r]-x[l]:0); else return p[r]+=p[l], work(l+1,r,r)+(tr==l?x[r]-x[l]:0); } return max(S-x[l],x[r]-S); } int main(){ #ifdef FSYolanda freopen("1.in","r",stdin); #endif scanf("%d%d",&n,&S); for(int i=1; i<=n; i++) scanf("%d%d",&x[i],&p[i]); cout<<work(1,n,p[1]<p[n]?1:n); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include<cstdio> int n, s; long long int x[100100]; long long int p[100100]; long long int stime[100100]; void calcans(int l, int r) { if (x[r] <= s) { for (int i = l; i <= r; i++) { stime[i] = s - x[i]; } } else if (x[l] >= s) { for (int i = l; i <= r; i++) { stime[i] = x[i] - s; } } else { if (p[l] >= p[r]) { p[l] += p[r]; calcans(l, r - 1); stime[r] += stime[l] + x[r] - x[l]; } else { p[r] += p[l]; calcans(l + 1, r); stime[l] += stime[r] + x[r] - x[l]; } } } int main() { scanf("%d%d", &n, &s); for (int i = 0; i < n; i++) { scanf("%lld%lld", &x[i], &p[i]); } calcans(0, n - 1); long long int ans = 0; for (int i = 0; i < n; i++) { if (ans < stime[i])ans = stime[i]; } printf("%lld", ans); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <bits/stdc++.h> typedef long long ll; const int N = 100054; int n, cnt = 0; int x[N], a[N]; ll p[N]; int main() { int i, j, s = 0, v = 0; ll ans = 0; scanf("%d%d", &n, x); for (i = 1; i <= n; ++i) scanf("%d%lld", x + i, p + i), (x[i] -= *x) < 0 && (s = i); *x = 0; for (i = 1, j = n; i <= s && j > s; a[cnt++] = p[i] >= p[j] ? (p[i] += p[j], j--) : (p[j] += p[i], i++)); for (a[cnt++] = (i <= s ? i : j); cnt; ans += abs(x[ a[--cnt] ] - x[v]), v = a[cnt]); printf("%lld\n", ans); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include<bits/stdc++.h> #define rint register int #define ll long long using namespace std; struct node{ ll pos,w; }A[1010010]; int n; ll S,ans; inline bool operator < (node p,node q){return p.pos<q.pos;} int main(){ cin>>n>>S; for(rint i=1;i<=n;++i) { cin>>A[i].pos>>A[i].w; } sort(A+1,A+n+1); int cur1=1,cur2=n,fl=0; while(A[cur1].pos<S&&A[cur2].pos>S){ if(A[cur1].w>=A[cur2].w) { if(fl!=1) { ans+=abs(A[cur2].pos-A[cur1].pos); } A[cur1].w+=A[cur2].w; cur2--; fl=1; } else { if(fl!=2) { ans+=abs(A[cur2].pos-A[cur1].pos); } A[cur2].w+=A[cur1].w; cur1++; fl=2; } } if(A[cur1].pos<S) ans+=abs(S-A[cur1].pos); else ans+=abs(S-A[cur2].pos); cout<<ans; return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include<bits/stdc++.h> using namespace std; using int64 = long long; int64 N, S, X[100000], P[100000]; int64 ret, last; void rec(int l, int r) { if(X[r] < S) { ret += S - X[l]; last = X[l]; } else if(X[l] > S) { ret += X[r] - S; last = X[r]; } else if(P[l] >= P[r]) { P[l] += P[r]; rec(l, r - 1); ret += abs(last - X[r]); last = X[r]; } else { P[r] += P[l]; rec(l + 1, r); ret += abs(last - X[l]); last = X[l]; } } int main() { cin >> N >> S; for(int i = 0; i < N; i++) { cin >> X[i] >> P[i]; } rec(0, N - 1); cout << ret << endl; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <cstdio> #include <cstdlib> #include <algorithm> #include <vector> #include <cstring> #include <queue> #include <functional> #include <set> #include <map> #include <cmath> #define SIZE 100005 using namespace std; typedef long long int ll; ll X[SIZE],P[SIZE]; int main() { int n; ll S; scanf("%d %lld",&n,&S); for(int i=0;i<n;i++) scanf("%lld %lld",&X[i],&P[i]); ll ret=0; int s=0,t=n-1; while(s<=t) { if(S<=X[s]) { ret+=X[t]-S; break; } if(X[t]<=S) { ret+=S-X[s]; break; } if(P[s]>=P[t]) { ret+=X[t]-X[s]; while(s<t&&P[s]>=P[t]) { P[s]+=P[t]; t--; } } else { ret+=X[t]-X[s]; while(s<t&&P[s]<P[t]) { P[t]+=P[s]; s++; } } } printf("%lld\n",ret); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=1e5+10; ll n,S; ll a[N],p[N],tag[N]; ll ans; int main(){ cin>>n>>S; for (int i=1;i<=n;i++) scanf("%lld%lld",&a[i],&p[i]); int l=1,r=n; while (a[l]<=S&&S<=a[r]){ if (p[l]>=p[r]){ if (!tag[l])ans+=a[r]-a[l],tag[l]=1; p[l]+=p[r]; r--; } else { if (!tag[r])ans+=a[r]-a[l],tag[r]=1; p[r]+=p[l]; l++; } } ans+=max(abs(S-a[l]),abs(S-a[r])); cout<<ans<<endl; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include<bits/stdc++.h> using namespace std; const int N=100100; typedef long long ll; int a[N],n,S; ll b[N]; pair<ll,int>solve(int l,int r){ if((a[l]<S)==(a[r]<S)){ if(a[l]<S)return make_pair(S-a[l],a[l]); else return make_pair(a[r]-S,a[r]); } if(b[l]>=b[r]){ b[l]+=b[r];auto s=solve(l,r-1); return make_pair(s.first+a[r]-s.second,a[r]); }else{ b[r]+=b[l];auto s=solve(l+1,r); return make_pair(s.first+s.second-a[l],a[l]); } } int main(){ ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin>>n>>S; for(int i=1;i<=n;++i)cin>>a[i]>>b[i]; cout<<solve(1,n).first<<'\n'; return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include<cstdio> #include<cstring> using namespace std; typedef long long ll; const int N=1e5+100; int x[N],Ans[N];ll p[N]; int abs(int x) {return x<0?-x:x;} int main() { int n,S;scanf("%d%d",&n,&S); for(int i=1;i<=n;i++) scanf("%d%lld",&x[i],&p[i]); int id1=1,id2=n; while(id1<id2){ if(x[id2]<=S){ while(id1<id2) Ans[++Ans[0]]=id1,id1++; break; } if(x[id1]>=S){ while(id1<id2) Ans[++Ans[0]]=id2,id2--; break; } if(p[id1]>=p[id2]) Ans[++Ans[0]]=id2,p[id1]+=p[id2],id2--; else Ans[++Ans[0]]=id1,p[id2]+=p[id1],id1++; } Ans[++Ans[0]]=id1; ll ans=0; for(int i=1;i<n;i++) ans+=abs(x[Ans[i]]-x[Ans[i+1]]); ans+=abs(x[Ans[Ans[0]]]-S); printf("%lld\n",ans); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll,ll> pll; int n; ll x, ans; vector<pll> vec; pll f(int l, int r) { if (vec[l].second>=x) return pll(vec[r].second-x,vec[r].second); if (vec[r].second<=x) return pll(x-vec[l].second,vec[l].second); if (vec[l].first<vec[r].first) { vec[r].first += vec[l].first; pll tmp = f(l+1,r); return pll(tmp.first+abs(tmp.second-vec[l].second),vec[l].second); } vec[l].first += vec[r].first; pll tmp = f(l,r-1); return pll(tmp.first+abs(tmp.second-vec[r].second),vec[r].second); } int main() { int i; scanf("%d%lld",&n,&x); for (i=0;i<n;i++){ ll a, b; scanf("%lld%lld",&a,&b); vec.push_back(pll(b,a)); } printf("%lld\n",f(0,(int)vec.size()-1).first); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <cstdio> long long x[100001],p[100001],ans,n,s; int main(){ scanf("%d%d",&n,&s); for(int i=1;i<=n;i++) scanf("%d%d",&x[i],&p[i]); int i=1,j=n; while(i<=j){ if(s<=x[i]){ printf("%lld",x[j]-s+ans); return 0; }else if(s>=x[j]){ printf("%lld",ans+s-x[i]); return 0; } if(p[i]>=p[j]){ ans+=x[j]-x[i]; while(i<j && p[i]>=p[j])p[i]+=p[j--]; }else{ ans+=x[j]-x[i]; while(i<j && p[i]<p[j])p[j]+=p[i++]; } } }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include<algorithm> #include<iostream> #include<cstring> #include<cstdio> using namespace std; typedef long long ll; const int maxn = 100000 + 10; int n; ll a[maxn], w[maxn], S; int main() { // freopen("data.in","r",stdin); scanf("%d%lld", &n, &S); for(int i = 1;i <= n;i ++) scanf("%lld%lld", &a[i], &w[i]); int head = 1, tail = n; ll Ans = 0; int lst = -1; for(int i = 1;i <= n;i ++) { if(a[tail] < S || (a[head] < S && w[head] < w[tail])) { w[tail] += w[head]; if(lst != -1) Ans += (ll)abs(lst - a[head]); lst = a[head]; head ++; } else { w[head] += w[tail]; if(lst != -1) Ans += (ll)abs(lst - a[tail]); lst = a[tail]; tail --; } } Ans += (ll)abs(lst - S); // if(S > a[head] && lst != 1) Ans += S - a[head]; // if(S < a[head] && lst != 0) Ans += a[head] - S; printf("%lld\n", Ans); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <iostream> #include <cstdio> #include <cstring> using namespace std; typedef long long ll; const int maxn = 1e5 + 10 , oo = 1e9 + 7; int n , s; int x[maxn]; ll p[maxn]; int main(){ ios::sync_with_stdio(0); cin >> n >> s; for(int i = 1 ; i <= n ; ++ i) cin >> x[i] >> p[i]; x[n + 1] = oo; int mid; for(int i = 0 ; i <= n ; ++ i) if(x[i] < s && x[i + 1] > s){ mid = i; break; } ll ans = 0; int l = 1 , r = n; int last = -1; while(l <= mid && r > mid){ if(p[l] < p[r]){ if(last != 0){ ans += x[r] - x[l]; last = 0; } p[r] += p[l]; l ++; } else{ if(last != 1){ ans += x[r] - x[l]; last = 1; } p[l] += p[r]; r --; } } if(l <= mid) ans += s - x[l]; if(r > mid) ans += x[r] - s; cout << ans << endl; return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <cstdio> #include <cstring> #include <algorithm> #include <cctype> typedef long long LL; const int N = 100100; inline int getint() { int r = 0,s = 0;char c = getchar();for(;!isdigit(c);c = getchar()) s |= c == '-'; for(;isdigit(c);c = getchar()) r = (((r << 2) + r) << 1) + (c ^ '0');return s ? -r : r; } int n,s,x[N]; LL p[N],ans = 0; int main() { n = getint(),s = getint(); for(int i = 1;i <= n;i++) x[i] = getint(),p[i] = getint(); for(int l = 1,r = n,d = -1;;) { if(x[l] >= s){printf("%lld\n",ans + x[r] - s);break;} if(x[r] <= s){printf("%lld\n",ans + s - x[l]);break;} if(p[l] >= p[r]) { if(d != 0) ans += x[r] - x[l]; p[l] += p[r--]; d = 0; } else { if(d != 1) ans += x[r] - x[l]; p[r] += p[l++]; d = 1; } } return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <bits/stdc++.h> using namespace std; #define MAXN 100000 #define ll long long #define rint register int #define gc() getchar() inline int read(int r=0,int s=0,int c=gc()){for(;c<48||c>57;s=c,c=gc());for(;c>=48&&c<=57;(r*=10)+=c-48,c=gc());return s^'-'?r:-r;} int X[MAXN+5], L, R, S; ll P[MAXN+5], Ans; bool f; int main() { R = read(), S = read(); for(rint i = L = 1; i <= R; X[i] = read(), P[i] = read(), i++); for(f = P[L]<P[R]; L <= R; Ans += ((P[L]>=P[R])^f)*(X[R]-X[L]), (f=P[L]>=P[R])?P[L]+=P[R],P[R--]=0:(P[R]+=P[L],P[L++]=0)) if(X[L] > S){Ans += X[R]-S; break;} else if(X[R] < S){Ans += S-X[L]; break;} printf("%lld\n",Ans); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <iostream> #define ll unsigned long long using namespace std; const int maxn=1e5+7; ll x[maxn],p[maxn]; int n;ll s; struct node { ll time;int dir; }; node find(int l,int r) { node res; if(l==r) { if(x[l]>s) {res.time=x[l]-s;res.dir=1;} if(x[l]<s) {res.time=s-x[l];res.dir=-1;} } else if(s<x[l]) { res.dir=1; res.time=x[r]-s; } else if(s>x[r]) { res.dir=-1; res.time=s-x[l]; } else if(p[l]<p[r]) { p[r]+=p[l]; // node k=find(l+1,r); res = find(l+1, r); if(res.dir==1) res.time+=(x[r]-x[l]); else res.time+=x[l+1]-x[l]; res.dir=-1; } else if(p[l]>=p[r]) { p[l]+=p[r]; res=find(l,r-1); if(res.dir==-1) res.time+=(x[r]-x[l]); else res.time+=x[r]-x[r-1]; res.dir=1; } return res; } int main() { cin>>n>>s; for(int i=1;i<=n;i++) cin>>x[i]>>p[i]; cout<<find(1,n).time<<endl; return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include<cstdio> #define N 100001 int a[N],i,j,k,m,n;long long b[N],x; int main() { for(scanf("%d%d",&n,&m),i=1;i<=n;i++)scanf("%d%lld",a+i,b+i); for(k=0;k<n&&a[k+1]<=m;k++); for(i=1,j=n;i<=k&&k<j;) if(b[i]<b[j])for(x+=a[j]-a[i];i<=k&&b[i]<b[j];b[j]+=b[i++]); else for(x+=a[j]-a[i];k<j&&b[j]<=b[i];b[i]+=b[j--]); return 0*printf("%lld\n",x+(i<=k?m-a[i]:a[j]-m)); }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include<cstdio> #include<cstring> #include<cstdlib> using namespace std; long long x[110000],p[110000]; int main() { long long n,s;scanf("%lld%lld",&n,&s); for(int i=1;i<=n;i++)scanf("%lld %lld",&x[i],&p[i]); long long ss=0,l=1,r=n,ed=-1; while(l<=r) { if(x[r]<s){ss+=s-x[l];break;} if(s<x[l]){ss+=x[r]-s;break;} if(p[l]<p[r]) { p[r]+=p[l]; if(ed!=-1)ss+=abs(x[r]-ed); ed=x[r]; l++; } else { p[l]+=p[r]; if(ed!=-1)ss+=abs(x[l]-ed); ed=x[l]; r--; } } printf("%lld\n",ss+(ed!=-1?x[n]-x[1]:0)); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <bits/stdc++.h> #define maxn 100086 using namespace std; int n, s; int x[maxn]; vector<int> v; long long p[maxn], ans; int main(){ scanf("%d%d", &n, &s); for(int i = 1;i <= n;i++) scanf("%d%lld", &x[i], &p[i]); //if(x[1] >= s) return printf("%d", x[n] - s), 0; //if(x[n] <= s) return printf("%d", s - x[1]), 0; int l = 1, r = n; while(l < r){ //特判全在一侧的情况 if(x[l] >= s){ l = r; break; } if(x[r] <= s) break; if(p[l] >= p[r]) p[l] += p[r], v.push_back(r--); else p[r] += p[l], v.push_back(l++); } v.push_back(l); while(!v.empty()) ans += abs(s - x[v.back()]), s = x[v.back()], v.pop_back(); printf("%lld", ans); }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <bits/stdc++.h> #define LL long long using namespace std; inline LL read(){ static LL x,f; x = 0,f = 1; static char c; c = getchar(); while (!isdigit(c)){ if (c == '-') f = -1; c = getchar(); } while (isdigit(c)){ x = x * 10 + c - '0',c = getchar(); } return x * f; } const int N = 100005; LL n,s,x[N],a[N],tot; inline LL work(int l,int r){ if (s <= x[l]){ tot += x[r] - s; return x[r]; } if (s >= x[r]){ tot += s - x[l]; return x[l]; } if (a[l] < a[r]){ a[r] += a[l],tot += work(l+1,r) - x[l]; return x[l]; } a[l] += a[r],tot += x[r] - work(l,r-1); return x[r]; } int main(){ int i; n = read(),s = read(); for (i = 1; i <= n; ++i) x[i] = read(),a[i] = read(); int l = 1,r = n; work(1,n); cout << tot << '\n'; return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <bits/stdc++.h> #define gc getchar() using namespace std; typedef long long ll; const int N=100009; int n; ll X[N],P[N],S; int read() { int x=1; char ch; while (ch=gc,ch<'0'||ch>'9') if (ch=='-') x=-1; int s=ch-'0'; while (ch=gc,ch>='0'&&ch<='9') s=s*10+ch-'0'; return s*x; } int main() { n=read(),S=read(); for (int i=1;i<=n;i++) X[i]=read(),P[i]=read(); int l=1,r=n,last=0; ll ret=0; for (;;) { if (X[l]>=S) { ret+=X[r]-S; break; } if (X[r]<=S) { ret+=S-X[l]; break; } if (P[l]>=P[r]) { if (last!=1) { last=1; ret+=X[r]-X[l]; } P[l]+=P[r]; r--; } else { if (last!=2) { last=2; ret+=X[r]-X[l]; } P[r]+=P[l]; l++; } } printf("%lld\n",ret); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include<bits/stdc++.h> #define rep(i,x,y) for (int i=(x); i<=(y); i++) #define ll long long #define ld long double #define inf 1000000000 #define N 100005 int n; ll s,a[N],b[N],ans; int main(){ scanf("%d%lld",&n,&s); rep (i,1,n) scanf("%lld%lld",&a[i],&b[i]); int l=1,r=n; ans=0; while (l<=r){ if (s<=a[l]){ ans+=a[r]-s; break; } if (s>=a[r]){ ans+=s-a[l]; break; } if (b[l]>=b[r]){ ans+=a[r]-a[l]; while (l<r && b[l]>=b[r]){ b[l]+=b[r]; r--; } } else{ ans+=a[r]-a[l]; while (l<r && b[l]<b[r]){ b[r]+=b[l]; l++; } } } printf("%lld\n",ans); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #define N 100005 using namespace std; int n, s, x[N]; long long p[N]; int main() { #ifdef whyqx freopen("work.in", "r", stdin); freopen("work.out", "w", stdout); #endif cin >> n >> s; if (n == 0) return puts("0"), 0; for (int i = 1; i <= n; ++i) scanf("%d%lld", x + i, p + i); long long ans = 0; int l = 1, r = n; int t = p[1] >= p[n] ? x[n] : x[1]; while (true) { if (s < x[l] || s > x[r]) break; if (p[l] >= p[r]) p[l] += p[r], --r, ans += abs(x[l] - t), t = x[l]; else p[r] += p[l], ++l, ans += abs(x[r] - t), t = x[r]; } if (x[r] < s) ans += s - min(x[l], t); else ans += max(x[r], t) - s; cout << ans << endl; return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <cstdio> long long p[100001],x[100001],n,s;; long long ans; int main(){ scanf("%d%d",&n,&s); for (int i=1;i<=n;i++) scanf("%d%d",&x[i],&p[i]); int i=1,j=n; while (i<=j){ if (s<=x[i]){ printf("%lld",x[j]-s+ans); return 0; } else if (s>=x[j]){ printf("%lld",ans+s-x[i]); return 0; } if (p[i]>=p[j]){ ans+=x[j]-x[i]; while (i<j && p[i]>=p[j]) p[i]+=p[j--]; } else{ ans+=x[j]-x[i]; while (i<j && p[i]<p[j]) p[j]+=p[i++]; } } }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include<bits/stdc++.h> using namespace std; const int N=300005; int n,s,x[N]; long long z,p[N]; int main() { scanf("%d%d",&n,&s); for (int i=1;i<=n;i++)scanf("%d%d",&x[i],&p[i]); for (int l=1,r=n;l<=r;) { if (x[l]>=s) { z+=x[r]-s; break; } if (x[r]<=s) { z+=s-x[l]; break; } z+=x[r]-x[l]; if (p[l]>=p[r]) {while (l<r&&p[l]>=p[r])p[l]+=p[r--];} else {while (l<r&&p[l]<p[r])p[r]+=p[l++];} } printf("%lld",z); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <bits/stdc++.h> using namespace std; int N; long long S; #define MAX_N 100000 long long X[MAX_N], P[MAX_N]; int I; int L, R; deque<long long> W; long long D; int main() { cin >> N >> S; for(int i = 0; i < N; ++i) { cin >> X[i] >> P[i]; if(X[i] < S) I = i; } L = 0, R = N - 1; while(!(R <= I || I < L)) { if(P[L] >= P[R]) { P[L] += P[R]; W.push_front(X[R]); --R; } else { P[R] += P[L]; W.push_front(X[L]); ++L; } } if(R <= I) W.push_front(X[L]); else W.push_front(X[R]); long long p = S; while((int)W.size() > 0) { D += abs(p - W.front()); p = W.front(); W.pop_front(); } cout << D << endl; return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <bits/stdc++.h> using namespace std; typedef long long LL; LL n,s,x[100009],p[100009],ans[100009]; void call(int l,int r) { if(l==r) { ans[l]=fabs(x[l]); return; } if(x[l]>0) { call(l,r-1); ans[r]=ans[r-1]+x[r]-x[r-1]; return; } if(x[r]<0) { call(l+1,r); ans[l]=ans[l+1]+x[l+1]-x[l]; return; } if(p[l]>=p[r]) { p[l]+=p[r]; call(l,r-1); ans[r]=ans[l]+x[r]-x[l]; return; } p[r]+=p[l]; call(l+1,r); ans[l]=ans[r]+x[r]-x[l]; return; } int main() { scanf("%lld %lld",&n,&s); for(int i=1;i<=n;i++) { scanf("%lld %lld",&x[i],&p[i]); x[i]-=s; } call(1,n); LL foo=max(ans[1],ans[n]); cout<<foo<<endl; return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <bits/stdc++.h> #define F first #define S second #define pii pair<int, int> #define pb push_back using namespace std; typedef long long ll; typedef long double ld; const int N = 1e5 + 10; ll x[N], p[N]; ll ans; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, S; cin >> n >> S; int pos = -1; for (int i=0; i<n; i++) { cin >> x[i] >> p[i]; if(x[i] < S) pos = i; } int st = 0, en = n - 1, dir = -1; while(true) { if(st > pos){ ans += x[en] - S; break ;} else if(en <= pos){ ans += S - x[st]; break; } if(p[st] >= p[en]) { if(dir != 0) ans += x[en] - x[st]; dir = 0; p[st] += p[en]; en --; }else { if(dir != 1) ans += x[en] - x[st]; dir = 1; p[en] += p[st]; st ++; } } cout << ans << endl; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <bits/stdc++.h> using namespace std; typedef bool boolean; #define ll long long const int N = 1e5 + 5; int n, S; ll X[N], P[N]; int main() { scanf("%d%d", &n, &S); for (int i = 1; i <= n; i++) { scanf("%lld%lld", X + i, P + i); } int L = 1, R = n, d = 0; ll ans = 0; while (true) { if (X[R] <= S) { ans += S - X[L]; break; } else if (X[L] >= S) { ans += X[R] - S; break; } if (P[L] >= P[R]) { (d != -1) && (ans += X[R] - X[L]); P[L] += P[R], d = -1, R--; } else { (d != 1) && (ans += X[R] - X[L]); P[R] += P[L], d = 1, L++; } } printf("%lld\n", ans); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include<iostream> #include<cstdio> using namespace std; int n,S,X[202020],st[202020],top; long long P[202020]; void solve(int l,int r) { if(S<=X[l]) return (void)(st[++top]=r); if(S>=X[r]) return (void)(st[++top]=l); if(P[l]<P[r]) return st[++top]=l,P[r]+=P[l],solve(l+1,r); else return st[++top]=r,P[l]+=P[r],solve(l,r-1); } int aabs(int x){ return x<0?-x:x;} int main() { scanf("%d%d",&n,&S); for(int i=1;i<=n;i++) scanf("%d%lld",&X[i],&P[i]); solve(1,n); long long ans=0; while(top) ans+=aabs(S-X[st[top]]),S=X[st[top--]]; printf("%lld\n",ans); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include<iostream> #include<cstdio> #include<algorithm> #define FOR(i,a,b) for(int i=a;i<=b;i++) using namespace std; const int N=200200; int n,S,x,w,d,D,A,B; int a[N],b[N]; long long ans,f[N],g[N]; int main(){ //freopen("1.in","r",stdin); scanf("%d%d",&n,&S); FOR(i,1,n){ scanf("%d%d",&x,&w); x<S?(a[++A]=x,f[A]=w):(b[++B]=x,g[B]=w); } reverse(a+1,a+A+1); reverse(f+1,f+A+1); while(A && B){ d=f[A]>=g[B]?-1:1; if(d!=D) ans+=b[B]-a[A],D=d; ~d?g[B]+=f[A--]:f[A]+=g[B--]; } ans+=A?S-a[A]:b[B]-S; cout<<ans<<'\n'; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<vector> #include<map> #include<set> #include<string> #include<queue> #include<stack> using namespace std; #define MOD 1000000007 #define INF (1<<29) #define LINF (1LL<<60) #define EPS (1e-10) typedef long long Int; typedef pair<Int, Int> P; Int n, s, res; Int x[108000], p[108000]; int main(){ cin >> n >> s; for(int i = 0;i < n;i++){ cin >> x[i] >> p[i]; } int l = 0, r = n-1; vector<Int> pos; while(x[l] < s && s < x[r]){ if(p[l] >= p[r]){ pos.push_back(x[r]); p[l] += p[r]; r--; } else{ pos.push_back(x[l]); p[r] += p[l]; l++; } } if(x[r] < s)pos.push_back(x[l]); else pos.push_back(x[r]); for(int i = pos.size() - 1;i >= 0;i--){ res += abs(s - pos[i]); s = pos[i]; } cout << res << endl; return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include<bits/stdc++.h> using namespace std; const int N=1e5+5; int n,S,x[N],i,L=1,R,lst,v; long long ans,p[N];bool fl; int main(){ scanf("%d%d",&n,&S); for(i=1;i<=n;++i)scanf("%d%lld",x+i,p+i);R=n; for(;;){ if(S>x[R]){ans+=S-x[L];break;} if(S<x[L]){ans+=x[R]-S;break;} fl=0;v=x[R]-x[L];if(p[L]>=p[R])fl=lst!=L,p[lst=L]+=p[R--];else fl=lst!=R,p[lst=R]+=p[L++]; if(fl)ans+=v; } printf("%lld\n",ans); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include<iostream> #include<iomanip> #include<cstring> #include<cmath> #include<cstdio> #include<queue> #include<algorithm> using namespace std; int n,s; struct que { int x; long long p; }a[100005]; long long calc(int l,int r,int last) { if(a[l].x>=s) return a[r].x-s; if(a[r].x<=s) return s-a[l].x; if(a[l].p>=a[r].p){a[l].p+=a[r].p;return calc(l,r-1,l)+((last==r)?a[r].x-a[l].x:0);} else {a[r].p+=a[l].p;return calc(l+1,r,r)+((last==l)?a[r].x-a[l].x:0);} } int main() { scanf("%d%d",&n,&s); for(int i=1;i<=n;i++) scanf("%d%lld",&a[i].x,&a[i].p); printf("%lld\n",calc(1,n,a[1].p<a[n].p?1:n)); }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll a[100005],b[100005]; int q[100005]; int main() { int n,s; scanf("%d%d",&n,&s); int id=0; for(int i=1;i<=n;i++) { scanf("%lld%lld",&a[i],&b[i]); if (a[i]<s) id=i; } int cnt=0,l=1,r=n; while ((l<=id)^(r<=id)) if (b[l]>=b[r]) { q[++cnt]=a[r]; b[l]+=b[r]; r--; } else { q[++cnt]=a[l]; b[r]+=b[l]; l++; } if (r<=id) { for(int i=l;i<=r;i++) q[++cnt]=a[i]; } else { for(int i=r;i>=l;i--) q[++cnt]=a[i]; } q[cnt+1]=s; ll ans=0; for(int i=1;i<=cnt;i++) ans+=abs(q[i]-q[i+1]); printf("%lld\n",ans); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include<bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0;i<(n);i++) #define REP(i,n) for(int i=1;i<=(n);i++) #define mp make_pair #define pb push_back #define fst first #define snd second typedef long long ll; typedef pair<int,int> pii; typedef pair<long long,long long> pll; const int maxn=100005; int n,s,pos; int x[maxn]; ll p[maxn],ans; pll solve(int l,int r){ if(r<=pos)return mp(s-x[l],s-x[r]); if(l>pos)return mp(x[l]-s,x[r]-s); if(p[l]>=p[r]){ p[l]+=p[r]; pll res=solve(l,r-1); return mp(res.fst,res.fst+x[r]-x[l]); } else{ p[r]+=p[l]; pll res=solve(l+1,r); return mp(res.snd+x[r]-x[l],res.snd); } } int main(){ scanf("%d%d",&n,&s); REP(i,n)scanf("%d%lld",&x[i],&p[i]); while(pos+1<=n&&x[pos+1]<s)pos++; pll ans=solve(1,n); printf("%lld\n",max(ans.fst,ans.snd)); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
from collections import Counter, defaultdict import math from decimal import Decimal, ROUND_HALF_UP, ROUND_CEILING from functools import lru_cache, reduce from itertools import combinations_with_replacement, product, combinations def read_int(): return int(input()) def read_int_n(): return list(map(int, input().split())) def read_str(): return input() def read_str_n(): return list(map(str, input().split())) def mt(f): import time import sys def wrap(*args, **kwargs): s = time.time() ret = f(*args, **kwargs) e = time.time() print(e - s, 'sec', file=sys.stderr) return ret return wrap @mt def slv(S, X, P): X_ = [[P[i], [x]] for i, x in enumerate(X)] while len(X_) != 1: if X_[0][0] >= X_[-1][0]: di = -1 li = 0 else: di = 0 li = -1 X_[li][0] += X_[di][0] X_[li][1] += X_[di][1] X_.pop(di) t = 0 pos = S p0 = S p1 = S for o in X_[0][1]: if p0 < o < p1: continue t += abs(pos - o) if o < pos: p0 = o else: p1 = o pos = o return t def main(): N, S = read_int_n() X = [] P = [] for _ in range(N): x, p = read_int_n() X.append(x) P.append(p) print(slv(S, X, P)) if __name__ == '__main__': main()
PYTHON3