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
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=100005; int a[Maxn]; ll p[Maxn]; vector<int>pos; void solve(int l,int r,int s){ if(s<=a[l])return pos.push_back(a[r]); if(s>=a[r])return pos.push_back(a[l]); if(p[l]>=p[r]){ p[l]+=p[r]; solve(l,r-1,s); pos.push_back(a[r]); }else{ p[r]+=p[l]; solve(l+1,r,s); pos.push_back(a[l]); } } int main(){ int n,s;scanf("%d%d",&n,&s); for(int i=1;i<=n;i++)scanf("%d%d",&a[i],&p[i]); solve(1,n,s); ll ans=0; for(int x:pos)ans+=abs(s-x),s=x; 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> using namespace std; const int N = 100010; typedef long long LL; LL num[N]; int pos[N]; int main() { int n, s; scanf("%d%d", &n, &s); for (int i = 1; i <= n; i++) scanf("%d%lld", &pos[i], &num[i]); int l = 1, r = n, ls = 0; LL res = 0; for (int i = 1; i <= n; i++) { if (pos[l] > s) { res += pos[r] - s; break; } if (pos[r] < s) { res += s - pos[l]; break; } int nw; if (num[l] >= num[r]) nw = 1, num[l] += num[r]; else nw = -1, num[r] += num[l]; if (nw != ls) res += pos[r] - pos[l]; ls = nw; if (nw == -1) l++; else r--; } printf("%lld\n", res); 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 N 110000 typedef long long ll; ll n,s,a[N],p[N],ans; ll l,r; bool check(){ return a[l]<=s&&s<=a[r]; } int main(){ scanf("%lld%lld",&n,&s); for(ll i=1;i<=n;i++){ scanf("%lld%lld",&a[i],&p[i]); } l=1; r=n; while(a[l]<=s&&s<=a[r]){ ans+=a[r]-a[l]; if(p[l]>=p[r])while(p[l]>=p[r]&&a[l]<=s&&s<=a[r])p[l]+=p[r],r--; else while(p[l]<p[r]&&a[l]<=s&&s<=a[r])p[r]+=p[l],l++; } if(a[l]>=s)ans+=a[r]-s; else if(a[r]<=s)ans+=s-a[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 <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 1e5 + 10; int n, s, si; ll ans; int x[maxn]; ll p[maxn]; int main() { scanf("%d%d", &n, &s); for(int i = 1; i <= n; ++i) scanf("%d%lld", &x[i], &p[i]); si = lower_bound(x + 1, x + n + 1, s) - x; int dir = -1; int l = 1, r = n; while(l < si && si <= r) { if(p[l] >= p[r]) { if(dir != 1) ans += x[r] - x[l]; p[l] += p[r]; dir = 1; --r; } else { if(dir != 0) ans += x[r] - x[l]; p[r] += p[l]; dir = 0; ++l; } } if(l >= si) ans += x[r] - s; else ans += s - x[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 <cstdio> #include <iostream> using namespace std; int n,s,a,b; long long x[120000],p[120000],ans; int main(){ scanf("%d%d",&n,&s); for (int i=1; i<=n; i++) scanf("%d%d",&x[i],&p[i]); a=1,b=n; while (a<=b){ if (s<=x[a]) { ans+=x[b]-s; break; } if (s>=x[b]) { ans+=s-x[a]; break; } if (p[a]>=p[b]){ ans+=x[b]-x[a]; while (a<b && p[a]>=p[b]) { p[a]+=p[b]; b--; } } else{ ans+=x[b]-x[a]; while (a<b && p[a]<p[b]) { p[b]+=p[a]; a++; } } } 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; #define Re return #define In inline #define Rg #define Op operator #define St static #define inc(l, i, r) for(Rg int i=l; i<r; ++i) #define dec(l, i, r) for(Rg int i=r; i>l; --i) #define fst first #define snd second typedef long long ll; int main() { St int n, s; St pair<int, ll> a[1<<17]; scanf("%d%d", &n, &s); inc(0, i, n) scanf("%d%lld", &a[i].fst, &a[i].snd); sort(a, a+n); Rg int m=lower_bound(a, a+n, make_pair(s, 0ll))-a; Rg int l=0, r=n-1; St ll f[1<<17]; for(;l<m && m<=r;) { if(a[l].snd<a[r].snd) f[r]=max(f[r], f[l]+a[r].fst-a[l].fst), a[r].snd+=a[l++].snd; else f[l]=max(f[l], f[r]+a[r].fst-a[l].fst), a[l].snd+=a[r--].snd; } printf("%lld\n", l<m? f[l]+s-a[l].fst: f[r]+a[r].fst-s); Re 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.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.PriorityQueue; class Main{ static class Node{ long dis, num; Node(long d, long n){dis=d;num=n;} } static void solve(){ int n = ni(); long S = nl(); PriorityQueue<Node> left = new PriorityQueue<>((a,b) -> b.dis-a.dis<0 ? -1:1); PriorityQueue<Node> right= new PriorityQueue<>((a,b) -> b.dis-a.dis<0 ? -1:1); for(int i=0;i<n;++i){ long x = nl(), p = nl(); if(x<S)left.add(new Node(S-x, p)); else right.add(new Node(x-S, p)); } long sum = 0; if(left.isEmpty() || right.isEmpty()){ if(!left.isEmpty())out.println(left.peek().dis); else out.println(right.peek().dis); return; } long ans = 0; if(left.peek().num >= right.peek().num)ans=right.peek().dis; else ans = left.peek().dis; while(true){ if(left.peek().num>=right.peek().num){ while(!right.isEmpty() && right.peek().num <= left.peek().num)left.peek().num += right.poll().num; ans += left.peek().dis*2; if(right.isEmpty())break; }else{ while(!left.isEmpty() && left.peek().num < right.peek().num)right.peek().num += left.poll().num; ans += right.peek().dis*2; if(left.isEmpty())break; } } out.println(ans); } public static void main(String[] args){ solve(); out.flush(); } private static InputStream in = System.in; private static PrintWriter out = new PrintWriter(System.out); private static final byte[] buffer = new byte[1<<15]; private static int ptr = 0; private static int buflen = 0; private static boolean hasNextByte(){ if(ptr<buflen)return true; ptr = 0; try{ buflen = in.read(buffer); } catch (IOException e){ e.printStackTrace(); } return buflen>0; } private static int readByte(){ if(hasNextByte()) return buffer[ptr++]; else return -1;} private static boolean isSpaceChar(int c){ return !(33<=c && c<=126);} private static int skip(){int res; while((res=readByte())!=-1 && isSpaceChar(res)); return res;} private static double nd(){ return Double.parseDouble(ns()); } private static char nc(){ return (char)skip(); } private static String ns(){ StringBuilder sb = new StringBuilder(); for(int b=skip();!isSpaceChar(b);b=readByte())sb.append((char)b); return sb.toString(); } private static int[] nia(int n){ int[] res = new int[n]; for(int i=0;i<n;++i)res[i]=ni(); return res; } private static long[] nla(int n){ long[] res = new long[n]; for(int i=0;i<n;++i)res[i]=nl(); return res; } private static int ni(){ int res=0,b; boolean minus=false; while((b=readByte())!=-1 && !((b>='0'&&b<='9') || b=='-')); if(b=='-'){ minus=true; b=readByte(); } for(;'0'<=b&&b<='9';b=readByte())res=res*10+(b-'0'); return minus ? -res:res; } private static long nl(){ long res=0,b; boolean minus=false; while((b=readByte())!=-1 && !((b>='0'&&b<='9') || b=='-')); if(b=='-'){ minus=true; b=readByte(); } for(;'0'<=b&&b<='9';b=readByte())res=res*10+(b-'0'); return minus ? -res:res; } }
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 <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]; else if (pos[l] > s) ans += pos[r] - s; else { 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; typedef long long ll; #define ri register int const long double PI=3.141592653589793238462643383279; const int M=998244353,N=200005; const long long inf=1000000000000000000ll; int n,i,s,l,r,x[100005],o[100005],k; long long a[100005],ans; int main() { scanf("%d %d",&n,&s); for(i=1;i<=n;++i) scanf("%d %d",&x[i],&a[i]); l=1,r=n; while(x[l]<s&&x[r]>s) { if(a[l]>=a[r]) { a[l]+=a[r]; o[++k]=r; --r; } else { a[r]+=a[l]; o[++k]=l; ++l; } } while(x[l]<s&&l<=n) o[++k]=l++; while(x[r]>s&&r>=1) o[++k]=r--; for(i=2;i<=k;++i) ans+=abs(x[o[i]]-x[o[i-1]]); ans+=abs(x[o[k]]-s); 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<bits/stdc++.h> #define ll long long #define N 100005 using namespace std; ll x[N],p[N],ans[N]; struct edge{ int to,next; ll v; }e[N]; int n,s,tot; int head[N]; void add(int x,int y,ll v){ e[++tot]=(edge){y,head[x],v}; head[x]=tot; } void dfs(int x){ for (int i=head[x];i;i=e[i].next) ans[e[i].to]=ans[x]+e[i].v,dfs(e[i].to); } 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]) p[l]+=p[r],add(l,r,x[r]-x[l]),--r; else p[r]+=p[l],add(r,l,x[r]-x[l]),++l; ans[l]=abs(x[l]-s); dfs(l); printf("%lld\n",max(ans[1],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<bits/stdc++.h> #define re register int x[100100]; long long p[101000]; int main() { re int n,s,i1,i2,nw; re long long ans=0; scanf("%d%d",&n,&s); i1=1;i2=n; for(re int i=1;i<=n;i++)scanf("%d%lld",&x[i],&p[i]); if(x[n]<=s){printf("%d\n",s-x[1]);return 0;} else if(x[1]>=s){printf("%d\n",x[n]-s);return 0;} if(p[1]>=p[n])nw=1,p[1]+=p[n],i2--;else nw=2,p[n]+=p[1],i1++; ans=x[n]-x[1]; while(1) { if(x[i1]>=s){printf("%lld\n",ans+x[i2]-s);return 0;} if(x[i2]<=s){printf("%lld\n",ans+s-x[i1]);return 0;} if(p[i1]>=p[i2]) { if(nw!=1)nw=1,ans+=x[i2]-x[i1]; p[i1]+=p[i2];i2--; } else { if(nw!=2)nw=2,ans+=x[i2]-x[i1]; p[i2]+=p[i1];i1++; } } }
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 MN=100000; int main() { int n; long long S; scanf("%d%lld",&n,&S); static long long X[MN],P[MN]; for(int i=0;i<n;i++){ scanf("%lld%lld",X+i,P+i); } int s=lower_bound(X,X+n,S)-X; long long ans=0ll; for(int i=0,j=n-1;;){ //printf("%d %d\n",i,j); if(i==s){ ans+=X[j]-S; break; } if(j==s-1){ ans+=S-X[i]; break; } if(P[i]>=P[j]){ ans+=X[j]-X[i]; while(j>=s&&P[i]>=P[j]){ P[i]+=P[j]; j--; } } else{ ans+=X[j]-X[i]; while(i<s&&P[i]<P[j]){ P[j]+=P[i]; i++; } } } 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> #include<cstring> #include<cstring> #include<algorithm> #include<vector> #define maxn 100005 #define ll long long using namespace std; int n,i,j,k; ll ans,c[maxn],x[maxn],now; ll dg(int l,int r){ ll s; if (x[l]>=now) {s=x[r]-now,now=x[r];return s;} if (x[r]<=now) {s=now-x[l],now=x[l];return s;} if (c[l]>=c[r]) c[l]+=c[r],s=dg(l,r-1),s+=x[r]-now,now=x[r]; else c[r]+=c[l],s=dg(l+1,r),s+=now-x[l],now=x[l]; return s; } int main(){ scanf("%d%lld",&n,&now); for(i=1;i<=n;i++) scanf("%lld%lld",&x[i],&c[i]); printf("%lld",dg(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> #define L long long using namespace std; int n,m,a[100010]; L b[100010],p; int main() { //freopen(".in","r",stdin); //freopen(".out","w",stdout); int i,j,k,l=0; scanf("%d%d",&n,&m); for(i=1;i<=n;i++) scanf("%d%lld",&a[i],&b[i]); for(i=1,j=n;i<=j;) if((b[i]>=b[j] || a[i]>m) && a[j]>m) { b[i]+=b[j]; if(l) p+=abs(k-a[j]); k=a[j]; l=1; j--; } else { b[j]+=b[i]; if(l) p+=abs(k-a[i]); k=a[i]; l=1; i++; } p+=abs(k-m); printf("%lld\n",p); 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> typedef long long ll; int n,s; ll Dis[100005]; struct Homeee{int x;ll c;}a[100005]; int main() { scanf("%d%d",&n,&s); for(int i=1;i<=n;++i)scanf("%d%lld",&a[i].x,&a[i].c); std::sort(a+1,a+n+1,[](const Homeee &x,const Homeee &y){return x.x<y.x;}); int l=1,r=n,t=2; while(a[l].x<s&&a[r].x>s) if(a[l].c<a[r].c)a[r].c+=a[l].c,Dis[r]+=Dis[l]+(t?a[r].x-a[l].x:0),t=0,++l; else a[l].c+=a[r].c,Dis[l]+=Dis[r]+(t!=1?a[r].x-a[l].x:0),t=1,--r; printf("%lld\n",a[l].x>s?Dis[r]+a[r].x-s:Dis[l]+s-a[l].x); 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 N 1000005 #define ll long long using namespace std; inline ll read(){ ll ans=0; char ch=getchar(); while(!isdigit(ch))ch=getchar(); while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar(); return ans; } ll n,s,ans,x[N],p[N]; int main(){ n=read(),s=read(); for(ll i=1;i<=n;++i)x[i]=read(),p[i]=read(); ll l=1,r=n; while(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(p[l]>=p[r]&&l<r)p[l]+=p[r--]; else while(p[l]<p[r]&&l<r)p[r]+=p[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
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; 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; InputReader in = new InputReader(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, InputReader in, PrintWriter out) { int n = in.nextInt(); long s = in.nextLong(); 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(); } int pos = -(Arrays.binarySearch(x, s) + 1); if (pos == 0) { out.println(x[n - 1] - s); } else if (pos == n) { out.println(s - x[0]); } else { int left = 0; int right = n - 1; boolean leftWins = false; long res = x[n - 1] - x[0]; long people = p[0] + p[n - 1]; long lastTurn; if (p[left] >= p[right]) { leftWins = true; lastTurn = x[left]; } else { lastTurn = x[right]; } while (true) { if (leftWins) { if (right - 1 >= pos) { if (p[right - 1] > people) { leftWins = false; res += x[right - 1] - lastTurn; lastTurn = x[right - 1]; } people += p[right - 1]; --right; } else { res += s - lastTurn; break; } } else { if (left + 1 < pos) { if (p[left + 1] >= people) { leftWins = true; res += lastTurn - x[left + 1]; lastTurn = x[left + 1]; } people += p[left + 1]; ++left; } else { res += lastTurn - s; break; } } } out.println(res); } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
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 <iostream> #define ll long long int using namespace std; ll n; ll s; ll x[100005]; ll p[100005]; ll ans; void get_ans(ll left, ll right) { while (x[left] < s && x[right] > s) { ans += x[right] - x[left]; if (p[left] >= p[right]) { while (p[left] >= p[right] && right > left) { p[left] += p[right]; right--; } } else { while (p[right] > p[left] && right > left) { p[right] += p[left]; left++; } } } if (x[left] >= s) { ans += x[right] - s; return; } if (x[right] <= s) { ans += s - x[left]; return; } } int main() { cin >> n >> s; for (ll i = 0; i < n; i++) { cin >> x[i] >> p[i]; } ans = 0; get_ans(0, n - 1); 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<bits/stdc++.h> using namespace std; #define ll long long ll P[100010],h[100010]; int X[100010],n,s,tt; inline int rd() { int x=0;char ch=getchar(); for (;ch<'0'||ch>'9';ch=getchar()); for (;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; return x; } int main() { n=rd();s=rd(); for (int i=1;i<=n;i++) X[i]=rd(),P[i]=rd(); int id=0; for (int i=1;i<=n;i++) if (X[i]<s) id=i; int l=1,r=n; while (l<=id||r>id) { if (l>id) h[++tt]=X[r--]; else if (r<=id) h[++tt]=X[l++]; else if (P[l]>=P[r]) P[l]+=P[r],h[++tt]=X[r--]; else P[r]+=P[l],h[++tt]=X[l++]; } h[++tt]=s; ll ans=0; for (int i=1;i<tt;i++) ans+=abs(h[i+1]-h[i]); 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> #define int long long using namespace std; const int N = 1e5 + 5; int n, s, a[N], p[N]; 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; } int calc(int l, int r, int x) { if(s < a[l]) return a[r] - s; if(s > a[r]) return s - a[l]; if(p[l] >= p[r]) return p[l] += p[r], calc(l, r - 1, l) + (x == r ? a[r] - a[l] : 0); else return p[r] += p[l], calc(l + 1, r, r) + (x == l ? a[r] - a[l] : 0); } signed main() { // freopen(".in", "r", stdin); // freopen(".out", "w", stdout); n = read(); s = read(); for(int i = 1; i <= n; i ++) a[i] = read(), p[i] = read(); printf("%lld\n", calc(1, n, p[1] < p[n] ? 1 : n)); fclose(stdin); fclose(stdout); 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 title "title" #define ll long long #define ull unsigned ll #define fix(x) fixed<<setprecision(x) #define pii pair<ll,ll> #define vint vector<ll> #define pb push_back using namespace std; void Freopen(){ freopen(title".in","r",stdin); freopen(title".out","w",stdout); } ll read(){ ll g=0,f=1; char ch=getchar(); while(ch<'0'||'9'<ch){if(ch=='-')f=-1;ch=getchar();} while('0'<=ch&&ch<='9'){g=g*10+ch-'0';ch=getchar();} return g*f; } const ll N=1e5+5; ll n,st,x[N],p[N]; ll calc(ll l,ll r,ll t){ if(st<x[l])return x[r]-st;if(x[r]<st)return st-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); } signed main(){ n=read(),st=read(); for(ll i=1;i<=n;i++)x[i]=read(),p[i]=read(); return cout<<calc(1,n,p[1]<p[n]?1:n),signed(); }
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; typedef long long LL; int n; LL x[N], s, p[N]; LL calc(int l, int r, int last) { if(x[r] < s) return s - x[l]; if(x[l] > s) return x[r] - s; if(p[l] >= p[r]) { p[l] += p[r]; return (r == last)*(x[r] - x[l]) + calc(l, r - 1, l); } else { p[r] += p[l]; return (l == last)*(x[r] - x[l]) + calc(l + 1, r, r); } } int main() { cin>>n>>s; for(int i = 1; i <= n; i++) scanf("%lld%lld", &x[i], &p[i]); cout<<calc(1, n, (p[1] < p[n]) ? 1:n)<<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; #define ll long long ll n,s,a[100005],p[100005]; int main() { scanf("%lld%lld",&n,&s); for(int i=1;i<=n;i++)scanf("%lld%lld",&a[i],&p[i]); ll l=1,r=n,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[r]>p[l]&&l<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
#include<bits/stdc++.h> using namespace std; long long x['zzz'],p['zzz'],A,n,s,l=1,r; int main(){cin>>n>>s;r=n;for(int i=1;i<=r;i++)cin>>x[i]>>p[i];while(l<=r){if(s<=x[l])cout<<A+x[r]-s,exit(0);if(s>=x[r])cout<<A+s-x[l],exit(0);A+=x[r]-x[l];if(p[l]>=p[r]) while(p[l]>=p[r]&&l<r) p[l]+=p[r--];else while(p[l]<p[r]&&l<r) p[r]+=p[l++];}}
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
/** * author: tourist * created: 28.04.2018 16:16:25 **/ #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; long long s; cin >> n >> s; vector<long long> x(n); vector<long long> p(n); for (int i = 0; i < n; i++) { cin >> x[i] >> p[i]; } vector<long long> route; long long ans = 0; int i = 0, j = n - 1; while (true) { if (x[j] < s) { route.push_back(x[i]); break; } if (x[i] > s) { route.push_back(x[j]); break; } if (p[i] >= p[j]) { route.push_back(x[j]); p[i] += p[j]; j--; } else { route.push_back(x[i]); p[j] += p[i]; i++; } } for (int ii = (int) route.size() - 1; ii >= 0; ii--) { ans += abs(s - route[ii]); s = route[ii]; } 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 <iostream> #include <cstdio> #define MN 101000 int n, s, x[MN], q[MN], p[MN]; int Abs(int x) {return x > 0 ? x : -x;} int main() { scanf("%d%d", &n, &s); for(int i = 1; i <= n; i++) scanf("%d%d", &x[i], &p[i]); if(x[1] > s) return 0 * printf("%d\n", x[n] - s); int lst = 1, h = 2, t = n, i; long long sum = 0, ans = 0; for(i = 1; i <= n; i++) { int now; if(x[lst] < s) { if(x[t] < s) break; now = t--; } else { if(x[h] > s) break; now = h++; } if(p[now] > sum + p[lst] || p[now] == sum + p[lst] && x[now] < s) q[i] = x[lst], sum += p[lst], lst = now; else q[i] = x[now], sum += p[now]; } q[i] = x[lst]; q[i + 1] = s; for(int j = i; j >= 1; j--) ans += Abs(q[j + 1] - q[j]); 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,s,a[1000005],p[1000005]; inline long long read() { char ch; while((ch=getchar())<'0'||ch>'9'){;} long long res=ch-'0'; while((ch=getchar())>='0'&&ch<='9') res=res*10+ch-'0'; return res; } int main() { n=read(),s=read(); for(long long i=1;i<=n;i++) { a[i]=read(),p[i]=read(); } long long l=1,r=n; long long 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=r-1; } } else { while(p[r]>p[l]&&l<r) { p[r]+=p[l]; l+=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 = 100005; int n, S, X[N]; long long P[N]; 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 lastd = 0, L = 1, R = n; while (true) { if (X[L] >= S) { ans += X[R] - S; break; } if (X[R] <= S) { ans += S - X[L]; break; } if (P[L] >= P[R]) { if (lastd != 1) { lastd = 1; ans += X[R] - X[L]; } P[L] += P[R]; R--; } else { if (lastd != 2) { lastd = 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<cstdio> #include<algorithm> using namespace std; const int N=200002; typedef long long ll; int x[N],S,n,i; ll p[N],t[N],Ans; void init(){ scanf("%d%d",&n,&S); for(i=1;i<=n;i++) scanf("%d%lld",x+i,p+i); } void Query(int l,int r){ if(S>=x[r]){ t[l]=S-x[l]; return; } if(S<=x[l]){ t[r]=x[r]-S; return; } if(p[l]>=p[r]){ p[l]+=p[r]; Query(l,r-1); t[r]=t[l]+x[r]-x[l]; } else{ p[r]+=p[l]; Query(l+1,r); t[l]=t[r]+x[r]-x[l]; } } void work(){ Query(1,n); for(i=1;i<=n;i++) Ans=max(Ans,t[i]); printf("%lld",Ans); } int main(){ init(); 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<cstdio> #include<cstring> #include<algorithm> using namespace std; int n,s,x[100010],p[100010]; long long nowl,nowr,suml[100010],sumr[100010],ans,pos[100010]; int main() { scanf("%d %d",&n,&s); for(int i=1;i<=n;++i)scanf("%d %d",&x[i],&p[i]); // for(int i=1;i<=n;++i)suml[i]=suml[i-1]+p[i]; // for(int i=n;i;--i)sumr[i]=sumr[i+1]+p[i]; int ls,rs,l,r; for(int i=1;i<=n;++i)if(x[i]<s)ls=i; rs=ls+1; l=1,r=n; int last=-1; while(l<=ls&&r>=rs) { if(p[l]+nowl>=p[r]+nowr) { if(last!=1)ans+=x[r]-x[l]; last=1; // pos[r]=l-1; nowl+=p[r]; nowl+=nowr,nowr=0; --r; } else { if(last!=2)ans+=x[r]-x[l]; last=2; // pos[l]=r+1; nowr+=p[l]; nowr+=nowl,nowl=0; ++l; } // printf("%d %d %lld\n",l,r,ans); } if(l>ls)ans+=x[r]-s; else 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 <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, l, r, last; scanf( "%d%d", &N, &S ); for( i = 1; i <= N; ++i ) scanf( "%d%lld", x + i, p + i ); l = 1, r = N, last = 0; while( x[l] <= S && x[r] >= S ) { if( p[l] >= p[r] ) p[l] += p[r], ans += ( x[r] - x[l] ) * ( last != 1 ), last = 1, --r; else p[r] += p[l], ans += ( x[r] - x[l] ) * ( last != 2 ), last = 2, ++l; } 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
#include<bits/stdc++.h> #define ll long long #define pb push_back #define mr make_pair #define N 100010 using namespace std; ll ans; int n,x,nb,nc; struct Info{ll x,y;}a[N],b[N],c[N]; int main(){ cin>>n>>x; for (int i=1;i<=n;i++)cin>>a[i].x>>a[i].y; for (int i=1;i<=n;i++)if (a[i].x>x)b[++nb]=a[i]; for (int i=n;i>=1;i--)if (a[i].x<x)c[++nc]=a[i]; while (nc&&nb){ ans+=b[nb].x-c[nc].x; if (b[nb].y>c[nc].y){ while(b[nb].y>c[nc].y&&nc>0){b[nb].y+=c[nc].y;nc--;} }else{ while(b[nb].y<=c[nc].y&&nb>0){c[nc].y+=b[nb].y;nb--;} } } if (nb) ans+=b[nb].x-x; if (nc) ans+=x-c[nc].x; 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> #define fi first #define se second #define Mp make_pair #define pb push_back #define rep(i, j, k) for (int i = (j); i <= (k); i++) #define per(i, j, k) for (int i = (j); i >= (k); i--) using namespace std; typedef long long ll; typedef double db; typedef pair<int, int> PII; typedef vector<int> VI; const int N = 1e5+3; ll n,S,x[N],p[N],ans[N]; int main(){ scanf("%lld %lld",&n,&S); rep(i,1,n) scanf("%lld %lld",&x[i],&p[i]); int l=1,r=n; rep(i,1,n){ if(x[r]<S) ans[i]=x[l]; else if(x[l]>S) ans[i]=x[r]; else{ if(p[l]<p[r]) ans[i]=x[l],p[r]+=p[l++]; else ans[i]=x[r],p[l]+=p[r--]; } } ll out=0; per(i,n,1) out+=abs(S-ans[i]), S=ans[i]; printf("%lld\n",out); 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> using namespace std; #define N 100500 long long v[N],su[N],n,s,as,l,r,tp[N],ct; int main() { scanf("%lld%lld",&n,&s);l=1,r=n;ct=n; for(int i=1;i<=n;i++)scanf("%lld%lld",&v[i],&su[i]); while(l<r) { if(s<v[l])su[l]=1e16;if(s>v[r])su[r]=1e16; if(su[l]>=su[r])su[l]+=su[r],tp[ct--]=v[r],r--;else su[r]+=su[l],tp[ct--]=v[l],l++; } tp[1]=v[l];tp[0]=s; for(int i=1;i<=n;i++)as+=tp[i]>tp[i-1]?tp[i]-tp[i-1]:tp[i-1]-tp[i]; printf("%lld\n",as); }//
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 main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int N; int64_t S; cin >> N >> S; vector<int64_t> X(N), P(N); for (int i = 0; i < N; ++i) cin >> X[i] >> P[i]; int64_t ans = 0; auto go = [&](int64_t X) { ans += abs(X - S); S = X; }; function<void(int, int)> solve = [&](int l, int r) { if (X[l] > S) return go(X[r]); if (X[r] < S) return go(X[l]); if (P[l] >= P[r]) { P[l] += P[r]; solve(l, r-1); return go(X[r]); } else { P[r] += P[l]; solve(l+1, r); return go(X[l]); } }; solve(0, N-1); 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 <iostream> #include <cstdio> #define MAXN 100005 //#define ivorysi using namespace std; typedef long long int64; typedef double db; int N; int64 S,X[MAXN],P[MAXN],ans; void Solve() { scanf("%d%lld",&N,&S); for(int i = 1 ; i <= N ; ++i) { scanf("%lld%lld",&X[i],&P[i]); } int L = 1,R = N; int dir = 0; 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(dir != 1) {dir = 1;ans += X[R] - X[L];} P[L] += P[R];R--; } else { if(dir != 2) {dir = 2;ans += X[R] - X[L];} P[R] += P[L];L++; } } printf("%lld\n",ans); } int main() { Solve(); 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> using namespace std; #define N 100500 long long v[N],su[N],n,s,as,l,r,tp[N],ct; int main() { scanf("%lld%lld",&n,&s);l=1,r=n;ct=n; for(int i=1;i<=n;i++)scanf("%lld%lld",&v[i],&su[i]); while(l<r) { if(s<v[l])su[l]=1e16;if(s>v[r])su[r]=1e16; if(su[l]>=su[r])su[l]+=su[r],tp[ct--]=v[r],r--;else su[r]+=su[l],tp[ct--]=v[l],l++; } tp[1]=v[l];tp[0]=s; for(int i=1;i<=n;i++)as+=tp[i]>tp[i-1]?tp[i]-tp[i-1]:tp[i-1]-tp[i]; printf("%lld\n",as); }
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<stdio.h> int x[131072]; long long p[131072]; long long ans[131072]; int n,s; long long sol(int l,int r) { if(s>x[r]) { for(int i=l;i<=r;i++) { ans[i]=s-x[i]; } return ans[l]; } if(s<x[l]) { for(int i=l;i<=r;i++) { ans[i]=x[i]-s; } return ans[r]; } if(p[l]>=p[r]) { p[l]+=p[r]; sol(l,r-1); ans[r]=ans[l]+x[r]-x[l]; return ans[r]; } p[r]+=p[l]; sol(l+1,r); ans[l]=ans[r]+x[r]-x[l]; return ans[l]; } int main() { scanf("%d%d",&n,&s); for(int i=1;i<=n;i++) { scanf("%d%lld",&x[i],&p[i]); } printf("%lld\n",sol(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<iostream> #include<cstring> #include<cstdio> #define ll long long #define maxn 100005 using namespace std; ll a[maxn],b[maxn]; int q[100005]; int n,s; int main(){ 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; int n, s, x[101000]; long long p[101000]; int main(void) { scanf("%d%d", &n, &s); for(int i = 1; i <= n; i++) { scanf("%d%lld", &x[i], &p[i]); } int l = 1, r = n, flg1 = 1, flg2 = 1; long long ans = 0; while(l <= r) { 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]; if(flg1) ans += x[r] - x[l]; r--; flg1 = 0; flg2 = 1; } else { p[r] += p[l]; if(flg2) ans += x[r] - x[l]; l++; flg2 = 0; flg1 = 1; } //hcerr << l << " " << r << " " << ans << endl; } 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 N = 1e5 + 10; int n, s, pos[N], num[N]; signed main() { scanf("%lld%lld", &n, &s); for (int i = 1; i <= n; ++i) scanf("%lld%lld", pos + i, num + i); int lp = 1, rp = n, ans = 0; while (1) { if (s > pos[rp]) { ans += s - pos[lp]; break; } if (s < pos[lp]) { ans += pos[rp] - s; break; } ans += pos[rp] - pos[lp]; if (num[lp] >= num[rp]) { while (lp < rp && num[lp] >= num[rp]) num[lp] += num[rp--]; } else { while (lp < rp && num[lp] < num[rp]) num[rp] += num[lp++]; } } 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
/*考虑最左端和最右端人数 分别为A和B 若A>=B,显然B个人都投A,相当于删掉最右端 最左端人数变为A+B 考虑删除序列 显然最先删除的点一定最晚到达 开个栈模拟这个过程即可*/ #include<bits/stdc++.h> #define N 500005 #define int long long using namespace std; int n,s,z; struct Node{int x,y;}a[N]; inline bool cmp(Node aa,Node bb){return aa.x<bb.x;} signed main(){ scanf("%lld%lld",&n,&s); for (int i=1;i<=n;i++) scanf("%lld%lld",&a[i].x,&a[i].y); sort(a+1,a+n+1,cmp); int l=1;int r=n; while (l<=r){ if (a[l].x>=s){ z+=a[r].x-s; break; } if (a[r].x<=s){ z+=s-a[l].x; break; } z+=a[r].x-a[l].x; if (a[l].y>=a[r].y){ while (l<r&&a[l].y>=a[r].y) a[l].y+=a[r--].y; } else { while (l<r&&a[l].y<a[r].y) a[r].y+=a[l++].y; } } 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> using namespace std; int n,s; long long seq[100050],cnt[100050]; int main() { scanf("%d%d",&n,&s); for(int i = 1;i <= n; ++ i) scanf("%lld %lld",&seq[i],&cnt[i]); int l = 1,r = n; long long ans = 0; while(seq[l] <= s && seq[r] > s) { if(cnt[l] >= cnt[r]) { ans += seq[r] - seq[l]; while(cnt[l] >= cnt[r] && seq[r] > s) { cnt[l] += cnt[r]; r --; } } else { ans += seq[r] - seq[l]; while(cnt[r] > cnt[l] && seq[l] <= s) { cnt[r] += cnt[l]; l ++; } } } if(seq[l] <= s) ans += s - seq[l]; else ans += seq[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
#include <bits/stdc++.h> using namespace std; using ll = long long; #define f(i,a,b) for (int i = a; i < b; i++) int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifdef LOCAL freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); clock_t start = clock(); #endif int n; cin>>n; ll S; cin>>S; ll x[n], p[n]; f(i,0,n) cin>>x[i]>>p[i]; ll ans = 0; function<int(int,int)> dfs = [&](int i, int j) { if (x[i]>S) {ans += x[j]-S; return j;} else if (x[j]<S) { ans += S-x[i]; return i; } else { if (p[i]>=p[j]) { p[i] += p[j]; ans += x[j]-x[dfs(i,j-1)]; return j; } else { p[j] += p[i]; ans += x[dfs(i+1,j)]-x[i]; return i; } } }; dfs(0,n-1); cout << ans << endl; #ifdef LOCAL cout << setprecision(12) << (long double)(clock()-start) / CLOCKS_PER_SEC << endl; #endif 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 maxn = 1e5; int n, s, p = -1; ll a[maxn + 10], w[maxn + 10], ans; int main() { scanf("%d%d", &n, &s); for (int i = 1; i <= n; ++i) scanf("%lld%lld", &a[i], &w[i]); for (int l = 1, r = n; ; ) { if (a[l] >= s) { ans += p == -1 ? a[r] - s : a[r] - p + a[r] - s; break; } if (a[r] <= s) { ans += p == -1 ? s - a[l] : p - a[l] + s - a[l]; break; } if (w[l] >= w[r]) { ans += p == -1 ? a[r] - a[l] : p - a[l]; w[l] += w[r]; p = a[l]; --r; } else { ans += p == -1 ? a[r] - a[l] : a[r] - p; w[r] += w[l]; p = a[r]; ++l; } } 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 <cstdio> #include <cstring> using namespace std; int n,S,x[100007]; typedef long long ll; ll p[100007],ans = 0ll; int abs(int x) { return x > 0 ? x : -x; } int solve(int l,int r) { if(S < x[l]) { ans += x[r] - S; return x[r]; } if(S > x[r]) { ans += S - x[l]; return x[l]; } if(p[l] >= p[r]) { p[l] += p[r], ans += x[r] - solve(l,r - 1); return x[r]; } else { p[r] += p[l], ans += solve(l + 1,r) - x[l]; return x[l]; } } int main() { scanf("%d%d",&n,&S); for(int i = 1;i <= n; ++ i) scanf("%d%lld",x + i,p + i); solve(1,n), 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.math.*; import java.util.*; public class Main { void submit() { int n = nextInt(); int s = nextInt(); int[] xs = new int[n]; long[] ps = new long[n]; for (int i = 0; i < n; i++) { xs[i] = nextInt(); ps[i] = nextInt(); } ArrayList<Integer> order = new ArrayList<>(); int l = 0, r = n - 1; while (true) { if (xs[l] >= s) { for (int i = r; i >= l; i--) { order.add(i); } break; } if (xs[r] <= s) { for (int i = l; i <= r; i++) { order.add(i); } break; } if (ps[l] >= ps[r]) { ps[l] += ps[r]; order.add(r); r--; } else { ps[r] += ps[l]; order.add(l); l++; } } Collections.reverse(order); long ans = Math.abs(s - xs[order.get(0)]); for (int i = 1; i < n; i++) { ans += Math.abs(xs[order.get(i)] - xs[order.get(i - 1)]); } out.println(ans); } void test() { } void stress() { for (int tst = 0;; tst++) { if (false) { throw new AssertionError(); } System.err.println(tst); } } Main() throws IOException { is = System.in; out = new PrintWriter(System.out); submit(); // stress(); // test(); out.close(); } static final Random rng = new Random(); static final int C = 5; static int rand(int l, int r) { return l + rng.nextInt(r - l + 1); } public static void main(String[] args) throws IOException { new Main(); } private InputStream is; PrintWriter out; private byte[] buf = new byte[1 << 14]; private int bufSz = 0, bufPtr = 0; private int readByte() { if (bufSz == -1) throw new RuntimeException("Reading past EOF"); if (bufPtr >= bufSz) { bufPtr = 0; try { bufSz = is.read(buf); } catch (IOException e) { throw new RuntimeException(e); } if (bufSz <= 0) return -1; } return buf[bufPtr++]; } private boolean isTrash(int c) { return c < 33 || c > 126; } private int skip() { int b; while ((b = readByte()) != -1 && isTrash(b)) ; return b; } String nextToken() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!isTrash(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } String nextString() { int b = readByte(); StringBuilder sb = new StringBuilder(); while (!isTrash(b) || b == ' ') { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } double nextDouble() { return Double.parseDouble(nextToken()); } char nextChar() { return (char) skip(); } int nextInt() { int ret = 0; int b = skip(); if (b != '-' && (b < '0' || b > '9')) { throw new InputMismatchException(); } boolean neg = false; if (b == '-') { neg = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { ret = ret * 10 + (b - '0'); } else { if (b != -1 && !isTrash(b)) { throw new InputMismatchException(); } return neg ? -ret : ret; } b = readByte(); } } long nextLong() { long ret = 0; int b = skip(); if (b != '-' && (b < '0' || b > '9')) { throw new InputMismatchException(); } boolean neg = false; if (b == '-') { neg = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { ret = ret * 10 + (b - '0'); } else { if (b != -1 && !isTrash(b)) { throw new InputMismatchException(); } return neg ? -ret : ret; } b = readByte(); } } }
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> #define fo(a,b,c) for (a=b; a<=c; a++) #define fd(a,b,c) for (a=b; a>=c; a--) #define ll long long //#define file using namespace std; int b[100001],n,i,j,k,l,r,x; ll a[100001],ans; int dg(int l,int r) { int X; if (x<b[l]) {ans+=b[r]-x;return b[r];} if (x>b[r]) {ans+=x-b[l];return b[l];} if (a[l]>=a[r]) { a[l]+=a[r]; ans+=b[r]-dg(l,r-1); return b[r]; } else { a[r]+=a[l]; ans+=dg(l+1,r)-b[l]; return b[l]; } } int main() { #ifdef file freopen("agc023d.in","r",stdin); #endif scanf("%d%d",&n,&x); fo(i,1,n) scanf("%lld%d",&b[i],&a[i]); dg(1,n); printf("%lld\n",ans); fclose(stdin); fclose(stdout); 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 llong long long int n; int s; int pos[100100]; llong w[100100]; int main() { scanf("%d%d",&n,&s); for(int i=1;i<=n;++i)scanf("%d%lld",&pos[i],&w[i]); int l=1,r=n; llong ans=0; int las=-1;//0为向左,1为向右 while(233) { if(pos[r]<s||pos[l]>s)break; if(w[l]<w[r]) { if(las!=1)ans+=pos[r]-pos[l]; w[r]+=w[l];l++; las=1; } else { if(las!=0)ans+=pos[r]-pos[l]; w[l]+=w[r];r--; las=0; } } ans+=max(abs(pos[r]-s),abs(pos[l]-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<algorithm> using namespace std; int const MAX = 100005; long long p[MAX], x[MAX]; int order[MAX]; int main() { int n, s; cin >> n >> s; int t = 1; //sを挟んだ境界 for (int i = 1; i <= n; i++) { cin >> x[i] >> p[i]; if (x[i] < s) { t = i + 1; } } int left = 1; int right = n; int now = 1; while (left < t && right >= t) { if (p[left] >= p[right]) { order[now] = right; p[left] += p[right]; right--; now++; } else { order[now] = left; p[right] += p[left]; left++; now++; } } if (left == t) { order[now] = right; } else if (right < t) { order[now] = left; } for (int i = 1; i <= now; i++) { } long long ans = abs(s-x[order[now]]); while (now>1) { ans += abs(x[order[now]] - x[order[now-1]]); now--; } 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> #define il inline #define ll long long const int N=1e5+5; int n,s,x[N]; ll p[N]; il ll cal(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],cal(l, r - 1, l)+(t==r)*(x[r]-x[l]); return p[r]+=p[l],cal(l+1,r,r)+(t==l)*(x[r]-x[l]); } int main() { scanf("%d%d",&n,&s); int i; for (i=1; i<=n; i++) scanf("%d%lld",x+i,p+i); printf("%lld", cal(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; typedef long long LL; #define N 120000 LL n,m,a[N],b[N],ans,x,lst; void renew(int x){ if (!lst){lst=x; return;} ans+=abs(a[x]-a[lst]); lst=x; } int main(){ scanf("%lld%lld",&n,&m); for (int i=1;i<=n;++i) scanf("%lld%lld",a+i,b+i); for (int i=1,j=n;i<=j;){ if (a[j]<m){renew(i); ++i;} else{ if (a[i]>m){renew(j); --j;} else{ if (b[i]>=b[j]){ renew(j); b[i]+=b[j]; --j; } else{ renew(i); b[j]+=b[i]; ++i; } } } } ans+=abs(a[lst]-m); 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 i64 long long int #define ran 101111 i64 pos[ran], cnt[ran], s; int n; int o[ran], lo; i64 AB(i64 x){return x>=0?x:-x;} int main(){ ios::sync_with_stdio(false); cin >> n >> s; for(int i=0; i<n; i++) cin >> pos[i] >> cnt[i]; int L = 0, R = n-1; i64 totLef = cnt[L], totRig = cnt[R]; while(true){ if(pos[R] < s){ for(int i=L; i<=R; i++) o[lo++] = i; break; } if(pos[L] > s){ for(int i=R; i>=L; i--) o[lo++] = i; break; } if(totLef >= totRig){ totLef += totRig; o[lo++] = R; R --; totRig = cnt[R]; }else{ totRig += totLef; o[lo++] = L; L++; totLef = cnt[L]; } } reverse(o,o+lo); i64 ret = 0; for(int i=0; i<lo; i++){ ret += AB(pos[o[i]] - s); s = pos[o[i]]; } cout << ret << 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; const int N=100005,p=1000000007; int read(){ int f=1,g=0; char ch=getchar(); for (;!isdigit(ch);ch=getchar()) if (ch=='-') f=-1; for (;isdigit(ch);ch=getchar()) g=g*10+ch-'0'; return f*g; } int n,s,a[N]; ll ans,b[N]; int main(){ n=read();s=read(); for (int i=1;i<=n;i++) a[i]=read(),b[i]=read(); int l=1,r=n,las=0; while (1){ if (a[r]<s){ans+=s-a[l];break;} if (a[l]>s){ans+=a[r]-s;break;} if (b[l]>=b[r]) { if (las!=-1) ans+=a[r]-a[l]; b[l]+=b[r--]; las=-1; } else { if (las!=1) ans+=a[r]-a[l]; b[r]+=b[l++]; las=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<iostream> #include<cstdlib> #include<cstdio> #include<cmath> #include<algorithm> #include<cstring> #define ll long long using namespace std; const int N=100005; int n,l,r,last; ll s,ans; ll a[N],x[N]; int main(){ int i; scanf("%d%lld",&n,&s); for (i=1;i<=n;i++) scanf("%lld%lld",&x[i],&a[i]); if (n==1){ printf("%lld\n",abs(s-x[1])); return 0; } l=1; r=n; last=0; while (s>=x[l]&&s<=x[r]){ if (a[l]>=a[r]){ a[l]+=a[r]; if (last!=1) ans+=x[r]-x[l]; last=1; r--; } else{ a[r]+=a[l]; if (last!=2) ans+=x[r]-x[l]; last=2; l++; } } if (l<=r){ if (s>=x[l]) ans+=s-x[l]; else if (s<=x[r]) 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
#include <bits/stdc++.h> using namespace std; const int N=100050; int n,S,x[N]; long long ans,p[N]; int main(){ cin>>n>>S; for (int i=1; i<=n; ++i) scanf("%d%lld",&x[i],&p[i]); int l=1,r=n,dir=0; for (;;){ if (x[l]>=S) { ans+=x[r]-S; break; } if (x[r]<=S) { ans+=S-x[l]; break; } if (p[l]>=p[r]){ if (dir!=1) dir=1,ans+=x[r]-x[l]; p[l]+=p[r],--r; } else{ if (dir!=2) dir=2,ans+=x[r]-x[l]; p[r]+=p[l],++l; } } 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 <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> using namespace std; const int N=100010; int a[N]; long long ans,b[N]; inline int gi() { int x=0,o=1; char ch=getchar(); while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar(); if(ch=='-') o=-1,ch=getchar(); while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x*o; } int main() { int n,s; cin>>n>>s; for(int i=1;i<=n;i++) a[i]=gi(),b[i]=gi(); int l=1,r=n,t=0; while(1) { if(a[r]<=s) {ans+=s-a[l];break;} if(a[l]>=s) {ans+=a[r]-s;break;} if(b[l]>=b[r]) { if(t!=1) t=1,ans+=a[r]-a[l]; b[l]+=b[r--]; } else { if(t!=2) t=2,ans+=a[r]-a[l]; b[r]+=b[l++]; } } 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> #define fo(i,a,b)for(int i=a,_e=b;i<=_e;++i) #define ll long long using namespace std; const int N=1e5+5; int n,s,x[N]; ll p[N],f[N]; void get(int l,int r){ if(x[r]<s){ f[l]=s-x[l]; f[r]=s-x[r]; return; } if(s<x[l]){ f[l]=x[l]-s; f[r]=x[r]-s; return; } if(p[l]>=p[r])p[l]+=p[r],get(l,r-1),f[r]=f[l]+x[r]-x[l]; else p[r]+=p[l],get(l+1,r),f[l]=f[r]+x[r]-x[l]; } int main(){ ios::sync_with_stdio(false); cin>>n>>s; fo(i,1,n)cin>>x[i]>>p[i]; get(1,n); cout<<max(f[1],f[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 <iostream> using namespace std; const int N = 1e5 + 5; #define ll long long ll n , s; ll x[N] , p[N]; ll solve(ll l , ll r , ll w){ 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 solve(l , r - 1 , l) + ((w == r) ? (x[r] - x[l]) : 0); } else{ p[r] += p[l]; return solve(l + 1 , r , r) + ((w == l) ? (x[r] - x[l]) : 0); } } int main(){ cin >> n >> s; for(int i = 1; i <= n; i++) cin >> x[i] >> p[i]; cout << solve(1 , n , p[1] < p[n] ? 1 : n) << 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> const int N=1e5+5; int n,s,x[N]; long long p[N]; int main(){ scanf("%d%d",&n,&s); for(int i=1;i<=n;++i){ scanf("%d%d",&x[i],&p[i]); } int l=1,r=n,dir=-1; long long ans=0; while(1){ if(s<=x[l]){ ans+=x[r]-s; break; } if(s>=x[r]){ ans+=s-x[l]; break; } if(p[l]>=p[r]){ if(dir!=0) ans+=x[r]-x[l]; p[l]+=p[r]; dir=0; --r; } else{ if(dir!=1) ans+=x[r]-x[l]; p[r]+=p[l]; dir=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 <bits/stdc++.h> using namespace std; int n, s, x, p; deque<pair<int, long long> > d; long long amx(int a) { if(s >= d.back().first) { return s - d.front().first; } else if(s <= d.front().first) { return d.back().first - s; } int na; long long D = d.back().first - d.front().first; if(d.front().second >= d.back().second) { pair<int, long long> t = d.front(); t.second += d.back().second; d.pop_back(); d.pop_front(); d.push_front(t); na = 0; } else { pair<int, long long> t = d.back(); t.second += d.front().second; d.pop_front(); d.pop_back(); d.push_back(t); na = 1; } return amx(na) + (long long)(a != na) * D; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> s; for(int i = 1; i <= n; ++i) { cin >> x >> p; d.push_back({x, p}); } cout << amx(2) << 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
/* *********************************************** Author :dasinlsb Created Time :2018/6/5 13:26:16 File Name :C:\dasin\duipai.cpp ************************************************ */ #include<cstdio> using namespace std; typedef long long ll; #define pb push_back #define fi first #define se second const int N=100005; int n,s,x[N];ll p[N]; int main(){ // freopen("C:/dasin/aa.in","r",stdin); //freopen("C:/dasin/mine.out","w",stdout); int i,j; scanf("%d%d",&n,&s); for(i=1;i<=n;++i)scanf("%d%d",x+i,p+i); int l=1,r=n; ll ans=0; for(;l<=r;){ if(x[r]<=s){ans+=s-x[l];break;} if(x[l]>=s){ans+=x[r]-s;break;} ans+=x[r]-x[l]; if(p[l]>=p[r])for(;l<r&&p[l]>=p[r];p[l]+=p[r--]); else for(;l<r&&p[l]<p[r];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> using namespace std; const int maxn=100100; struct node{ int p; long long v; }a[maxn]; int n,s; int stk[maxn],top; int Abs(int x){ return x>0?x:-x; } int main(){ scanf("%d%d",&n,&s); for(int i=1;i<=n;i++) scanf("%d%lld",&a[i].p,&a[i].v); int l=1,r=n; while(1){ if(a[l].p>=s){ for(int i=r;i>=l;i--) stk[++top]=a[i].p; break; } if(a[r].p<=s){ for(int i=l;i<=r;i++) stk[++top]=a[i].p; break; } if(a[l].v>=a[r].v){ stk[++top]=a[r].p; a[l].v+=a[r].v,r--; } else{ stk[++top]=a[l].p; a[r].v+=a[l].v,l++; } } long long ans=0; stk[++top]=s; for(int i=top-1;i>=1;i--) ans=ans+Abs(stk[i+1]-stk[i]); 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<queue> using namespace std;queue<int> q; long long pos[100005],val[100005]; inline int fix(int x) {return max(x,-x);} int main() {int n,s,lb,rb,bnd=0;long long ans=0; scanf("%d%d",&n,&s);for (int i=1;i<=n;++i) { scanf("%lld%lld",pos+i,val+i);if (pos[i]<s) bnd=i; }lb=1,rb=n;while ((lb<=bnd)^(rb<=bnd)) { if (val[lb]<val[rb]) q.push(pos[lb]),val[rb]+=val[lb],++lb; else q.push(pos[rb]),val[lb]+=val[rb],--rb; }if (rb<=bnd) for (int i=lb;i<=rb;++i) q.push(pos[i]); else for (int i=rb;i>=lb;--i) q.push(pos[i]);q.push(s); while (q.size()>1) lb=q.front(),q.pop(),rb=q.front(),ans+=fix(lb-rb); 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; const int N=1e5+5; int n,s,l,r; ll x[N],p[N],ans; int main() { scanf("%d%d",&n,&s); for (int i=1;i<=n;i++) scanf("%lld%lld",&x[i],&p[i]); l=1;r=n; while (1==1) { if (x[r]<=s) {ans+=s-x[l];break;} if (x[l]>=s) {ans+=x[r]-s;break;} if (p[l]>=p[r]) { ans+=x[r]-x[l]; while (l<r&&p[l]>=p[r]) p[l]+=p[r--];//注意l<r,不能取等 }else { ans+=x[r]-x[l]; while (l<r&&p[l]<p[r]) 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> using namespace std; const int maxn=100005; inline int read(){ register int x=0,w=1,ch=getchar(); for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')w=-1; for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-48; return w*x; } int n,s,x[maxn]; long long p[maxn]; long long calc(int l,int r,int lst){ if(s<x[l])return x[r]-s; if(x[r]<s)return s-x[l]; if(p[l]>=p[r]){ p[l]+=p[r]; return calc(l,r-1,l)+(lst==r)*(x[r]-x[l]); } else{ p[r]+=p[l]; return calc(l+1,r,r)+(lst==l)*(x[r]-x[l]); } } 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)<<'\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 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,s; ll a[N],b[N],ans; int main(){ scanf("%d%d",&n,&s); rep (i,1,n) scanf("%d%d",&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--]; } else{ ans+=a[r]-a[l]; while (l<r && b[l]<b[r]) b[r]+=b[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; using Int = long long; template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;} template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;} //INSERT ABOVE HERE signed main(){ Int n,s; cin>>n>>s; vector<Int> x(n),p(n); for(Int i=0;i<n;i++) cin>>x[i]>>p[i]; Int l=0,r=n-1; vector<Int> dp(n,0); while(x[l]<s&&s<x[r]){ if(p[l]>=p[r]){ p[l]+=p[r]; chmax(dp[l],dp[r]+abs(x[r]-x[l])); r--; }else{ p[r]+=p[l]; chmax(dp[r],dp[l]+abs(x[r]-x[l])); l++; } } Int ans=0; for(Int i=0;i<n;i++) chmax(ans,dp[i]+abs(s-x[i])); 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; typedef long long ll; const int MAXN = 1e5+5; ll n, s, x[MAXN], p[MAXN], k; vector<ll> v; int main() { cin >> n >> s; for (int i = 0; i < n; ++i) cin >> x[i] >> p[i]; k = -1; for (int i = 0; i < n; ++i) if (x[i] < s) k++; int l = 0, r = n-1; while (true) { if (k < l) { for (int i = r; i >= l; --i) v.push_back(i); break; } if (k >= r) { for (int i = l; i <= r; i++) v.push_back(i); break; } if (p[l] >= p[r]) { v.push_back(r); p[l] += p[r]; r--; } else { v.push_back(l); p[r] += p[l]; l++; } } ll pos = s; reverse(v.begin(), v.end()); ll ans = 0; for (int i = 0; i < v.size(); ++i) { ans += abs(x[v[i]] - pos); pos = x[v[i]]; } 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<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; int x[101010]; long long p[101010]; int main() { int n,s,st,ed; int dir=0; long long ans=0; scanf("%d%d",&n,&s); st=1; ed=n; for(int i=1;i<=n;i++) scanf("%d%lld",&x[i],&p[i]); while(x[st]<s&&x[ed]>s) { if(p[st]>=p[ed]) { if(dir!=1) { ans+=x[ed]-x[st]; dir=1; } p[st]+=p[ed]; ed--; } else { if(dir!=2) { ans+=x[ed]-x[st]; dir=2; } p[ed]+=p[st]; st++; } } if(x[ed]>s) ans+=x[ed]-s; else ans+=s-x[st]; 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> #include<cmath> #define fo(x,a,b) for(int x=(a),e_=(b);x<=e_;x++) using namespace std; typedef long long ll; const int N=100005; ll q[N],x[N],a[N],ans=0,s; int n; int main(){ scanf("%d %lld",&n,&s); fo(i,1,n) scanf("%lld %lld",&x[i],&a[i]); int L=1, R=n;q[0]=0; while(L<=R) { if(x[L]>s) { ans+=x[R]-s, s=x[R]; break; } if(x[R]<s) { ans+=s-x[L], s=x[L]; break; } if(a[L]>=a[R]) a[L]+=a[R], q[++q[0]]=R, R--; else a[R]+=a[L], q[++q[0]]=L, L++; } for(; q[0]; q[0]--) ans+=abs(s-x[q[q[0]]]), s=x[q[q[0]]]; 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> #include <cctype> #include <algorithm> #include <cstring> using namespace std; typedef long long i64; inline int read(int f = 1, int x = 0, char ch = ' ') { while(!isdigit(ch = getchar())) if(ch == '-') f = -1; while(isdigit(ch)) x = x*10+ch-'0', ch = getchar(); return f*x; } const int N = 1e5+5; int n, s, p[N]; i64 c[N]; i64 solve(int x, int l, int r) { if(s < p[l]) return p[r]-s; if(s > p[r]) return s-p[l]; if(c[l] >= c[r]) return c[l] += c[r], solve(l, l, r-1)+(r==x?p[r]-p[l]:0); else return c[r] += c[l], solve(r, l+1, r)+(l==x?p[r]-p[l]:0); } int main() { n = read(), s = read(); for(int i = 1; i <= n; ++i) p[i] = read(), c[i] = read(); printf("%lld\n", solve(c[1]<c[n]?1: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; typedef long long ll; const int maxn = 1e5; int n, s, x[maxn + 3]; ll p[maxn + 3]; int _abs(int x) { return x < 0 ? -x : x; } ll solve(int l, int r, int t) { if (l > r) { return _abs(s - t); } ll ret = 0; if (x[l] > s || (x[r] > s && p[l] >= p[r])) { p[l] += p[r]; ret = solve(l, r - 1, x[r]); if (t != -1) ret += _abs(x[r] - t); } else { p[r] += p[l]; ret = solve(l + 1, r, x[l]); if (t != -1) ret += _abs(x[l] - t); } return ret; } int main() { scanf("%d %d", &n, &s); for (int i = 1; i <= n; i++) { scanf("%d %lld", &x[i], &p[i]); } printf("%lld\n", solve(1, n, -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<cstdio> #define int long long const int N=100005; int n,m,w[N],v[N],u,a; inline int ib(int x){return x>0?x:-x;} signed main() { scanf("%lld%lld",&n,&m); for(int i=1;i<=n;++i) scanf("%lld%lld",&w[i],&v[i]); for(int i=1,j=1,k=n;i<=n;++i) { if(w[j]>m){u&&(a+=ib(u-w[k])),u=w[k],--k;continue;} if(w[k]<m){u&&(a+=ib(u-w[j])),u=w[j],++j;continue;} if(v[j]>=v[k])u&&(a+=ib(u-w[k])),u=w[k],v[j]+=v[k],--k; else u&&(a+=ib(u-w[j])),u=w[j],v[k]+=v[j],++j; } printf("%lld",a+ib(u-m)); 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 200007 using namespace std; int n; long long x[Maxn],p[Maxn]; long long s; long long ans=0; int main() { scanf("%d%lld",&n,&s); for (int i=1;i<=n;i++) scanf("%lld%lld",&x[i],&p[i]); int lx=1,rx=n,pos=0; while (true) { if (x[lx]>=s) { ans+=x[rx]-s; break; } if (x[rx]<=s) { ans+=s-x[lx]; break; } if (p[lx]>=p[rx]) { if (pos!=1) { pos=1; ans+=x[rx]-x[lx]; } p[lx]+=p[rx]; --rx; } else { if (pos!=2) { pos=2; ans+=x[rx]-x[lx]; } p[rx]+=p[lx]; ++lx; } } 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 rep(i,n) for ((i)=1;(i)<=(n);(i)++) using namespace std; long long n,m,i,j,p[100005],c[100005],s[100005],ans,r,lst; int main(){ cin>>n>>m; rep(i,n){ cin>>p[i]>>c[i]; if(p[i]<=m) r=i; } i=1;j=n;lst=0; while(i<=r&&j>r){ if(c[i]>=c[j]){ if(lst!=1){ lst=1; s[i]+=p[j]-p[i]; } c[i]+=c[j]; s[i]+=s[j]; j--; } else{ if(lst!=-1){ lst=-1; s[j]+=p[j]-p[i]; } c[j]+=c[i]; s[j]+=s[i]; i++; } } if(i<=r) ans=s[i]+m-p[i]; else ans=s[j]+p[j]-m; 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=2e5; int n,s,l,r,x[maxn]; long long ans=0,p[maxn]; 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; while(1){ 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(p[l]<p[r]&&l<r){ p[r]+=p[l];l++; } } else{ while(p[l]>=p[r]&&l<r){ p[l]+=p[r];r--; } } } 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> #define LL long long const int maxn = 5e5 + 5; using namespace std; int n, s, x[maxn], dire; LL ans = 0, p[maxn]; 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; while (1){ if (x[l] >= s){ ans += 1ll * (x[r] - s); break; } if (x[r] <= s){ ans += 1ll * (s - x[l]); break; } if (p[l] < p[r]){ if (dire != 1) dire = 1, ans += 1ll * (x[r] - x[l]); p[r] += p[l++]; } else if (p[l] >= p[r]){ if (dire != -1) dire = -1, ans += 1ll * (x[r] - x[l]); p[l] += p[r--]; } } 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 sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines sys.setrecursionlimit(10 ** 7) """ ・両端のマンション → どういうルートをたどっても順序が決まる → 彼らの意見は常に一致するとしてよい """ N,S,*XP = map(int,read().split()) X = XP[::2] P = XP[1::2] leftN = sum(x<S for x in X) leftX = X[leftN-1::-1] # 内側から順にもつ leftP = P[leftN-1::-1] # 内側から順にもつ rightN = N-leftN rightX = X[leftN:] rightP = P[leftN:] visit = [] # 後ろ側から訪問順 while leftN and rightN: p1 = leftP[-1]; x1 = leftX[-1] p2 = rightP[-1]; x2 = rightX[-1] if p1 < p2: visit.append(x1) leftP.pop(); leftX.pop(); leftN -= 1 rightP[-1] = p1 + p2 else: visit.append(x2) rightP.pop(); rightX.pop(); rightN -= 1 leftP[-1] = p1 + p2 if leftN: visit += leftX[::-1] else: visit += rightX[::-1] visit.append(S) answer = sum(x-y if x>y else y-x for x,y in zip(visit,visit[1:])) print(answer)
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> #include<algorithm> #include<cstring> #define ll long long using namespace std; int n,s,l,r; ll ans,x[100005],p[100005]; int main() { scanf("%d%d",&n,&s); for(int i=1;i<=n;i++)scanf("%lld%lld",&x[i],&p[i]); l=1,r=n; while(s>=x[l]&&s<=x[r]) { ans+=x[r]-x[l]; if(p[l]<p[r]) { while(p[l]<p[r]&&s>=x[l]) p[r]+=p[l],l++; } else { while(p[l]>=p[r]&&s<=x[r]) p[l]+=p[r],r--; } } if(s<x[l])ans+=x[r]-s; else if(s>x[r])ans+=s-x[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<cstdio> #include<algorithm> using namespace std; struct node { int x; long long num; }a[100005]; long long ans,dis[100005]; inline int read() { char ch=getchar(); int ans=0; while (ch<'0'||ch>'9') ch=getchar(); while (ch<='9'&&ch>='0') ans=ans*10+ch-48,ch=getchar(); return ans; } bool cmp(node t1,node t2){return t1.x<t2.x;} int main() { int n=read(),s=read(); for (int i=1; i<=n; i++) a[i].x=read(),a[i].num=read(); sort(a+1,a+1+n,cmp); int l=1,r=n,t=2; while (a[l].x<s&&a[r].x>s) if (a[l].num<a[r].num) a[r].num+=a[l].num,dis[r]+=dis[l]+((t!=0)?(a[r].x-a[l].x):0),t=0,l++; else a[l].num+=a[r].num,dis[l]+=dis[r]+((t!=1)?(a[r].x-a[l].x):0),t=1,r--; if (a[l].x>s) ans=dis[r]+a[r].x-s; else ans=dis[l]+s-a[l].x; 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> typedef long long ll; ll gi(){ ll x=0,f=1; char ch=getchar(); while(!isdigit(ch))f^=ch=='-',ch=getchar(); while(isdigit(ch))x=x*10+ch-'0',ch=getchar(); return f?x:-x; } ll X[100010],P[100010]; int main(){ #ifdef XZZSB freopen("in.in","r",stdin); freopen("out.out","w",stdout); #endif int n=gi(),S=gi(); for(int i=1;i<=n;++i)X[i]=gi(),P[i]=gi(); int l=1,r=n;ll ans=0,s; int o=-1; while(1ll*(S-X[l])*(S-X[r])<0){ s=P[l]+P[r]; if(P[l]<P[r]){ if(o!=r)ans+=X[r]-X[l]; P[o=r]=s,++l; }else{ if(o!=l)ans+=X[r]-X[l]; P[o=l]=s,--r; } } ans+=std::max(abs(S-X[l]),abs(S-X[r])); 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> typedef long long ll; const int N=100010; int pos[N]; ll cnt[N]; int n,S; ll ans; int lst; void solve(int L,int R) { if(pos[L]>=S)return void(ans+=pos[R]-S); if(pos[R]<=S)return void(ans+=S-pos[L]); if(cnt[L]>=cnt[R]) { if(lst!=pos[L])ans+=pos[R]-pos[L]; lst=pos[L]; cnt[L]+=cnt[R]; solve(L,R-1); } else { if(lst!=pos[R])ans+=pos[R]-pos[L]; lst=pos[R]; cnt[R]+=cnt[L]; solve(L+1,R); } } int main() { scanf("%d%d",&n,&S); for(int i=1;i<=n;i++) scanf("%d%lld",pos+i,cnt+i); lst=-1;ans=0,solve(1,n); 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 MOD 1000000007LL using namespace std; typedef long long ll; typedef pair<int,int> P; int n; ll s; ll x[200005],p[200005]; ll now[200005]; ll labs(ll a){ if(a<0LL)return -a; return a; } int main(void){ scanf("%d%lld",&n,&s); for(int i=0;i<n;i++){ scanf("%lld%lld",&x[i],&p[i]); now[i]=p[i]; } int l=0,r=n-1; ll ans=0; while(l<=r){ if(s<=x[l]){ ans+=x[r]-s; break; } if(x[r]<=s){ ans+=s-x[l]; break; } if(now[l]>=now[r]){ ans+=x[r]-x[l]; while(l<r && now[l]>=now[r]){ now[l]+=now[r]; r--; } }else{ ans+=x[r]-x[l]; while(l<r && now[l]<now[r]){ now[r]+=now[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> #define int long long using namespace std; int n,pos,x[100010],id,g[100010],u; long long s[100010]; long long ans; int read() { int x=0; char ch=getchar(); while(ch<'0'||ch>'9') ch=getchar(); while(ch>='0'&&ch<='9') { x=(x<<3)+(x<<1)+ch-'0'; ch=getchar(); } return x; } void work(int l,int r) { if(u<=l) {for(int i=r;i>=l;--i) g[id--]=i; return;} if(u>=r+1) {for(int i=l;i<=r;++i) g[id--]=i; return;} if(s[l]>=s[r]) { g[id--]=r; s[l]+=s[r]; work(l,r-1); } else{ g[id--]=l; s[r]+=s[l]; work(l+1,r); } } signed main() { n=read(); pos=read(); for(int i=1;i<=n;++i) x[i]=read(),s[i]=read(); id=n; u=lower_bound(x+1,x+n+1,pos)-x; work(1,n); for(int i=1;i<=n;++i) ans+=abs(x[g[i]]-pos),pos=x[g[i]]; 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
using namespace std; #include <cstdio> #include <cstring> #include <algorithm> #define ll long long #define N 100010 int n,x0,bd; ll x[N],p[N]; int q[N],nq; int main(){ scanf("%d%lld",&n,&x[0]); for (int i=1;i<=n;++i){ scanf("%lld%lld",&x[i],&p[i]); if (x[i]<x[0]) bd=i; } int l=1,r=n; while (l<=bd && r>bd) if (p[l]>=p[r]){ p[l]+=p[r]; q[++nq]=r--; } else{ p[r]+=p[l]; q[++nq]=l++; } for (;l<=bd;++l) q[++nq]=l; for (;r>bd;--r) q[++nq]=r; ll ans=0; for (int i=nq;i>=1;--i) ans+=abs(x[q[i+1]]-x[q[i]]); 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 fre(i,t,n) for(int i =(t);i<=(n);++i) #define fer(i,n,t) for(int i =(n);i>=(t);--i) #define mp make_pair #define pb push_back typedef long long ll; void smain(); int main(){ ios::sync_with_stdio(false); smain(); return 0; } const int maxn = 1e5+100; ll P[maxn]; ll X[maxn]; ll ans=0; void smain(){ int N; ll S; cin>>N>>S; fre(i,1,N) cin>>X[i]>>P[i]; int i=1,j=N; int flag=-1; while(X[i]<S&&X[j]>S) { if(P[i]>=P[j]){ P[i]+=P[j]; if(flag!=1) flag=1,ans+=(X[j]-X[i]); --j; } else{ P[j]+=P[i]; if(flag!=0) flag=0,ans+=(X[j]-X[i]); ++i; } } ans+=max(abs(S-X[i]),abs(S-X[j])); 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 MX = 100000; int x[MX]; long long p[MX]; int main() { int n, s; ignore = scanf("%d %d", &n, &s); for (int i = 0; i < n; i++) ignore = scanf("%d %lld", x + i, p + i); vector<int> order; int i = 0, j = n - 1; while (i < j) { if (p[i] >= p[j]) { order.push_back(j); p[i] += p[j]; j--; } else { order.push_back(i); p[j] += p[i]; i++; } } order.push_back(i); reverse(order.begin(), order.end()); long long ans = 0; int L = s, R = s; for (int i : order) { if (L <= x[i] && x[i] <= R) continue; ans += abs(x[i] - s); s = x[i]; L = min(L, s); R = max(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
#include <cstdio> #include <cmath> #include <cstring> #include <cstdlib> #include <algorithm> typedef long long LL; const int MAXN = 1e5 + 10; int n, S; LL ans; int a[MAXN]; LL b[MAXN]; int main() { scanf("%d%d", &n, &S); for (int i = 1; i <= n; ++i) scanf("%d%lld", &a[i], &b[i]); for (int L = 1, R = n, dir = 2333;;) { if (S < a[L]) { ans += a[R] - S; break; } if (S > a[R]) { ans += S - a[L]; break; } if (b[L] >= b[R]) ans += dir != 0 ? a[R] - a[L] : 0, b[L] += b[R--], dir = 0; else ans += dir != 1 ? a[R] - a[L] : 0, b[R] += b[L++], dir = 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 <iostream> #include <cstdio> #define MAXN 100005 //#define ivorysi using namespace std; typedef long long int64; typedef double db; int N; int64 S,X[MAXN],P[MAXN],ans; void Solve() { scanf("%d%lld",&N,&S); for(int i = 1 ; i <= N ; ++i) { scanf("%lld%lld",&X[i],&P[i]); } int L = 1,R = N; int dir = 0; 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(dir != 1) {dir = 1;ans += X[R] - X[L];} P[L] += P[R];R--; } else { if(dir != 2) {dir = 2;ans += X[R] - X[L];} P[R] += P[L];L++; } } printf("%lld\n",ans); } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); 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> #define fo(i,a,b) for(int i=a;i<=b;i++) #define fd(i,a,b) for(int i=a;i>=b;i--) using namespace std; typedef long long ll; int read() { char ch; for(ch=getchar();ch<'0'||ch>'9';ch=getchar()); int x=ch-'0'; for(ch=getchar();ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; return x; } const int N=1e5+5; int n,s,x[N]; ll t[N],p[N]; void solve(int l,int r) { if (x[r]<s) { fo(i,l,r) t[i]=s-x[i]; return; } if (x[l]>s) { fo(i,l,r) t[i]=x[i]-s; return; } if (p[l]>=p[r]) { p[l]+=p[r]; solve(l,r-1); t[r]=t[l]+x[r]-x[l]; } else { p[r]+=p[l]; solve(l+1,r); t[l]=t[r]+x[r]-x[l]; } } int main() { n=read();s=read(); fo(i,1,n) x[i]=read(),p[i]=read(); solve(1,n); printf("%lld\n",max(t[1],t[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 java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.IOException; import java.util.ArrayList; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.Collections; import java.io.InputStream; /** * Built using CHelper plug-in Actual solution is at the top * * @author ilyakor */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } static class TaskD { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); int s = in.nextInt(); ArrayList<ii> left = new ArrayList<>(); ArrayList<ii> right = new ArrayList<>(); for (int i = 0; i < n; ++i) { int x = in.nextInt() - s; int y = in.nextInt(); if (x < 0) { left.add(new ii(x, y)); } if (x > 0) { right.add(new ii(x, y)); } } Collections.reverse(left); // long[] sumLeft = partSum(left); // long[] sumRight = partSum(right); long res = 0; int sl = left.size() - 1, sr = right.size() - 1; int[] ra = new int[sl + sr + 2]; int rs = 0; long vote = 0; long prev = 0; while (sl >= 0 || sr >= 0) { if (sl < 0) { ra[rs++] = +1; --sr; continue; } if (sr < 0) { ra[rs++] = -1; --sl; continue; } //long total = vote - left.get(sl).second + right.get(sr).second; long total = vote * prev - left.get(sl).second + right.get(sr).second; if (total <= 0) { ra[rs++] = +1; //vote -= right.get(sr).second; prev = -1; vote += right.get(sr).second; --sr; } else { ra[rs++] = -1; prev = 1; vote += left.get(sl).second; --sl; } } long cur = 0; sl = 0; sr = 0; for (int i = rs - 1; i >= 0; --i) { if (ra[i] > 0) { res += right.get(sr).first - cur; cur = right.get(sr).first; ++sr; } else { res += cur - left.get(sl).first; cur = left.get(sl).first; ++sl; } } Assert.assertTrue(sl == left.size()); Assert.assertTrue(sr == right.size()); out.printLine(res); // int[] sleft = calc(left, right, 1); // int[] sright = calc(right, left, 0); // long res = 0; // int cur = 0; // int uleft = 0, uright = 0; // for (int pleft = 0, pright = 0; pleft < left.size() || pright < right.size(); ) { // if (pleft == left.size()) { // res += right.get(pright).first - cur; // cur = right.get(pright).first; // System.err.print("r"); // ++pright; // continue; // } // if (pright == right.size()) { // res += cur - left.get(pleft).first; // cur = left.get(pleft).first; // System.err.print("l"); // ++pleft; // continue; // } // while (uleft < left.size() && sleft[uleft] <= pright) // ++uleft; // while (uright < right.size() && sright[uright] <= pleft) // ++uright; // if (uleft < pleft) uleft = pleft; // if (uright < pright) uright = pright; // long leftLeft = sum(sumLeft, pleft, uleft); // long rightRight = sum(sumRight, pright, uright); // long totalLeft = leftLeft + sum(sumRight, uright, right.size()); // long totalRight = rightRight + sum(sumLeft, uleft, left.size()); // if (totalLeft >= totalRight) { // res += cur - left.get(pleft).first; // cur = left.get(pleft).first; // System.err.print("l"); // ++pleft; // } else { // res += right.get(pright).first - cur; // cur = right.get(pright).first; // System.err.print("r"); // ++pright; // } // } // System.err.println(); // out.printLine(res); } } static class InputReader { private InputStream stream; private byte[] buffer = new byte[10000]; private int cur; private int count; public InputReader(InputStream stream) { this.stream = stream; } public static boolean isSpace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int read() { if (count == -1) { throw new InputMismatchException(); } try { if (cur >= count) { cur = 0; count = stream.read(buffer); if (count <= 0) { return -1; } } } catch (IOException e) { throw new InputMismatchException(); } return buffer[cur++]; } public int readSkipSpace() { int c; do { c = read(); } while (isSpace(c)); return c; } public int nextInt() { int sgn = 1; int c = readSkipSpace(); if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res = res * 10 + c - '0'; c = read(); } while (!isSpace(c)); res *= sgn; return res; } } static class Assert { public static void assertTrue(boolean flag) { // if (!flag) // while (true); if (!flag) { throw new AssertionError(); } } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } static class ii implements Comparable<ii> { public int first; public int second; public ii() { } public ii(int first, int second) { this.first = first; this.second = second; } public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } ii ii = (ii) o; if (first != ii.first) { return false; } if (second != ii.second) { return false; } return true; } public int hashCode() { int result = first; result = 31 * result + second; return result; } public int compareTo(ii o) { if (first != o.first) { return first < o.first ? -1 : 1; } if (second != o.second) { return second < o.second ? -1 : 1; } return 0; } public String toString() { return "{" + "first=" + first + ", second=" + second + '}'; } } }
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; #define rep(i,j,k) for(int i=j;i<=k;++i) typedef long long ll; char cch; inline ll rd(){ ll x=0,fl=1; cch=getchar(); while(cch>'9'||cch<'0'){ if(cch=='-') fl=-1; cch=getchar(); } while(cch>='0'&&cch<='9') x=(x<<3)+(x<<1)+cch-'0',cch=getchar(); return x*fl; } const int N=1e5+3; int a[N]; ll p[N]; int main(){ int n=rd(),s=rd(); rep(i,1,n) a[i]=rd(),p[i]=(ll)rd(); ll ans=0; int l=1,r=n; while(l<=r){ if(a[l]>s){ ans=ans+a[r]-s;break; } if(a[r]<s){ ans=ans+s-a[l];break; } ans+=a[r]-a[l]; if(p[l]<p[r]) while(p[l]<p[r]&&l<r) p[r]+=p[l],++l; else if(p[l]>=p[r])while(p[l]>=p[r]&&l<r) p[l]+=p[r],--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<bits/stdc++.h> using namespace std; const int N = 1e5 + 11; #define LL long long LL a[N], p[N], s, ans; int n; LL dfs(int l, int r){ if(a[l] > s || a[r] < s){ if(a[l] > s){ ans += a[r] - s; return a[r]; } if(a[r] < s){ ans += s - a[l]; return a[l]; } } LL now, pos; if(p[l] >= p[r]){ now = r; p[l] += p[r]; pos = dfs(l, r - 1); } else { now = l; p[r] += p[l]; pos = dfs(l + 1, r); } ans += abs(a[now] - pos); return a[now]; } int main(){ cin>>n>>s; for(int i = 1;i <= n; i++){ scanf("%lld%lld", &a[i], &p[i]); } dfs(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<cstdio> using namespace std; int n, s, l, r, last; long long ans, x[100001], p[100001]; int main() { scanf("%d%d", &n, &s); for (int i = 1; i <= n; ++i) { scanf("%lld%lld", &x[i], &p[i]); } l = 1; r = n; while (l <= r) { if (s <= x[l]) { ans += x[r] - s; break; } if (s >= x[r]) { 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); }
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.BufferedReader; import java.io.InputStreamReader; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in))); int N = sc.nextInt(); long S = sc.nextInt(); long[] x = new long[N], p = new long[N]; for (int i = 0; i < N; i++) { x[i] = sc.nextInt(); p[i] = sc.nextInt(); } int left = 0, right = N - 1; long[] order = new long[N]; int idx = 0; while (x[left] < S && S < x[right]) { if (p[left] >= p[right]) { order[idx++] = x[right]; p[left] += p[right]; right--; } else { order[idx++] = x[left]; p[right] += p[left]; left++; } } while (right >= 0 && S < x[right]) { order[idx++] = x[right]; right--; } while (left < N && x[left] < S) { order[idx++] = x[left]; left++; } long ans = 0; for (int i = N - 1; i >= 0; i--) { ans += Math.abs(order[i] - S); S = order[i]; } System.out.println(ans); } }
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<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; const int N=100007; int n,l,r,f; ll s,x[N],sm[N],p[N]; int main(){ scanf("%d%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]){ if(p[l]>=p[r])p[l]+=p[r],sm[l]+=sm[r]+(x[r]-x[l])*(f!=1),f=1,--r; else p[r]+=p[l],sm[r]+=sm[l]+(x[r]-x[l])*(f!=-1),f=-1,++l; } printf("%lld\n",s<x[l]?(x[r]-s+sm[r]):(s-x[l]+sm[l])); 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 <vector> #include <cstdio> #include <string> #include <cstring> #include <iostream> #include <algorithm> using namespace std; typedef long long ll; const int N=1e5+5; int n,cnt; ll S,X[N],P[N],tmp[N]; template <typename _Tp> inline void IN(_Tp&x) { char ch;bool flag=0;x=0; while(ch=getchar(),!isdigit(ch)) if(ch=='-') flag=1; while(isdigit(ch)) x=x*10+ch-'0',ch=getchar(); if(flag) x=-x; } int main() { IN(n),IN(S); int ID=0,l=1,r=n; for(int i=1;i<=n;++i) IN(X[i]),IN(P[i]); for(int i=1;i<=n;++i) if(X[i]<S) ID=i; while((l<=ID)^(r<=ID)) { if(P[l]>=P[r]) P[l]+=P[r],tmp[++cnt]=X[r--]; else P[r]+=P[l],tmp[++cnt]=X[l++]; } if(r<=ID) for(int i=l;i<=r;++i) tmp[++cnt]=X[i]; else for(int i=r;i>=l;--i) tmp[++cnt]=X[i]; tmp[++cnt]=S; ll ans=0; for(int i=1;i<cnt;++i) ans+=abs(tmp[i]-tmp[i+1]); return printf("%lld\n",ans),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
#!/usr/bin/python # -*- coding: utf-8 -*- import sys,collections,itertools,re,math,fractions,decimal,random,array,bisect,heapq # decimal.getcontext().prec = 50 # sys.setrecursionlimit(10000) # MOD = 10**9 + 7 def solve(f): n, s = f.read_int_list() x = [f.read_int_list() for _ in xrange(n)] ans = 0 side = 0 l = 0 r = n-1 while True: if x[l][0] > s: return ans + x[r][0] - s elif x[r][0] < s: return ans + s - x[l][0] else: if x[l][1] >= x[r][1]: x[l][1] += x[r][1] if side != 1: ans += x[r][0] - x[l][0] side = 1 r -= 1 else: x[r][1] += x[l][1] if side != -1: ans += x[r][0] - x[l][0] side = -1 l += 1 class Reader(object): def __init__(self, filename=None): self.file = open(filename) if filename is not None else None self.case = 1 def __readline(self): return self.file.next().strip() if self.file else raw_input() def next_case(self): self.file.next() self.case += 1 def read_int(self): return int(self.__readline()) def read_float(self): return float(self.__readline()) def read_long(self): return long(self.__readline()) def read_decimal(self): return decimal.Decimal(self.__readline()) def read_str(self): return self.__readline() def read_int_list(self): return map(int, self.__readline().split()) def read_float_list(self): return map(float, self.__readline().split()) def read_long_list(self): return map(long, self.__readline().split()) def read_decimal_list(self): return map(decimal.Decimal, self.__readline().split()) def read_str_list(self): return self.__readline().split() if __name__ == '__main__': filename = sys.argv[1] if len(sys.argv) > 1 else None f = Reader(filename) if f.file: while True: print "Case #%d\n"%f.case, solve(f) try: f.next_case() except StopIteration: break else: print solve(f)
PYTHON
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; #define ll long long //{{{ read() ll read(){ ll 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*10+(ch^48),ch=getchar(); return x*f; } //}}} const int N=1e5+5; struct each{ int p; ll x; }a[N]; ll ans; int n,s,las; bool com(each x,each y){ return x.p<y.p; } ll sol(int l,int r,int s){ if(s<=a[l].p) return a[las=r].p-s; if(s>=a[r].p) return s-a[las=l].p; if(a[l].x>=a[r].x){ a[l].x+=a[r].x; ll d=sol(l,r-1,s); d+=a[r].p-a[las].p,las=r; return d; } else{ a[r].x+=a[l].x; ll d=sol(l+1,r,s); d+=a[las].p-a[l].p,las=l; return d; } } int main(){ n=read(),s=read(); for(int i=1;i<=n;i++) a[i].p=read(),a[i].x=read(); sort(a+1,a+n+1,com); printf("%lld\n",sol(1,n,s)); return 0; }
CPP