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> const int N=100005; using namespace std; int n,S,a[N],lst=2; long long b[N],ans; bool fl; int main(){ scanf("%d%d",&n,&S); for(int i=1;i<=n;i++) scanf("%d%lld",a+i,b+i); a[n+1]=2e9; for(int h=1,t=n;;){ if(a[h]<S&&a[t]>S){ fl=b[h]<b[t]; if(lst^fl)ans+=a[t]-a[h]; fl?b[t]+=b[h++]:b[h]+=b[t--]; lst=fl; } else{ if(a[h]>S)lst?ans+=a[t]-S:0; if(a[t]<S)lst^1?ans+=S-a[h]:0; break; } } printf("%lld",ans); }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include<bits/stdc++.h> using namespace std; int N, S, x[100009]; long long p[100009]; int main () { //freopen ("input", "r", stdin); //freopen ("output", "w", stdout); scanf ("%d %d", &N, &S); for (int i=1; i<=N; i++) scanf ("%d %lld", &x[i], &p[i]); int i = 1, j = N, lst = 0; long long ans = 0; while (x[i] <= S && x[j] >= S) { if (p[i] >= p[j]) p[i] += p[j], ans += (x[j --] - x[i]) * (lst != 1), lst = 1; else p[j] += p[i], ans += (x[j] - x[i ++]) * (lst != 2), lst = 2; } if (i <= j) { if (x[i] <= S) ans += S - x[i]; else ans += x[j] - 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=1e5+10; 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; for(int l=1,r=n; ; ){ 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
#include <bits/stdc++.h> #define For(i, j, k) for (int i = j; i <= k; i++) using namespace std; typedef long long LL; const int N = 1e5 + 10; int x[N]; LL w[N]; int S, n; int main() { scanf("%d%d", &n, &S); For(i, 1, n) scanf("%d%lld", &x[i], &w[i]); int l = 1, r = n, nxt = 0; LL ans = 0; while (x[l] < S && x[r] > S) { if (w[l] >= w[r]) { w[l] += w[r]; if (nxt != l) ans += x[r] - x[l]; --r, nxt = l; } else { w[r] += w[l]; if (nxt != r) ans += x[r] - x[l]; ++l, nxt = 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> using namespace std; #define int long long const int maxn=1e5+5; inline int read(){ char c=getchar();int t=0,f=1; while((!isdigit(c))&&(c!=EOF)){if(c=='-')f=-1;c=getchar();} while((isdigit(c))&&(c!=EOF)){t=(t<<3)+(t<<1)+(c^48);c=getchar();} return t*f; } int n,x[maxn],ans,s,p[maxn]; void dfs(int l,int r){ if(l>r)return ; if(x[l]<s&&s<x[r]){ if(p[l]<p[r]){ p[r]+=p[l]; dfs(l+1,r); ans=ans+abs(x[l]-s); s=x[l]; } else{ p[l]+=p[r]; dfs(l,r-1); ans=ans+abs(x[r]-s); s=x[r]; } return ; } if(s<=x[l]){ ans=ans+(x[l]-s); s=x[l]; dfs(l+1,r); return ; } else if(s>=x[r]){ ans=ans+(s-x[r]); s=x[r]; dfs(l,r-1); return ; } } signed main(){ n=read();s=read(); for(int i=1;i<=n;i++){ x[i]=read(); p[i]=read(); } dfs(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 <cstdio> #include <algorithm> #include <cmath> #define maxn 500005 using namespace std; struct node{ long long pos, num; }a[maxn]; long long n, s, ans = 0; bool cmp(node x, node y){ return (x.pos < y.pos); } int main(){ scanf("%lld%lld", &n, &s); for (long long i = 1; i <= n; i++) scanf("%lld%lld", &a[i].pos, &a[i].num); sort(a + 1, a + n + 1, cmp); long long left = 1, right = n; while (left <= right){ if (a[right].pos <= s){ ans += (s - a[left].pos); break; } if (a[left].pos >= s){ ans += (a[right].pos - s); break; } ans += (a[right].pos - a[left].pos); if (a[left].num >= a[right].num){ while (left < right && a[left].num >= a[right].num) a[left].num += a[right--].num; } else{ while (left < right && a[right].num > a[left].num) a[right].num += a[left++].num; } } 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
// package agc.agc023; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class Main { public static void main(String[] args) { InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.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.nextInt(); p[i] = in.nextInt(); } int ith = n; for (int i = 0; i < n ; i++) { if (s < x[i]) { ith = i; break; } } int l = 0; int r = n-1; long dist = 0; int last = 2; while (true) { if (ith <= l || r < ith) { dist += x[r] - x[l]; if (ith <= l) { dist += x[l] - s; } else { dist += s - x[r]; } break; } if (p[l] >= p[r]) { p[l] += p[r]; if (last != 0) { dist += x[r] - x[l]; } last = 0; r--; } else { p[r] += p[l]; if (last != 1) { dist += x[r] - x[l]; } last = 1; l++; } } out.println(dist); out.flush(); } public static void debug(Object... o) { System.err.println(Arrays.deepToString(o)); } public static class InputReader { private static final int BUFFER_LENGTH = 1 << 12; private InputStream stream; private byte[] buf = new byte[BUFFER_LENGTH]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } private int next() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public char nextChar() { return (char) skipWhileSpace(); } public String nextToken() { int c = skipWhileSpace(); StringBuilder res = new StringBuilder(); do { res.append((char) c); c = next(); } while (!isSpaceChar(c)); return res.toString(); } public int nextInt() { return (int) nextLong(); } public long nextLong() { int c = skipWhileSpace(); long sgn = 1; if (c == '-') { sgn = -1; c = next(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = next(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { return Double.valueOf(nextToken()); } int skipWhileSpace() { int c = next(); while (isSpaceChar(c)) { c = next(); } return c; } boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
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 int long long #define N 100005 using namespace std; int X[N],P[N],n,S,ret,s,t; signed main() { scanf("%lld%lld",&n,&S); for(int i=0;i<n;i++) scanf("%lld %lld",&X[i],&P[i]); ret=0; s=0,t=n-1; while(s<=t) { if(S<=X[s]) { ret+=X[t]-S; break; } if(X[t]<=S) { ret+=S-X[s]; break; } if(P[s]>=P[t]) { ret+=X[t]-X[s]; while(s<t&&P[s]>=P[t]) { P[s]+=P[t]; t--; } } else { ret+=X[t]-X[s]; while(s<t&&P[s]<P[t]) { P[t]+=P[s]; s++; } } } 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<iostream> #include<algorithm> #include<cstdio> #include<cstring> using namespace std; int n,s; int x[100010]; long long p[100010]; int main() { scanf("%d %d",&n,&s); for(int i=1;i<=n;i++) scanf("%d %lld",&x[i],&p[i]); long long ans=0; int l=1,r=n; int lst=0; while(1) { if(s<=x[l]){ printf("%lld\n",ans+x[r]-s); return 0; } if(s>=x[r]){ printf("%lld\n",ans+s-x[l]); return 0; } if(p[l]>=p[r]){ if(lst!=1)ans+=x[r]-x[l]; lst=1; p[l]+=p[r]; r--; } else{ if(lst!=2)ans+=x[r]-x[l]; lst=2; p[r]+=p[l]; 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 <iostream> #include <cstdio> #include <algorithm> #include <cstring> #define LL long long #define MAXN 100005 using namespace std; struct Node {LL x, p;} a[MAXN]; LL n, s, p; LL doit(LL l, LL r, LL p) { if(a[l].x>=s) return a[r].x-s; if(a[r].x<=s) return s-a[l].x; if(a[l].p>=a[r].p) { a[l].p+=a[r].p; return doit(l, r-1, l)+a[p].x-a[l].x; } else { a[r].p+=a[l].p; return doit(l+1, r, r)+a[r].x-a[p].x; } } int main() { scanf("%lld%lld", &n, &s); for(int i=1; i<=n; i++) scanf("%lld%lld", &a[i].x, &a[i].p); if(a[1].p>=a[n].p) p=n; else p=1; printf("%lld\n", doit(1, n, p)); }
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[100010],p[100010]; int main() { int n,s,l=1,r;long long ans=0;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<<ans+x[r]-s,exit(0); if(s>=x[r]) cout<<ans+s-x[l],exit(0); 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++]; } }
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
//Love and Freedom. #include<cstdio> #include<cmath> #include<algorithm> #include<cstring> #define ll long long #define inf 20021225 using namespace std; int read() { int s=0,t=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-') t=-1; ch=getchar();} while(ch>='0' && ch<='9') s=s*10+ch-'0',ch=getchar(); return s*t; } #define N 100010 int n,s,x[N]; ll p[N]; int main() { n=read(),s=read(); for(int i=1;i<=n;i++) x[i]=read(),p[i]=read(); int l=1,r=n; ll ans=0; int fl=2; while(l<r&&x[l]<s&&x[r]>s) { if(p[l]<p[r]) { p[r]+=p[l]; if(fl!=0) ans+=x[r]-x[l],fl=0; l++; } else { p[l]+=p[r]; if(fl!=1) ans+=x[r]-x[l],fl=1; r--; } } if(x[l]>s) 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<bits/stdc++.h> #define rep(i,a,b) for (int i=(a); i<=(b); i++) #define per(i,a,b) for (int i=(a); i>=(b); i--) using namespace std; typedef long long ll; const int maxn = 100005; ll p[maxn], x[maxn], ans; int n, S, l, r, last; int main() { cin >> n >> S; rep (i, 1, n) cin >> x[i] >> p[i]; l = 1; r = n; last = 0; while (x[l] < S && S < x[r]) if (p[l] >= p[r]) { if (last) ans += abs(x[r] - x[last]); last = r; p[l] += p[r--]; } else { if (last) ans += abs(x[l] - x[last]); last = l; p[r] += p[l++]; } if (x[l] < S) { if (last) ans += abs(x[l] - x[last]); ans += abs(S - x[l]); } if (x[r] > S) { if (last) ans += abs(x[r] - x[last]); ans += abs(x[r] - S); } cout << ans << endl; return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #define ll long long #define N 100010 using namespace std; int n,m,cnt,x[N],d[N]; ll ans,v[N]; int main() { int i,s,a,b,j,k; int l,r; scanf("%d%d",&n,&m); for(i=1;i<=n;i++)scanf("%d%lld",&x[i],&v[i]); l=1;r=n;x[0]=m; while(l<=r) { if(m<=x[l]) { ans+=x[r]-m,d[cnt+1]=r; break; } if(x[r]<=m) { ans+=m-x[l];d[cnt+1]=l; break; } if(v[l]<v[r])d[++cnt]=l,v[r]+=v[l++]; else d[++cnt]=r,v[l]+=v[r--]; } for(i=1;i<=cnt;i++)ans+=abs(x[d[i]]-x[d[i+1]]); printf("%lld",ans); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include<cstdio> #define int long long int n,S,x[300100],p[300100],ans; signed main(){ scanf("%lld%lld",&n,&S); for(int i=1;i<=n;i++) scanf("%lld%lld",&x[i],&p[i]); int l=1,r=n,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)ans+=x[r]-x[l],dir=1; p[l]+=p[r]; r--; } else{ if(dir!=2)ans+=x[r]-x[l],dir=2; p[r]+=p[l]; l++; } } printf("%lld\n",ans); }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair <int, ll> ill; int n, p; vector <ill> A, B; ll res; int main() { scanf("%d %d", &n, &p); for (int i = 0; i < n; i++) { int x, c; scanf("%d %d", &x, &c); if (x < p) A.push_back(ill(x, c)); else B.push_back(ill(x, c)); } reverse(A.begin(), A.end()); while (A.size() > 0 && B.size() > 0) { res += B.back().first - A.back().first; if (A.back().second >= B.back().second) while (!B.empty() && A.back().second >= B.back().second) { A.back().second += B.back().second; B.pop_back(); } else while (!A.empty() && A.back().second < B.back().second) { B.back().second += A.back().second; A.pop_back(); } } if (A.empty()) res += B.back().first - p; else res += p - A.back().first; cout << res << endl; return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <bits/stdc++.h> using namespace std; const int maxn = 123456; int x[maxn]; long long p[maxn]; int main(){ int n, s; cin >> n >> s; for(int i = 0; i < n; i++) cin >> x[i] >> p[i]; long long ans = 0; int l = 0, r = n - 1; while(true){ if(s < x[l]){ ans += x[r] - s; break; } else if(s > x[r]){ ans += s - x[l]; break; } else { ans += x[r] - x[l]; bool go_left = p[l] >= p[r]; while(l < r){ if(go_left && p[l] >= p[r]){ p[l] += p[r--]; } else if(!go_left && p[l] < p[r]){ p[r] += p[l++]; } else { break; } } } } 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 to edge[i].v #define mp make_pair #define rint register int #define debug(x) cerr<<#x<<"="<<x<<endl #define fgx cerr<<"-------------"<<endl using namespace std; typedef long long ll; typedef pair<int,int> pii; ll x[100010],p[100010]; int main() { int n,s,l=1,r; ll ans=0; cin>>n>>s; r=n; for(rint i=1;i<=n;i++) scanf("%lld%lld",&x[i],&p[i]); while(l<=r) { if(s<=x[l]) return !printf("%lld\n",ans+x[r]-s); if(s>=x[r]) return !printf("%lld\n",ans+s-x[l]); 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++]; } assert(0); }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include<bits/stdc++.h> #define ll long long using namespace std; struct yu { ll p,v; }a[100001]; int n; ll s,ans,l,r; bool cmp(yu x,yu y) { return x.p<y.p; } int main() { scanf("%d %lld",&n,&s); for (int i=1;i<=n;i++)scanf("%lld %lld",&a[i].p,&a[i].v); sort(a+1,a+1+n,cmp); l=1;r=n; while (r>=l) { if (a[r].p<=s) { ans+=s-a[l].p; break; } if (a[l].p>=s) { ans+=a[r].p-s; break; } ans+=a[r].p-a[l].p; if (a[l].v>=a[r].v) { while (l<r&&a[l].v>=a[r].v)a[l].v+=a[r--].v; } else { while (l<r&&a[r].v>a[l].v)a[r].v+=a[l++].v; } } printf("%lld\n",ans); }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include<bits/stdc++.h> #define PB push_back #define rep(i,a,b) for(int i=(a);i<=(b);++i) using namespace std; const int N=100005; int n,s,x[N]; long long ans,p[N]; vector<int>v; int main(){ scanf("%d%d",&n,&s); rep(i,1,n)scanf("%d%lld",x+i,p+i); int l=1,r=n; while(l<r){ if(x[l]>=s){ l=r; break; } if(x[r]<=s)break; if(p[l]>=p[r])p[l]+=p[r],v.PB(r--); else p[r]+=p[l],v.PB(l++); } v.PB(l); while(v.size())ans+=abs(s-x[v.back()]),s=x[v.back()],v.pop_back(); 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; const int N = 100005; int n, s, x[N]; LL p[N], ans; int dfs(int l, int r) { if (x[r] <= s) return ans += s - x[l], x[l]; if (s <= x[l]) return ans += x[r] - s, x[r]; if (p[l] >= p[r]) { int pos = (p[l] += p[r], dfs(l, r - 1)); return ans += x[r] - pos, x[r]; } else { int pos = (p[r] += p[l], dfs(l + 1, r)); return ans += pos - x[l], x[l]; } } int main() { std::ios::sync_with_stdio(0), std::cin.tie(0); std::cin >> n >> s; for (int i = 0; i < n; ++i) std::cin >> x[i] >> p[i]; dfs(0, n - 1), std::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; typedef long long LL; const int maxn = 1e5 + 10; int X[maxn]; LL P[maxn]; int main() { int N, S; scanf("%d %d", &N, &S); for(int i = 1; i <= N; ++i) scanf("%d %lld", X + i, P + i); int p1 = 1, p2 = N, d = 0; LL ans = 0; while(1){ if(X[p1] >= S) {ans += X[p2] - S; break;} if(X[p2] <= S) {ans += S - X[p1]; break;} if(P[p1] < P[p2]) { if(d != -1) ans += X[p2] - X[p1], d = -1; P[p2] += P[p1], p1++; } else { if(d != 1) ans += X[p2] - X[p1], d = 1; P[p1] += P[p2], p2--; } } 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> using namespace std; long long p[100001],x[100001],ans=0; int n,s,l,r; int main(){ cin>>n>>s; for (int i=1;i<=n;i++) cin>>x[i]>>p[i]; l=1;r=n; while (l<=r){ if (s<=x[l]){ cout<<x[r]-s+ans; return 0; } else if (s>=x[r]){ cout<<ans+s-x[l]; return 0; } //所有住处都在s的一侧,可以直接开过去 if (p[l]>=p[r]){ ans+=x[r]-x[l]; while (l<r&&p[l]>=p[r]) p[l]+=p[r--]; } else{ ans+=x[r]-x[l]; while (l<r&&p[l]<p[r]) p[r]+=p[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<cstdio> #include<cctype> #include<algorithm> using namespace std; const int N=1e5; int n,s,pos[N+1]; long long arr[N+1]; template<class T>T read() { T ret=0; char c=getchar(); while(!isdigit(c)) { c=getchar(); } while(isdigit(c)) { ret=ret*10+c-'0'; c=getchar(); } return ret; } long long solve(int fro,int to,int end) { if(s<=pos[fro]) { return pos[to]-s; } if(s>=pos[to]) { return s-pos[fro]; } if(arr[fro]>=arr[to]) { arr[fro]+=arr[to]; return solve(fro,to-1,fro)+(end==to?pos[to]-pos[fro]:0); } else { arr[to]+=arr[fro]; return solve(fro+1,to,to)+(end==fro?pos[to]-pos[fro]:0); } } int main() { n=read<int>(),s=read<int>(); for(int i=1;i<=n;++i) { pos[i]=read<int>(),arr[i]=read<long long>(); } printf("%lld\n",solve(1,n,arr[1]<arr[n]?1:n)); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include<cstdio> #include<algorithm> #include<iostream> #include<cstring> #include<vector> using namespace std; typedef long long LL; typedef pair<LL,LL> PI; const LL N=100005; LL n,S; LL x[N],p[N]; vector<PI> vec[N]; void link (LL x,LL y,LL c) {vec[x].push_back({y,c});} LL ans[N]; void dfs (LL x) { LL siz=vec[x].size(); for (LL u=0;u<siz;u++) { LL y=vec[x][u].first,z=vec[x][u].second; ans[y]=ans[x]+z; dfs(y); } } int main() { scanf("%lld%lld",&n,&S); for (LL u=1;u<=n;u++) scanf("%lld%lld",&x[u],&p[u]); LL l=1,r=n; while (l<r) { //printf("YES:%lld %lld\n",l,r); if (x[l]>=S) {link(l,l+1,x[l+1]-x[l]);l++;} else if (x[r]<=S) {link(r,r-1,x[r]-x[r-1]);r--;} else if (p[l]>=p[r]) {link(l,r,x[r]-x[l]);p[l]+=p[r];r--;} else {link(r,l,x[r]-x[l]);p[r]+=p[l];l++;} } ans[l]=abs(x[l]-S); dfs(l); printf("%lld\n",max(ans[1],ans[n])); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
from collections import Counter, defaultdict import math import random from decimal import Decimal, ROUND_HALF_UP, ROUND_CEILING from functools import lru_cache, reduce from itertools import combinations_with_replacement, product, combinations def read_int(): return int(input()) def read_int_n(): return list(map(int, input().split())) def read_str(): return input() def read_str_n(): return list(map(str, input().split())) def mt(f): import time import sys def wrap(*args, **kwargs): s = time.time() ret = f(*args, **kwargs) e = time.time() print(e - s, 'sec', file=sys.stderr) return ret return wrap @mt def slv(S, X, P): X_ = [[P[i], [x]] for i, x in enumerate(X)] i = 0 j = len(X_) - 1 # while len(X_) != 1: while i < j: if X_[i][0] >= X_[j][0]: X_[i][0] += X_[j][0] X_[i][1] += X_[j][1] j -= 1 else: X_[j][0] += X_[i][0] X_[j][1] += X_[i][1] i += 1 t = 0 pos = S p0 = S p1 = S for o in X_[i][1]: if p0 < o < p1: continue t += abs(pos - o) if o < pos: p0 = o else: p1 = o pos = o return t def main(): N, S = read_int_n() X = [] P = [] for _ in range(N): x, p = read_int_n() X.append(x) P.append(p) # import random # N = 100000 # S = N / 2 # X = [x + 1 for x in range(N)] # P = [random.randint(1, 1000000000) for _ in range(N)] print(slv(S, X, P)) if __name__ == '__main__': main()
PYTHON3
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=1e5+10; int n,s,x[maxn]; ll p[maxn],ans; deque<int> q; stack<int> stk; int main(){ cin>>n>>s; for(int i=1;i<=n;++i) cin>>x[i]>>p[i]; for(int i=1;i<=n;++i) q.push_back(i); while(true){ if(x[q.front()]>s){ while(!q.empty()){ stk.push(q.back()); q.pop_back(); } break; } if(x[q.back()]<s){ while(!q.empty()){ stk.push(q.front()); q.pop_front(); } break; } if(p[q.front()]>=p[q.back()]){ stk.push(q.back()); p[q.front()]+=p[q.back()]; q.pop_back(); } else{ stk.push(q.front()); p[q.back()]+=p[q.front()]; q.pop_front(); } } for(int now=s;!stk.empty();now=x[stk.top()],stk.pop()) ans+=abs(now-x[stk.top()]); 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=1e5; typedef long long ll; #define mp make_pair ll x[maxn+5],p[maxn+5]; vector<pair<int,ll> > g[maxn+5]; ll ans[maxn+5]; int n,s; void addedge(int u,int v,ll w) { g[u].push_back(mp(v,w)); } void dfs(int k) { for(auto x:g[k]) { ans[x.first]=ans[k]+x.second; dfs(x.first); } } int main() { scanf("%d%d",&n,&s); for(int i=1;i<=n;++i) scanf("%lld%lld",&x[i],&p[i]); int l=1,r=n; while(l<r) { if(x[l]>=s) addedge(l,l+1,x[l+1]-x[l]),++l; else if(x[r]<=s) addedge(r,r-1,x[r]-x[r-1]),--r; else if(p[l]>=p[r]) p[l]+=p[r],addedge(l,r,x[r]-x[l]),--r; else p[r]+=p[l],addedge(r,l,x[r]-x[l]),++l; } ans[l]=abs(x[l]-s); dfs(l); printf("%lld\n",max(ans[1],ans[n])); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <bits/stdc++.h> using namespace std; struct node{ long long x,p; }a[100010]; bool cmp(node a,node b){ return a.x<b.x; } long long n,s,ans; int main(){ scanf("%lld%lld",&n,&s); for (int i=1;i<=n;i++) scanf("%lld%lld",&a[i].x,&a[i].p); sort(a+1,a+n+1,cmp); long long l=n,l1=1; while (l>=l1){ if (a[l].x<=s){ ans+=s-a[l1].x; break; } if (a[l1].x>=s){ ans+=a[l].x-s; break; } ans+=a[l].x-a[l1].x; if (a[l1].p>=a[l].p) while (l>l1 && a[l1].p>=a[l].p) a[l1].p+=a[l].p,l--; else while (l>l1 && a[l].p>a[l1].p) a[l].p+=a[l1].p,l1++; } 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; int n,S,l,r,x[100010],p[100010],la; long long ans,cntl,cntr; int gabs(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 printf("%d\n",x[n]-S),0; if (x[n]<S) return printf("%d\n",S-x[1]),0; l=1,r=n,cntl=cntr=0; if (p[1]<p[n]) l++,cntr+=p[1],la=x[1]; else r--,cntl+=p[n],la=x[n]; while (x[l]<S&&x[r]>S) { if (cntl+p[l]<cntr+p[r]) ans+=gabs(la-x[l]),la=x[l],cntr+=cntl+p[l++],cntl=0; else ans+=gabs(la-x[r]),la=x[r],cntl+=cntr+p[r--],cntr=0; } if (x[l]<S) ans+=gabs(la-x[l])+S-x[l]; else ans+=gabs(la-x[r])+x[r]-S; 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
#include<bits/stdc++.h> using namespace std; long long n, s, x[101000], p[101000], ans; void out(long long x){ cout<<x; exit(0); } int main(){ cin>>n>>s; for (int i=1;i<=n;++i) cin>>x[i]>>p[i]; int l=1, r=n; for (;l<=r;){ if (s<=x[l]) out(ans+x[r]-s); if (s>=x[r]) out(ans+s-x[l]); 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++]; } } out(ans); }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include<iostream> #include<cstdio> #define MN 100000 using namespace std; inline int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n,x[MN+5],pos,S,s,l=1,r; long long p[MN+5],ans; inline int Abs(int x){return x<0?-x:x;} int main() { r=n=read();S=read(); for(int i=1;i<=n;++i) x[i]=read(),p[i]=read(); for(int i=n;;--i) if(x[i]<S) {s=i;break;} for(int i=1;i<=n;++i) if(l<=s&&(r<=s||p[l]<p[r])) { if(i>1) ans+=Abs(pos-x[l]); pos=x[l];p[r]+=p[l];++l; } else { if(i>1) ans+=Abs(pos-x[r]); pos=x[r];p[l]+=p[r];--r; } cout<<ans+Abs(pos-S); return 0; }
CPP
p03366 AtCoder Grand Contest 023 - Go Home
There are N apartments along a number line, numbered 1 through N. Apartment i is located at coordinate X_i. Also, the office of AtCoder Inc. is located at coordinate S. Every employee of AtCoder lives in one of the N apartments. There are P_i employees who are living in Apartment i. All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate S with all the employees and move according to the following rule: * Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of 1 in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off. * Repeat the operation above as long as there is one or more employees on the bus. For a specific example, see Sample Input 1. The bus takes one seconds to travel a distance of 1. The time required to vote and get off the bus is ignorable. Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction. Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time. Constraints * 1 \leq N \leq 10^5 * 1 \leq S \leq 10^9 * 1 \leq X_1 < X_2 < ... < X_N \leq 10^9 * X_i \neq S ( 1 \leq i \leq N ) * 1 \leq P_i \leq 10^9 ( 1 \leq i \leq N ) * All values in input are integers. Input Input is given from Standard Input in the following format: N S X_1 P_1 X_2 P_2 : X_N P_N Output Print the number of seconds the bus will take from departure to arrival at the last employees' apartment. Examples Input 3 2 1 3 3 4 4 2 Output 4 Input 6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 Output 21 Input 15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 Output 2397671583
6
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 100005; int x[MAXN], wt[MAXN]; int main(int argc, char const *argv[]) { int n, s; cin>>n>>s; for (int i = 0; i < n; ++i) { cin>>x[i]>>wt[i]; } int lptr = 0, rptr = n - 1; long long int early_l = 0, early_r = 0; vector <int> A; while(lptr < n && x[lptr] < s && rptr >= 0 && x[rptr] > s) { if(early_l + wt[lptr] >= early_r + wt[rptr]) { early_l+=(early_r + wt[rptr]); early_r = 0; A.push_back(rptr); rptr--; } else { early_r+=(early_l + wt[lptr]); early_l = 0; A.push_back(lptr); lptr++; } } while(lptr < n && x[lptr] < s) { A.push_back(lptr); lptr++; } while(rptr >= 0 && x[rptr] > s) { A.push_back(rptr); rptr--; } long long int ans = 0; for (int i = n - 1; i >= 0; --i) { ans+=abs(s - x[A[i]]); s = x[A[i]]; } cout<<ans<<"\n"; return 0; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include <bits/stdc++.h> using namespace std ; const int N = 5010 ; const long long inf = 10000000000000000 ; pair<int,int> zab [ N ] ; bool cmp ( pair<int,int> A, pair<int,int> B ) { return A.first + A.second < B.first + B.second ; } long long dp [ N ][ 3 ]; int main() { // freopen("input.txt", "r", stdin) ; int n ; cin >> n ; for ( int i = 0 ; i < n ; ++i ) cin >> zab[i].first >> zab[i].second ; sort ( zab, zab+n, cmp ) ; int ans =0 ; for ( int i = 1 ; i <= n ; ++i ) { for ( int j = 0 ; j < n ; ++j ) { if ( i == 1 ) dp[j][i%2] = zab[j].first ; else if ( j + i <= n ) dp[j][i%2] = min ( 1ll*zab[j].first, dp[j+1][(i-1)%2] - zab[j].second ); else dp[j][i%2] = -inf ; } bool kPosible = (dp[n-1][i%2] >= 0ll) ; for ( int j = n-2 ; j >= 0 ; --j ) { dp[j][i%2] = max ( dp[j][i%2], dp[j+1][i%2] ) ; kPosible |= (dp[j][i%2]>=0ll) ; } if ( kPosible ) ans = i ; } cout << ans << '\n' ; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include <bits/stdc++.h> using namespace std; const int N = 5 * 1000 + 17; const int MOD = 2 * 1000 * 1000 * 1000 + 7; struct item { int h, p; item() = default; }; int n; item a[N]; long long dp[N][N]; bool read() { if (!(cin >> n)) return false; for (int i = 0; i < n; ++i) cin >> a[i].h >> a[i].p; return true; } void solve() { sort(a, a + n, [](item a, item b) { return a.h + a.p < b.h + b.p; }); for (int i = 0; i < n; ++i) for (int j = 0; j <= n; ++j) dp[i][j] = MOD; dp[0][0] = 0; dp[0][1] = a[0].p; for (int i = 1; i < n; ++i) for (int j = 0; j <= i + 1; ++j) { dp[i][j] = dp[i - 1][j]; if (j > 0 && dp[i - 1][j - 1] <= a[i].h) dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + a[i].p); } auto ans = 0; for (int i = 0; i <= n; ++i) if (dp[n - 1][i] < MOD) ans = i; cout << ans << endl; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); while (read()) solve(); return 0; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin >> n; int h[n], p[n]; vector<int> v; for (int i = 0; i < n; i++) { v.emplace_back(i); cin >> h[i] >> p[i]; } auto cmp = [&](int i, int j) { if (h[i] + p[i] != h[j] + p[j]) return h[i] + p[i] < h[j] + p[j]; return h[i] < h[j]; }; sort(v.begin(), v.end(), cmp); vector<long long> dp(n+1, 1e18); dp[0] = 0; for (int i = 0; i < n; i++) { int j = v[i]; for (int k = n-1; k >= 0; k--) { if (dp[k] > h[j]) continue; dp[k+1] = min(dp[k+1], dp[k]+p[j]); } } for (int i = n; i >= 0; i--) { if (dp[i] != 1e18) { cout << i << "\n"; return 0; } } return 0; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include <bits/stdc++.h> using namespace std; long long n,f[5005]; struct eming { long long a,b; }; eming e[5005]; long long cmp(eming x,eming y) { return (x.a+x.b<y.a+y.b)?1:0; } int main() { scanf("%lld",&n); for(long long i=1;i<=n;i++) { f[i]=99999999999; scanf("%lld %lld",&e[i].a,&e[i].b); } sort(e+1,e+n+1,cmp); for(long long i=1;i<=n;i++) { for(long long j=n;j>=1;j--) { if(f[j]>f[j-1]+e[i].b&&e[i].a>=f[j-1]) { f[j]=f[j-1]+e[i].b; } } } for(long long i=n;i>=0;i--) { if(f[i]<9999999999) { printf("%lld\n",i); return 0; } } return 0; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include <iostream> #include <utility> #include <algorithm> typedef long long ll; using namespace std; ll H[5010], P[5010], dp[5010], lmax=1e12; pair<pair<ll, ll>, int> pr[5010]; int main() { int N; cin >> N; for(int i=0; i<N; ++i){ cin >> H[i] >> P[i]; pr[i]=make_pair(make_pair(H[i]+P[i], P[i]), i); } sort(pr, pr+N); dp[0]=0; for(int i=1; i<=N+1; ++i) dp[i]=lmax; for(int i=0; i<N; ++i){ int t=pr[i].second; for(int j=i; j>=0; --j){ if(dp[j]<=H[t]){ dp[j+1]=min(dp[j+1], dp[j]+P[t]); } } } int ans=0; while(dp[ans+1]<lmax) ++ans; cout << ans << endl; return 0; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> using namespace std; typedef long long LL; const LL INF = 0x3f3f3f3f3f3f3f3f; struct node{ LL h,p; }s[5005]; bool cmp(node x,node y){ return x.h + x.p < y.h + y.p; } LL n; LL dp[5005]; int main(){ cin >> n; for(LL i = 1;i <= n;i ++) cin >> s[i].h >> s[i].p; sort(s + 1,s + 1 + n,cmp); memset(dp,INF,sizeof(dp)); dp[0] = 0; for(LL i = 1;i <= n;i ++){ for(LL j = i;j >= 1;j --){ if(dp[j - 1] <= s[i].h) dp[j] = min(dp[j],dp[j - 1] + s[i].p); } } for(LL i = n;i >= 0;i --){ if(dp[i] < INF){ cout << i << endl; break; } } return 0; } //
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
N = int(input()) men = [] for _ in range(N): H,P = map(int,input().split()) men.append((H,P,H+P)) men.sort(key=lambda x: x[2]) maxH = max(men)[0] inf = maxH+1 #dp[i][j] -> i人目までみたとき、j人が座布団を積む場合の最小枚数 dp = [[-1]*(N+1) for _ in range(N+1)] dp[0][0] = 0 for i in range(1,N+1): h,p,a = men[i-1] for j in range(1,N+1): if 0 <= dp[i-1][j-1] <= h: if dp[i-1][j] == -1: dp[i][j] = dp[i-1][j-1]+p else: dp[i][j] = min(dp[i-1][j],dp[i-1][j-1]+p) else: dp[i][j] = dp[i-1][j] for j in range(N,-1,-1): if dp[-1][j] != -1: print(j) break
PYTHON3
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> using namespace std; const int N=5010; int a[N],b[N],v[N],id[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; } inline bool cmp(const int &x,const int &y) {return b[x]+a[x]<b[y]+a[y];} int main() { int n,tp=0; cin>>n; for(int i=1;i<=n;i++) a[i]=gi(),b[i]=gi(),id[i]=i; sort(id+1,id+1+n,cmp); for(int i=1;i<=n;i++) { int t=id[i]; if(v[tp]<=a[t]) ++tp,v[tp]=v[tp-1]+b[t]; for(int j=tp;~j;j--) if(v[j]<=a[t]) v[j+1]=min(v[j+1],v[j]+b[t]); if(v[tp+1]) ++tp; } cout<<tp; return 0; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include <bits/stdc++.h> #define int long long #define rep(i,n) for(int i=0;i<(int)(n);i++) #define rep1(i,n) for(int i=1;i<=(int)(n);i++) #define all(c) c.begin(),c.end() #define pb push_back #define fs first #define sc second #define show(x) cout << #x << " = " << x << endl #define chmin(x,y) x=min(x,y) #define chmax(x,y) x=max(x,y) using namespace std; template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){return o<<"("<<p.fs<<","<<p.sc<<")";} template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){o<<"sz = "<<vc.size()<<endl<<"[";for(const T& v:vc) o<<v<<",";o<<"]";return o;} typedef pair<int,int> P; int N; vector<P> ps; int dp[5010][5010]; int inf = 1e18; signed main(){ rep(i,5010) rep(j,5010) dp[i][j] = inf; cin>>N; rep(i,N){ int a,b; cin>>b>>a; ps.pb(P(b+a,a)); } N = ps.size(); sort(all(ps)); dp[0][0] = 0; rep(i,N) rep(j,i+1) if(dp[i][j]!=inf){ int a = ps[i].sc; int b = ps[i].fs; chmin(dp[i+1][j],dp[i][j]); if(dp[i][j]+a<=b) chmin(dp[i+1][j+1],dp[i][j]+a); } int ans = 0; rep(j,N+1) if(dp[N][j]!=inf) ans = j; cout<<ans<<endl; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include <algorithm> #include <iostream> using namespace std; typedef long long ll; typedef pair<ll, ll> P; const ll INF = 10000000000000000; bool comp(const P &a, const P &b){ if(a.first - b.first != b.second - a.second) return a.first - b.first < b.second - a.second; return a.first < b.first; } ll dp[5002][5002]; int main() { int n; cin >> n; P q[5003]; for(int i = 0; i < n; i++){ ll h, p; cin >> h >> p; q[i] = P(p, h); } sort(q, q + n, comp); for(int j = 1; j <= n; j++) dp[0][j] = INF; for(int i = 1; i <= n; i++){ for(int j = 0; j <= n; j++) dp[i][j] = dp[i - 1][j]; for(int j = 1; j <= n; j++){ if(dp[i - 1][j - 1] <= q[i - 1].second) dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + q[i - 1].first); } } for(int j = n; j >= 0; j--){ if(dp[n][j] != INF){ cout << j << endl; return 0; } } }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include<bits/stdc++.h> #define rint register int #define rep(i,a,b) for (rint i=(a),_E=(b); i<=_E; ++i) #define per(i,a,b) for (rint i=(a),_E=(b); i>=_E; --i) #define REP(i,n) for (rint i=(0),_E=(n); i<_E; ++i) #define fi first #define se second #define pb push_back #define mp make_pair using namespace std; typedef pair<int,int> pii; typedef vector<int> vi; typedef long long ll; const int N = 100005; priority_queue<int> Q; int n, res; pii a[N]; ll now; int main() { scanf("%d", &n); rep (i, 1, n) { scanf("%d%d", &a[i].fi, &a[i].se); a[i].fi += a[i].se; } sort(a + 1, a + n + 1); rep (i, 1, n) { now += a[i].se; Q.push(a[i].se); if (now > a[i].fi) { now -= Q.top(); Q.pop(); } } cout << Q.size() << endl; return 0; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#!/usr/bin/env python from collections import deque import itertools as it import sys import math sys.setrecursionlimit(10000000) INF = 10 ** 18 N = input() ls = [] for i in range(N): H, P = map(int, raw_input().split()) ls.append((H + P, H, P)) ls.sort() DP = [INF] * (N + 1) DP[0] = 0 for i in range(N): _, H, P = ls[i] for j in range(i, -1, -1): if DP[j] <= H and DP[j + 1] > DP[j] + P: DP[j + 1] = DP[j] + P ans = 0 for i in range(N + 1): if DP[i] < INF: ans = i print ans
PYTHON
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include <bits/stdc++.h> #define x first #define y second using namespace std; using i64 = long long; using pii = pair<int, int>; const i64 INF = 0x3f3f3f3f3f3f3f3fLL; const int N = 5005; i64 dp[N]; vector<pii> v; int n, ans = 0; int main() { #ifdef HOME freopen("zabuton.in", "r", stdin); freopen("zabuton.out", "w", stdout); #endif ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> n; v.resize(n); for (auto &i: v) cin >> i.x >> i.y; memset(dp, 0x3f, sizeof dp); sort(begin(v), end(v), [&](const pii &a, const pii &b) { return a.x + a.y < b.x + b.y; }); dp[0] = 0; for (auto i: v) { for (int j = N - 2; j >= 0; --j) { if (dp[j] <= i.x) { dp[j + 1] = min(dp[j + 1], dp[j] + i.y); ans = max(ans, j + 1); } } } cout << ans << endl; return 0; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include<bits/stdc++.h> using namespace std; #define fs first #define sc second #define pb push_back #define mp make_pair #define eb emplace_back #define ALL(A) A.begin(),A.end() #define RALL(A) A.rbegin(),A.rend() typedef long long LL; typedef pair<int,int> P; const LL mod=1000000007; const LL LINF=1LL<<60; const int INF=1<<30; int dx[]={1,0,-1,0}; int dy[]={0,-1,0,1}; int main(){ int n;cin >> n; vector<P> hp(n); for (int i = 0; i < n; i++) { LL h,p;cin >> h >> p; hp[i] = mp(h+p,p); } sort(ALL(hp)); vector<LL> dp(n+1,LINF); dp[0] = 0; for (int i = 0; i < n; i++) { for (int j = i+1; j > 0; j--) { if(dp[j-1] <= hp[i].fs - hp[i].sc) dp[j] = min(dp[j], dp[j-1] + hp[i].sc); } } int ans = 0; for (int i = 0; i <= n; i++) { if(dp[i] != LINF) ans = i; } cout << ans << endl; return 0; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include <bits/stdc++.h> #define int long long using namespace std; typedef pair<int, int> P; const int INF = 1e15; int dp[5010], nxt[5010]; signed main(){ int n; cin >> n; vector<int> h(n), p(n); vector<P> d(n); for(int i = 0; i < n; i++){ cin >> h[i] >> p[i]; d[i] = {h[i] + p[i], i}; } sort(d.begin(), d.end()); for(int i = 0; i <= n; i++) dp[i] = INF; dp[0] = 0; for(int i = 0; i < n; i++){ for(int j = 0; j <= n; j++) nxt[j] = dp[j]; for(int j = 0; j < n; j++){ if(dp[j] == INF) continue; int id = d[i].second; if(dp[j] <= h[id]){ nxt[j + 1] = min(nxt[j + 1], dp[j] + p[id]); } } swap(dp, nxt); } int ans = 0; for(int i = 0; i <= n; i++){ if(dp[i] != INF) ans = i; } cout << ans << endl; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
import sys readline = sys.stdin.readline N = int(readline()) HP = [tuple(map(int, readline().split())) for _ in range(N)] HP.sort(key = lambda x: x[0] + x[1]) INF = 1<<64 dp = [0] for i in range(N): hi, pi = HP[i] dp += [INF] for j in range(i+1, 0, -1): if dp[j-1] > hi: continue dp[j] = min(dp[j], dp[j-1]+pi) for ans in range(N+1): if dp[ans] == INF: ans -= 1 break print(ans)
PYTHON3
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include <bits/stdc++.h> using namespace std; class Obj{ public: int h, p; Obj (int a, int b){ h = a; p = b; } bool operator<(const Obj &o) const{ return 1LL* (long long)h + (long long)p < 1LL*(long long)o.h + (long long)o.p; } }; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); vector<Obj> li; int n; cin >> n; for(int i = 0; i<n; i++){ int a, b; cin >> a >> b; li.push_back(Obj(a,b)); } sort(li.begin(),li.end()); int best[n+1]; int inf = 2000000069; for(int i = 0; i<=n; i++){ best[i] = inf; } best[0] = 0; for(int i = 0; i<n; i++){ for(int j = n; j>0; j--){ if(best[j-1]<=li[i].h){ best[j] = min(best[j],best[j-1]+li[i].p); } } } for(int i = n; i>=0; i--){ if(best[i]!=inf){ cout << i << endl; return 0; } } }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #define LL long long using namespace std; const int N = 5010; const LL inf = 0x3f3f3f3f3f3f3f3f; struct node{ LL s, w; }f[N]; LL dp[N]; int cmp(const node&a, const node&b) { return a.s+a.w < b.s+b.w; } int main() { int n; scanf("%d", &n); for(int i = 1; i <= n; i++) scanf("%lld%lld", &f[i].s, &f[i].w); sort(f+1, f+n+1, cmp); memset(dp, 0x3f, sizeof(dp)); dp[0] = 0; for(int i = 1; i <= n; i++) for(int j = n; j; j--) { if(f[i].s >= dp[j-1]) dp[j] = min(dp[j], dp[j-1]+f[i].w); } int ans = 0; for(int i = 1; i <= n; i++) if(dp[i] < inf) ans = i; printf("%d\n", ans); }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include <stdio.h> #include <algorithm> #include <vector> using namespace std; int main() { vector<long long> res(1,0); vector<pair<int, int> > u; int N; scanf ("%d",&N); while (N--){ int h,p; scanf ("%d %d",&h,&p); u.push_back({h,p}); } sort(u.begin(),u.end(),[](const pair<int, int> &a, const pair<int, int> &b){return a.first+a.second<b.first+b.second;}); for (auto &v : u){ int h = v.first, p = v.second; for (int i=(int)res.size()-1;i>=0;i--){ if (res[i] <= h){ if (i + 1 == res.size()) res.push_back(res[i] + p); else if (res[i+1] > res[i] + p) res[i+1] = res[i] + p; } } } printf ("%d\n",res.size()-1); return 0; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll,ll> Pair; int N; ll H[5000], P[5000]; ll sumP=0; ll minH=1e9; ll dp[5001][5001]; int main(){ cin>>N; for(int i=0;i<N;i++){ cin>>H[i]>>P[i]; minH=min(minH,H[i]); sumP+=P[i]; } ll tmp = minH / sumP * sumP; if(tmp>0){ cout<<N<<endl; return 0; } for(int i=0;i<=N;i++) for(int j=0;j<=N;j++) dp[i][j]=(1LL<<60); dp[0][0]=0; vector< Pair > vec; for(int i=0;i<N;i++){ vec.push_back( Pair( H[i]+P[i] , i ) ); } sort(vec.begin(),vec.end()); for(int i=0;i<N;i++){ int id=vec[i].second; ll h=H[id]; ll p=P[id]; for(int j=0;j<N;j++){ if(dp[i][j] == (1LL<<60) )continue; if(dp[i][j]<=h)dp[i+1][j+1]=min(dp[i+1][j+1],dp[i][j]+p); dp[i+1][j]=min(dp[i+1][j] , dp[i][j]); } } ll ans=0; for(int i=0;i<=N;i++) for(int j=0;j<=N;j++) if(dp[i][j]!=(1LL<<60)) ans=max(ans,(ll)j); cout<< ans <<endl; return 0; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll INF = 1LL << 50; int n; vector<pair<ll, ll>> v(5010); ll dp[5010][5010]; int main(){ cin >> n; v.resize(n); for (int i = 0; i < n; i++){ cin >> v[i].first >> v[i].second; } sort(v.begin(), v.end(), [](pair<ll, ll> a, pair<ll, ll> b){ return a.first + a.second < b.first + b.second; }); /* for (auto a : v){ cout << a.first << " " << a.second << endl;; } */ for (int i = 0; i <= n; i++){ for (int j = 1; j <= n; j++){ dp[i][j] = INF; } } int res = 0; for (int i = 1; i <= n; i++){ for (int j = 1; j <= n; j++){ if (dp[i-1][j-1] > v[i-1].first){ dp[i][j] = dp[i-1][j]; } else { dp[i][j] = min(dp[i-1][j], dp[i-1][j-1] + v[i-1].second); } } } for (int j = 0; j <= n; j++){ //cout << dp[n][j] << endl; if (dp[n][j] < INF) res = j; } cout << res << endl; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include <bits/stdc++.h> using namespace std; #define MOD @ #define ADD(X,Y) ((X) = ((X) + (Y)) % MOD) typedef long long i64; typedef vector<int> ivec; typedef vector<string> svec; const i64 INFT = 1LL << 60LL; int N; i64 H[5050], P[5050]; i64 dp[5050][5050]; void chmin(i64& a, i64 b) { a = min(a, b); } int main() { scanf("%d", &N); for (int i = 0; i < N; ++i) scanf("%lld%lld", H + i, P + i); vector<pair<i64, pair<i64, i64> > > ppl; for (int i = 0; i < N; ++i) ppl.push_back({ H[i] + P[i], {H[i], P[i]} }); sort(ppl.begin(), ppl.end()); for (int i = 0; i < N; ++i) { H[i] = ppl[i].second.first; P[i] = ppl[i].second.second; } for (int i = 0; i <= N; ++i) { for (int j = 0; j <= N; ++j) dp[i][j] = INFT; } dp[0][0] = 0; for (int i = 0; i < N; ++i) { for (int j = 0; j <= i; ++j) { chmin(dp[i + 1][j], dp[i][j]); if (dp[i][j] <= H[i]) chmin(dp[i + 1][j + 1], dp[i][j] + P[i]); } } for (int i = N; i >= 0; --i) { if (dp[N][i] < (1LL << 40LL)) { printf("%d\n", i); return 0; } } return 0; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; const ll inf = 1e18; pii p[5005]; ll dp[5005]; int main() { ios::sync_with_stdio(0), cin.tie(0); int n; cin >> n; for (int i = 1; i <= n; ++i) cin >> p[i].first >> p[i].second; sort(p + 1, p + n + 1, [&] (pii a, pii b) { return a.first + a.second < b.first + b.second; }); for (int i = 1; i <= n; ++i) dp[i] = inf; for (int i = 1; i <= n; ++i) for (int j = n; j >= 1; --j) if (dp[j - 1] <= p[i].first) dp[j] = min(dp[j], dp[j - 1] + p[i].second); int sol = 0; for (int i = 1; i <= n; ++i) if (dp[i] != inf) sol = i; cout << sol << '\n'; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include <bits/stdc++.h> using namespace std; typedef long long LL; struct node { LL S,W; }; bool comp(node a,node b) { return a.S - b.W > b.S - a.W; } int main() { LL n; cin>>n; vector<node> stones(n); for(LL i=0;i<n;i++) { cin>>stones[i].S>>stones[i].W; } sort(stones.begin(), stones.end(), comp); vector<LL> dp(n+1,-1e18); for(int j=0;j<n;j++) { for(int i = n;i>=0;i--) { if(i!=1 && dp[i-1]>=stones[j].W) { dp[i] = max(dp[i],min(stones[j].S,dp[i-1]-stones[j].W)); } else if(i==1) dp[i] = max(dp[i],stones[j].S); } } LL ans = 0; for(int i=1;i<=n;i++) { if(dp[i]<0) break; ans++; } cout<<ans<<endl; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
N = int(input()) M = [] for _ in range(N): h, p = map(int, input().split()) M.append((h, p)) M = sorted(M, key=lambda x: x[0] + x[1]) dp = [10 ** 9 + 1] * (N+1) dp[0] = 0 ans = 0 for h, p in M: for i in range(N-1, -1, -1): if dp[i] <= h: dp[i+1] = min(dp[i+1], dp[i] + p) ans = max(ans, i+1) print(ans)
PYTHON3
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
from bisect import* N = int(input()) P = [] for i in range(N): h, p = map(int, input().split()) P.append((h+p, h, p)) P.sort() dp = [10**18]*(N+1) dp[0] = 0 for i in range(N): k, h, p = P[i] idx = bisect(dp, h) for j in range(idx-1, -1, -1): dp[j+1] = min(dp[j]+p, dp[j+1]) #print(idx) #print(dp[:idx+1]) print(bisect(dp, 10**17)-1)
PYTHON3
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include <bits/stdc++.h> #define int long long #define ri register int #define N 5005 using namespace std; struct node { int h,p; } a[N]= {0}; int n,ans,f[N]= {0}; template <typename T> inline void read(T &x) { T c=getchar(),f=0; for (; c<48||57<c; c=getchar()) if (c=='-') f=1; for (x=0; 48<=c&&c<=57; c=getchar()) x=(x<<3)+(x<<1)+(c^48); if (f) x=-x; } template <typename T> inline void print(T x) { if (x<0) x=-x,putchar('-'); if (x>9) print(x/10); putchar(x%10+48); } inline int cmp(node a,node b) { return (a.h+a.p)<(b.h+b.p); } signed main() { read(n); for (ri i=1; i<=n; i++) read(a[i].h),read(a[i].p); for (ri i=1; i<=n; i++) f[i]=1e18; sort(a+1,a+1+n,cmp),f[0]=0; for (ri i=1; i<=n; i++) for (ri j=i; j; j--) if (f[j-1]<=a[i].h) f[j]=min(f[j],f[j-1]+a[i].p); for (ri i=0; i<=n; i++) if (f[i]<1e18) ans=i; print(ans); return 0; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
// https://cf17-final.contest.atcoder.jp/tasks/cf17_final_d #include <bits/stdc++.h> using namespace std; int n, h, p; long long dp[5050], tmp[5050]; vector<pair<int, int>> v; bool cmp(pair<int, int> a, pair<int, int> b) { return (a.first + a.second < b.first + b.second); } int main() { scanf("%d", &n); for(int i = 0; i < n; i++) { scanf("%d %d", &h, &p); v.emplace_back(h, p); } sort(v.begin(), v.end(), cmp); for(int i = 1; i < 5050; i++) dp[i] = 10000000000000000LL; for(int i = 0; i < n; i++) { memcpy(tmp, dp, sizeof dp); for(int j = 0; j <= i; j++) { if(v[i].first >= tmp[j]) dp[j+1] = min(dp[j+1], tmp[j] + v[i].second); } } for(int i = n; i >= 0; i--) { if(dp[i] < 10000000000000000LL) { printf("%d\n", i); return 0; } } return 0; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include<bits/stdc++.h> using namespace std; #define int long long #define inf 10000000000000 struct player { int h, p; }; int n; player a[5001]; int dp[5001][5001]; bool comp(player p1, player p2) { return min(p1.h, p2.h - p1.p) > min(p2.h, p1.h - p2.p); } void read() { cin>>n; for(int i = 1; i<=n; i++) { cin>>a[i].h>>a[i].p; } } void solve() { sort(a + 1, a + n + 1, comp); for(int i=0;i<=n;i++) for(int j = 0; j<=n; j++)dp[i][j] = inf; int ans = 1; dp[1][1] = a[1].p; for(int i = 2; i<=n; i++) { for(int j = 1;j <= i; j++) { if(dp[i - 1][j - 1] <= a[i].h) { ans = max(ans, j); dp[i][j] = dp[i - 1][j - 1] + a[i].p; } dp[i][j] = min(dp[i][j], dp[i - 1][j]); } } cout<<ans<<'\n'; } signed main() { read(); solve(); }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include <bits/stdc++.h> using namespace std; #define mod 998244353 #define MOD 1000000007 #define inf 0x3f3f3f3f #define linf 0x3f3f3f3f3f3f3f3fll typedef long long ll; typedef pair<int,int> pii; typedef unsigned long long ull; int n; ll H[5050],P[5050]; vector<pair<ll,int> > od; ll dp[5050][5050]; int main() { scanf("%d",&n); for(int i=0;i<n;i++)scanf("%lld%lld",&H[i],&P[i]); for(int i=0;i<n;i++)od.push_back(make_pair(H[i]+P[i],i)); sort(od.begin(),od.end()); memset(dp,0x3f,sizeof(dp)); dp[0][0]=0; for(int i=0;i<n;i++) { for(int j=0;j<=i;j++) { if(dp[i][j]==linf)break; dp[i+1][j]=min(dp[i+1][j],dp[i][j]); if(dp[i][j]<=H[od[i].second]) { dp[i+1][j+1]=min(dp[i+1][j+1],dp[i][j]+P[od[i].second]); } } } for(int i=n;i>=0;i--) { if(dp[n][i]<linf) { printf("%d\n",i); return 0; } } }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define all(x) (x).begin(),(x).end() const int mod=1000000007,MAX=1<<19; const ll INF=1LL<<60; bool compare(pair<ll,ll> a,pair<ll,ll> b){ if(a.first+a.second==b.first+b.second) return a.first>b.first; return a.first+a.second<b.first+b.second; } int main(){ int N;cin>>N; vector<pair<ll,ll>> A(N); for(int i=0;i<N;i++) cin>>A[i].first>>A[i].second; sort(all(A),compare); vector<vector<ll>> dp(N+3,vector<ll>(N+3,INF)); dp[0][0]=0; for(int i=0;i<N;i++){ for(int j=0;j<=i;j++){ dp[i+1][j]=min(dp[i+1][j],dp[i][j]); if(dp[i][j]<=A[i].first) dp[i+1][j+1]=min(dp[i+1][j+1],dp[i][j]+A[i].second); } } for(int j=0;j<=N+2;j++){ if(dp[N][j]==INF){ cout<<j-1<<endl; return 0; } } }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> ii; bool comp(pair<int, int> a, pair<int, int> b) { return a.first + a.second < b.first + b.second; } const long long INF = 1e18 + 2; int main() { int n; cin >> n; ii arr[n]; for(int i = 0; i < n; ++i) { cin >> arr[i].first >> arr[i].second; } sort(arr, arr + n, comp); vector<long long> dp(n + 2,INF); dp[0] = 0; for(int i = 0; i < n; ++i) { for(int j = n; j >= 0; --j) { if(dp[j] <= arr[i].first) { dp[j + 1] = min(dp[j+1], dp[j] + arr[i].second); } } } int ans = 1; for(int i = n; i >= 0; --i) { if(dp[i] < INF) { ans = i; break; } } cout << ans; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include<bits/stdc++.h> using namespace std; #define fi first #define se second #define FOR(a, b, c) for(int a = b; a <= c; ++a) #define int long long const int N = 1e5 + 10; int n; struct book { int h, p; }t[N]; bool cmp(const book x, const book y) { return (x.h + x.p) < (y.h + y.p); } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; FOR(i, 1, n) { cin >> t[i].h >> t[i].p; } sort(t + 1, t + n + 1, cmp); priority_queue<int> pq; int sum = 0ll; FOR(i, 1, n) { sum += t[i].p; pq.push(t[i].p); while(!pq.empty() && sum > t[i].h + t[i].p) { sum -= pq.top(); pq.pop(); } } cout << pq.size(); }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include <bits/stdc++.h> typedef long long ll; const ll MOD = 1000000007; using namespace std; bool comp(const pair<int, int> &a, const pair<int, int> &b) { return ((ll)a.first + a.second) < ((ll)b.first + b.second); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<pair<int, int> > v(n); for (int i = 0; i < n; i++) { cin >> v[i].first >> v[i].second; } sort(v.begin(), v.end(), comp); vector<vector<ll> > dp(n + 1, vector<ll>(n + 1, LLONG_MAX)); int ans = 0; dp[0][0] = 0; for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]); if (v[i].first >= dp[i][j]) { dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + (ll)v[i].second); ans = max(ans, j + 1); } } } cout << ans; return 0; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define pb push_back #define mp make_pair typedef pair<long long, long long> ii; vector<pair<long long, ii>> ans; long long sum = 0, n; priority_queue<int> pq; int main() { cin.tie(0), ios::sync_with_stdio(0); cin >> n; while(n--){ int h, p; cin >> h >> p; ans.pb({h + p, {h, p}}); } sort(ans.begin(), ans.end()); for(int i = 0; i < ans.size(); i++){ pq.push(ans[i].se.se); sum += ans[i].se.se; if(sum > ans[i].fi){ while(sum > ans[i].fi && !pq.empty()){ sum -= pq.top(); pq.pop(); } } } cout << pq.size() << "\n"; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll n , dp[5005][5005]; pair<ll,ll> a[5005]; bool cmp(pair<ll,ll> x,pair<ll,ll> y){ return x.first+x.second < y.first+y.second; } int main(){ ios_base:: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin>>n; for(ll i=0;i<n;i++)cin>>a[i].first>>a[i].second; sort(a,a+n,cmp); memset(dp,0x3f,sizeof(dp)); dp[0][0]=0; for(ll i=0;i<n;i++){ for(ll j=0;j<=i;j++){ if(dp[i][j]<=a[i].first) dp[i+1][j+1]=min(dp[i+1][j+1] , dp[i][j] + a[i].second); dp[i+1][j]=min(dp[i+1][j],dp[i][j]); } } for(ll i=n;i>=0;i--)if(dp[n][i]<(1e16))return cout<<i , 0; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include <bits/stdc++.h> using namespace std; using ll = long long; using ii = pair<int, int>; using vi = vector<int>; #define all(v) begin(v), end(v) const int N = 5003; struct dt { int s, w; bool operator < (const dt& o) const { return s+w < o.s+o.w; } } a[N]; int n; ll dp[N][N]; int main(int argc, char const *argv[]) { #ifdef LOCAL freopen("in", "r", stdin); #endif memset(dp, 63, sizeof dp); cin >> n; for(int i = 1; i <= n; ++i) { scanf("%d %d", &a[i].s, &a[i].w); } sort(a+1, a+1+n); dp[0][0] = 0; // ofstream out; for(int i = 1; i <= n; ++i) { // out.open("out_" + to_string(i)); for(int j = 0; j <= n; ++j) { dp[i][j] = dp[i-1][j]; if(j && dp[i-1][j-1] <= a[i].s) { dp[i][j] = min(dp[i-1][j-1] + a[i].w, dp[i][j]); } // if(dp[i][j] <= 1000000) out << j << ' ' << dp[i][j] << endl; } // out.close(); } int ans = 0; for(int i = 1; i <= n; ++i) { if(dp[n][i] != 4557430888798830399LL) { ans = i; } } cout << ans << endl; return 0; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
import java.io.BufferedReader; import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; import static java.util.Comparator.comparingLong; public class Main implements Closeable { private InputReader in = new InputReader(System.in); private PrintWriter out = new PrintWriter(System.out); private class Participant { private long height, power; private Participant(long height, long power) { this.height = height; this.power = power; } } public void solve() { int n = in.ni(); Participant[] participants = new Participant[n]; for (int i = 0; i < n; i++) { participants[i] = new Participant(in.nl(), in.nl()); } Arrays.sort(participants, comparingLong(x -> x.height + x.power)); long[][] dp = new long[2][n + 1]; for (int i = 0; i < 2; i++) { for (int j = 0; j <= n; j++) { dp[i][j] = Long.MAX_VALUE; } } dp[0][0] = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (dp[0][j] <= participants[i].height) { dp[1][j + 1] = Math.min(dp[1][j + 1], dp[0][j] + participants[i].power); } dp[1][j] = Math.min(dp[1][j], dp[0][j]); } dp[1][n] = Math.min(dp[0][n], dp[1][n]); long[] tmp = dp[0]; Arrays.fill(tmp, Long.MAX_VALUE); dp[0] = dp[1]; dp[1] = tmp; } for (int i = n; i >= 0; i--) { if (dp[0][i] != Long.MAX_VALUE) { out.println(i); return; } } } @Override public void close() throws IOException { in.close(); out.close(); } 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 ni() { return Integer.parseInt(next()); } public long nl() { return Long.parseLong(next()); } public void close() throws IOException { reader.close(); } } public static void main(String[] args) throws IOException { try (Main instance = new Main()) { instance.solve(); } } }
JAVA
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cstdlib> #include <ctime> #include <vector> #include <cmath> #include <iomanip> #include <fstream> using namespace std; typedef long long ll; const int MAXN = 250005; const ll INF = (1ll << 60) - 1; int n; ll MIN[MAXN]; pair<ll,ll> a[MAXN]; bool cmp(const pair<ll,ll> &A,const pair<ll,ll> &B) { return A.first + A.second < B.first + B.second; } int main() { scanf("%d",&n); for (int i = 1;i <= n;i++) scanf("%d%d",&a[i].first,&a[i].second); sort(a + 1,a + n + 1,cmp); for (int i = 1;i <= n;i++) MIN[i] = INF; for (int i = 1;i <= n;i++) { int low = 0,high = i - 1; while (low < high) { int mid = (low + high + 1) >> 1; if (MIN[mid] <= a[i].first) low = mid; else high = mid - 1; } for (int j = low;j >= 0;j--) MIN[j + 1] = min(MIN[j + 1],MIN[j] + a[i].second); } for (int i = n;i >= 0;i--) if (MIN[i] != INF) { printf("%d\n",i); return 0; } return 0; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include "bits/stdc++.h" using namespace std; typedef long long ll; const int nax = 5010; const ll INF = 1e18L; ll dp[nax][nax]; int main() { ios::sync_with_stdio(false); cin.tie(0); #ifdef LOCAL freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif for (int i = 0; i < nax; ++i) { for (int j = 0; j <= i; ++j) { dp[i][j] = INF; } } dp[0][0] = 0; int n; cin >> n; vector<pair<int, int>> q(n); for (int i = 0; i < n; ++i) { cin >> q[i].first >> q[i].second; q[i].first += q[i].second; } sort(q.begin(), q.end()); for (int i = 0; i < n; ++i) { q[i].first -= q[i].second; } for (int i = 0; i < n; ++i) { for (int j = 0; j <= i; ++j) { if (dp[i][j] == INF) continue; dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]); if (dp[i][j] <= q[i].first) { dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + q[i].second); } } } int ans = 0; for (int i = 0; i <= n; ++i) { if (dp[n][i] != INF) { ans = i; } } cout << ans << '\n'; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include "bits/stdc++.h" using namespace std; #define fi first #define se second #define ll long long #define dbg(v) cerr<<#v<<" = "<<v<<'\n' #define vi vector<int> #define vl vector <ll> #define pii pair<int,int> #define mp make_pair #define db long double #define pb push_back #define all(s) s.begin(),s.end() template < class T > T smin(T &a,T b) {if (a > b) a = b;return a;} template < class T > T smax(T &a,T b) {if (a < b) a = b;return a;} ll dp[5555]; int main(void) { int n; cin>>n; vector < pii > s(n); for (auto &it : s) cin>>it.fi>>it.se; sort(all(s),[&](pii x,pii y) { return (x.fi + x.se) < (y.fi + y.se); }); for (int i = 1;i <= n;++i) dp[i] = 1e18; dp[0] = 0; for (auto it : s) for (int i = n;i;--i) if (dp[i - 1] <= it.fi) smin(dp[i],dp[i - 1] + it.se); int ans = 0; for (int i = 0;i <= n;++i) if (dp[i] < 1e18) ans = i; cout << ans << '\n'; return 0; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include <bits/stdc++.h> using namespace std; #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define endl "\n" #define int long long const int N = 5005; struct data { int h, p; }; int n, mx; struct data a[N]; int cache[N][N]; bool comp(struct data &d1, struct data &d2) { return d1.h + d1.p < d2.h + d2.p; } int dp(int i, int taken) //should store minP sum { if(i == 0) { if(taken == 0) return 0; return 1e14; } int &ans = cache[i][taken]; if(ans != -1) return ans; ans = dp(i - 1, taken); int curSum = dp(i - 1, taken - 1); if(a[i].h >= curSum) ans = min(ans, dp(i - 1, taken - 1) + a[i].p); //cerr << i << " " << taken << " " << ans << endl; return ans; } int32_t main() { IOS; memset(cache, -1, sizeof(cache)); cin >> n; for(int i = 1; i <= n; i++) cin >> a[i].h >> a[i].p; sort(a + 1, a + n + 1, comp); for(int i = n; i >= 1; i--) { if(dp(n, i) < 1e14) { cout << i; return 0; } } return 0; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include <iostream> #include<vector> #include<algorithm> #include<iomanip> #include<map> using namespace std; #define rep(i,n) for(int i=0;i<(int)(n);i++) #define REP(i,m,n) for(int i=m;i<(int)(n);i++) typedef long long ll; typedef pair<ll,ll> pint; const int inf = 510101010; const int mod=1e9+7; const ll longinf=1LL<<60; int main(){ int n;cin>>n; pint p[n]; rep(i,n){ ll a,b;cin>>a>>b; p[i]=pint(a+b,b); } sort(p,p+n); ll dp[5010]; rep(i,n+1) dp[i]=longinf; dp[0]=0; rep(i,n){ for(int j=n;j>=0;j--){ if(dp[j]<=p[i].first-p[i].second) dp[j+1]=min(dp[j]+p[i].second,dp[j+1]); } } rep(i,n){ if(dp[n-i]!=longinf){ cout<<n-i<<endl; break; } } return 0; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include<algorithm> #include<iostream> #include<cstdio> typedef long long ll; using namespace std; struct Node{ int h,p; }a[5005]; int n; ll f[5005][5005]; bool cmp(Node a,Node b){ return a.h+a.p<b.h+b.p; } int main(){ ios::sync_with_stdio(false); int i,j; cin>>n; fill(f[0],f[0]+n+1,2e9+1); for(i=1;i<=n;i++) cin>>a[i].h>>a[i].p; sort(a+1,a+n+1,cmp); f[0][0]=0; for(i=1;i<=n;i++) for(j=0;j<=n;j++){ f[i][j]=f[i-1][j]; if(j&&f[i-1][j-1]<=a[i].h) f[i][j]=min(f[i][j],f[i-1][j-1]+a[i].p); } for(i=n;i>=0;i--) if(f[n][i]<=2e9) break; cout<<i; return 0; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; import java.util.NoSuchElementException; public class Main { public static void main(String[] args) { IO io = new IO(); int n = io.nextInt(); Man[] m = new Man[n]; for(int i=0;i<n;i++) { m[i] = new Man(io.nextInt(), io.nextInt()); } System.out.println(uso(n,m)); } public static long INF = 1L << 60L; public static int uso(int n,Man[] m) { Arrays.sort(m,(m1,m2)->Integer.compare(m1.h + m1.p, m2.h + m2.p)); long[] dp = new long[n+1]; //i番目まで,j個使った高さ,INF Arrays.fill(dp, INF); dp[0] = 0; for(int i=0;i<n;i++) { for(int j=n;j>0;j--) { if (dp[j-1] <= m[i].h) { dp[j] = Math.min(dp[j], dp[j-1] + m[i].p); } } // System.out.println(Arrays.toString(dp)); } // System.out.println(Arrays.deepToString(dp)); for(int i=n;i>=0;i--) { if (dp[i] < INF) { return i; } } return 0; } } class Man { int h,p; public Man(int h, int p) { super(); this.h = h; this.p = p; } @Override public String toString() { return "Man [h=" + h + ", p=" + p + "]"; } } class IO extends PrintWriter { private final InputStream in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; public IO() { this(System.in);} public IO(InputStream source) { super(System.out); this.in = source;} private boolean hasNextByte() { if (ptr < buflen) { return true; }else{ ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;} private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;} private static boolean isNewLine(int c) { return c == '\n' || c == '\r';} public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();} public boolean hasNextLine() { while(hasNextByte() && isNewLine(buffer[ptr])) ptr++; return hasNextByte();} public String next() { if (!hasNext()) { throw new NoSuchElementException(); } StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public char[] nextCharArray(int len) { if (!hasNext()) { throw new NoSuchElementException(); } char[] s = new char[len]; int i = 0; int b = readByte(); while(isPrintableChar(b)) { if (i == len) { throw new InputMismatchException(); } s[i++] = (char) b; b = readByte(); } return s; } public String nextLine() { if (!hasNextLine()) { throw new NoSuchElementException(); } StringBuilder sb = new StringBuilder(); int b = readByte(); while(!isNewLine(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) { throw new NoSuchElementException(); } long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while(true){ if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; }else if(b == -1 || !isPrintableChar(b)){ return minus ? -n : n; }else{ throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) { throw new NumberFormatException(); } return (int) nl; } public char nextChar() { if (!hasNext()) { throw new NoSuchElementException(); } return (char) readByte(); } public double nextDouble() { return Double.parseDouble(next());} public int[] nextIntArray(int n) { int[] a = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a;} public long[] nextLongArray(int n) { long[] a = new long[n]; for(int i=0;i<n;i++) a[i] = nextLong(); return a;} public double[] nextDoubleArray(int n) { double[] a = new double[n]; for(int i=0;i<n;i++) a[i] = nextDouble(); return a;} public void nextIntArrays(int[]... a) { for(int i=0;i<a[0].length;i++) for(int j=0;j<a.length;j++) a[j][i] = nextInt();} public int[][] nextIntMatrix(int n,int m) { int[][] a = new int[n][]; for(int i=0;i<n;i++) a[i] = nextIntArray(m); return a;} public char[][] nextCharMap(int n,int m) { char[][] a = new char[n][]; for(int i=0;i<n;i++) a[i] = nextCharArray(m); return a;} public void close() { super.close(); try {in.close();} catch (IOException e) {}} }
JAVA
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=5002; typedef long long ll; struct cy{ int H,P; }a[N]; int n,i,j; ll f[N]; int Min(int x,int y){ return x<y?x:y; } bool cmp(cy x,cy y){ return Min(x.H,y.H-x.P)>Min(y.H,x.H-y.P); } void init(){ scanf("%d",&n); for(i=1;i<=n;i++) scanf("%d%d",&a[i].H,&a[i].P); sort(a+1,a+n+1,cmp); } void work(){ memset(f,0x3f,sizeof(f)); f[0]=0; for(i=1;i<=n;i++){ for(j=N-2;j>=0;j--) if(f[j]<=a[i].H) f[j+1]=min(f[j+1],f[j]+a[i].P); } for(i=N-1;i>=0;i--) if(f[i]<1ll<<50){ printf("%d\n",i); return; } } int main(){ init(); work(); return 0; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include<bits/stdc++.h> #define ll long long using namespace std; struct dat{ ll h,p; bool operator <(const dat&x)const{return h+p<x.h+x.p;} }p[5005]; ll dp[5005],n,ans,inf; int main(){ cin>>n;for(int i=1;i<=n;++i)cin>>p[i].h>>p[i].p; sort(p+1,p+n+1);memset(dp,127,sizeof(dp));inf=dp[0],dp[0]=0; for(int i=1;i<=n;++i)for(int j=i;j;--j)if(dp[j-1]<=p[i].h)dp[j]=min(dp[j],dp[j-1]+p[i].p); for(int i=n;i;--i)if(dp[i]!=inf)return cout<<i,0; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include <bits/stdc++.h> using namespace std; typedef long long LL; #define INF 10000000000000000 LL zab[11000]; int main(){ LL n; cin >> n; vector<pair<LL,LL> > g; for(LL i = 0; i < n; i++){ LL a, b; cin >> a >> b; a += b; g.push_back(make_pair(a,b)); } sort(g.begin(), g.end()); for(LL i = 0; i <= n; i++){ zab[i] = INF; } zab[0] = 0; for(LL i = 0; i < n; i++){ g[i].first -= g[i].second; for(LL j = n-1; j >= 0; j--){ if(zab[j] <= g[i].first){ zab[j+1] = min(zab[j+1], zab[j] + g[i].second); } } } LL best = 0; for(LL i = 0; i <= n; i++){ if(zab[i] < INF){ best = i; } } cout << best << endl; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include <iostream> #include <algorithm> #include <string> #include <vector> #include <map> #include <queue> #include <cstdio> #include <complex> #include <numeric> #include <string.h> #include <random> #define rep(i,n) for (int i = 0; i < (int)n; i++) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pi; typedef pair<pi, pi> pp; typedef pair<ll, ll> pl; double PI = 3.141592653589793238462643383279; const double EPS = 1e-9; const ll MOD = 998244353; const int inf = 1 << 30; const ll linf = 1LL << 60; bool cmp (const pl& l, const pl& r) { return l.first+l.second < r.first+r.second; } int N; pl p[5000]; ll dp[5001]; int main() { cin >> N; rep(i,N) cin >> p[i].first >> p[i].second; sort(p,p+N,cmp); rep(i,N+1) dp[i] = linf; dp[0] = 0; rep(i,N) { for (int j = N; j >= 1; j--) { if (dp[j-1] <= p[i].first) dp[j] = min(dp[j], dp[j-1]+p[i].second); } } int ans = 0; rep(i,N+1) { if (dp[i] == linf) break; ans = i; } cout << ans << endl; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include <bits/stdc++.h> using namespace std; const int N = 5 * 1000 + 17; const int MOD = 1000 * 1000 * 1000 + 7; struct item { int h, p; item() = default; }; int n; item a[N]; bool read() { if (!(cin >> n)) return false; for (int i = 0; i < n; ++i) cin >> a[i].h >> a[i].p; return true; } void solve() { sort(a, a + n, [](item a, item b) { return a.h + a.p < b.h + b.p; }); auto cmp = [](item a, item b) { return a.p > b.p; }; multiset<item, decltype(cmp)> S(cmp); auto P = 0ll; for (int i = 0; i < n; ++i) if (a[i].h >= P) { S.insert(a[i]); P += a[i].p; } else { auto b = *S.begin(); if (b.p > a[i].p) { S.erase(S.begin()); S.insert(a[i]); P += a[i].p - b.p; } } cout << S.size() << endl; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); while (read()) solve(); return 0; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
import sys input = lambda : sys.stdin.readline().rstrip() sys.setrecursionlimit(max(1000, 10**9)) write = lambda x: sys.stdout.write(x+"\n") n = int(input()) hp = [tuple(map(int, input().split())) for _ in range(n)] hp.sort(key=lambda x: x[0]+x[1]) dp = [10**15] * (n+1) dp[0] = 0 for i in range(n): h,p = hp[i] for j in range(n,0,-1): if dp[j-1]<=h: dp[j] = min(dp[j], dp[j-1]+p) # print(dp) for i in range(n, -1, -1): if dp[i]<10**15: break ans = i print(ans)
PYTHON3
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> using namespace std; #if people_to_orz %%% lys orz; %%% lyj orz; %%% shy orz; %%% yy orz; cai->tlx; #endif #define read(x) scanf("%lld",&x) #define MAXN 5005 #define ll long long int n; struct node { ll h,p; }a[MAXN]; ll dp[MAXN]; int ans; bool cmp(node x,node y){return x.h+x.p<y.h+y.p;} int main() { read(n); for(int i=1;i<=n;i++) read(a[i].h),read(a[i].p); sort(a+1,a+n+1,cmp); for(int i=1;i<=n;i++) dp[i]=0x7fffffffffffffff; dp[0]=0; for(int i=1;i<=n;i++) { for(int j=i;j>=1;j--) if(dp[j-1]<=a[i].h) dp[j]=min(dp[j],dp[j-1]+a[i].p); } for(int i=0;i<=n;i++) if(dp[i]<0x7fffffffffffffff) ans=i; printf("%d\n",ans); return 0; } /* 6 5 8 13 9 3 12 7 10 17 1 9 10 1 10 11 8 4 12 6 1 9 1 10 9 3 8 4 5 8 7 10 5 10 3 8 2 6 9 3 5 5 2 */
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include <bits/stdc++.h> #define F first #define S second #define PB push_back #define PF push_front #define MP make_pair using namespace std; typedef long long ll; typedef long double ld; typedef pair<int,int> pii; const int maxn = 5e3 + 10; const int mod = 1e9 + 7; int s[maxn], w[maxn]; int arr[maxn]; bool Compare (int fi, int se) { return s[fi] - w[se] < s[se] - w[fi]; } ll dp[maxn]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> s[i] >> w[i]; arr[i] = i; } sort (arr + 1, arr + n + 1, Compare); memset (dp, 63, sizeof dp); dp[0] = 0; int ans = 0; for (int i = 1; i <= n; i++) { for (int j = i; j >= 1; j--) { if (dp[j - 1] <= s[arr[i]]) { dp[j] = min (dp[j], dp[j - 1] + w[arr[i]]); } } } for (int i = 1; i <= n; i++) if (dp[i] != dp[n + 1]) ans = i; cout << ans << endl; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.NoSuchElementException; public class Main { private static void solve() { int n = nei(); long[][] hps = nls2(n, 2); for (int i = 0; i < n; i++) { hps[i][0] += hps[i][1]; // System.out.printf("h[%d]=%d, p[%d]=%d\n", i, hps[i][0], i, hps[i][1]); } Arrays.sort(hps, (hp1, hp2) -> { return (int) (hp1[0] - hp2[0]); }); long[][] dp = new long[n + 1][n + 1]; long inf = Long.MAX_VALUE / 10; for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { long h = inf; if (dp[i - 1][j - 1] + hps[i - 1][1] <= hps[i - 1][0]) h = min(h, dp[i - 1][j - 1] + hps[i - 1][1]); if (j <= i - 1) h = min(h, dp[i - 1][j]); dp[i][j] = h; // System.out.printf("dp[%d][%d] = %d\n", i, j, h); } } for (int i = n; i > 0; i--) { if (dp[n][i] != inf) kil(i); } throw new RuntimeException(); } static int[] lis(int[] s) { int n = s.length; int[] dp = new int[n]; int[] ids = new int[n]; int[] pids = new int[n]; dp[0] = s[0]; int len = 1; int lidx = 0; for (int i = 1; i < n; i++) { int idx = bs(s[i], dp, 0, len); dp[idx] = s[i]; ids[idx] = i; if (idx == len) { lidx = i; len++; } if (idx > 0) pids[i] = ids[idx - 1]; } int[] lis = new int[len]; lis[len - 1] = s[lidx]; for (int i = len - 1; i >= 0; i--) { lis[i] = s[lidx]; lidx = pids[lidx]; } return lis; } static int bs(int a, int[] as, int from, int num) { int min = from; int max = from + num - 1; while (min < max) { int mid = min + max >> 1; if (as[mid] < a) min = mid + 1; else if (as[mid] > a) max = mid; else return mid; } return as[min] < a ? min + 1 : min; } static int gcd(int x, int y) { x = (x ^ x >> 31) - (x >> 31); y = (y ^ y >> 31) - (y >> 31); if (x < y) { x ^= y; y ^= x; x ^= y; } int z = x % y; if (z == 0) return y; return gcd(y, z); } static long gcd(long x, long y) { x = (x ^ x >> 63) - (x >> 63); y = (y ^ y >> 63) - (y >> 63); if (x < y) { x ^= y; y ^= x; x ^= y; } long z = x % y; if (z == 0) return y; return gcd(y, z); } static int lcm(int x, int y) { x = (x ^ x >> 31) - (x >> 31); y = (y ^ y >> 31) - (y >> 31); return x / gcd(x, y) * y; } static long lcm(long x, long y) { x = (x ^ x >> 63) - (x >> 63); y = (y ^ y >> 63) - (y >> 63); return x / gcd(x, y) * y; } static int abs(int x) { return x < 0 ? -x : x; } static long abs(long x) { return x < 0 ? -x : x; } static int min(int a, int b) { return a < b ? a : b; } static long min(long a, long b) { return a < b ? a : b; } static int max(int a, int b) { return a > b ? a : b; } static long max(long a, long b) { return a > b ? a : b; } static int clamp(int a, int min, int max) { return a < min ? min : a > max ? max : a; } static long clamp(long a, long min, long max) { return a < min ? min : a > max ? max : a; } static void out(String val) { IO.out(val); } static void out(Object val) { IO.out(String.valueOf(val)); } static void out(int val) { IO.out(String.valueOf(val)); } static void out(long val) { IO.out(String.valueOf(val)); } static void out(char val) { IO.out(String.valueOf(val)); } static void out(float val) { IO.out(String.valueOf(val)); } static void out(double val) { IO.out(String.valueOf(val)); } static void out(boolean val) { IO.out(String.valueOf(val)); } static void kil(String val) { IO.out(val); IO.flush(); System.exit(0); } static void kil(Object val) { IO.out(String.valueOf(val)); IO.flush(); System.exit(0); } static void kil(int val) { IO.out(String.valueOf(val)); IO.flush(); System.exit(0); } static void kil(long val) { IO.out(String.valueOf(val)); IO.flush(); System.exit(0); } static void kil(char val) { IO.out(String.valueOf(val)); IO.flush(); System.exit(0); } static void kil(float val) { IO.out(String.valueOf(val)); IO.flush(); System.exit(0); } static void kil(double val) { IO.out(String.valueOf(val)); IO.flush(); System.exit(0); } static void kil(boolean val) { IO.out(String.valueOf(val)); IO.flush(); System.exit(0); } static String nes() { return IO.next(); } static int nei() { return IO.nextInt(); } static long nel() { return IO.nextLong(); } static double ned() { return IO.nextDouble(); } static char nec() { return IO.nextChar(); } static String[] nss(int n) { String[] as = new String[n]; for (int i = 0; i < n; i++) { as[i] = IO.next(); } return as; } static int[] nis(int n) { int[] as = new int[n]; for (int i = 0; i < n; i++) { as[i] = IO.nextInt(); } return as; } static long[] nls(int n) { long[] as = new long[n]; for (int i = 0; i < n; i++) { as[i] = IO.nextLong(); } return as; } static double[] nds(int n) { double[] as = new double[n]; for (int i = 0; i < n; i++) { as[i] = IO.nextDouble(); } return as; } static char[] ncs(int n) { char[] as = new char[n]; for (int i = 0; i < n; i++) { as[i] = IO.nextChar(); } return as; } static String[][] nss2(int n, int m) { String[][] as = new String[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { as[i][j] = IO.next(); } } return as; } static int[][] nis2(int n, int m) { int[][] as = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { as[i][j] = IO.nextInt(); } } return as; } static long[][] nls2(int n, int m) { long[][] as = new long[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { as[i][j] = IO.nextLong(); } } return as; } static double[][] nds2(int n, int m) { double[][] as = new double[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { as[i][j] = IO.nextDouble(); } } return as; } static char[][] ncs2(int n, int m) { char[][] as = new char[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { as[i][j] = IO.nextChar(); } } return as; } static int parseInt(String val) { return Integer.parseInt(val); } static int parseInt(char val) { return Integer.parseInt(String.valueOf(val)); } static long parseLong(String val) { return Long.parseLong(val); } public static void main(String[] args) { try { solve(); IO.flush(); } catch (Exception e) { e.printStackTrace(); } } } final class IO { private static final InputStream in = System.in; private static final PrintWriter out = new PrintWriter(System.out, false); private static final byte[] buffer = new byte[1024]; private static int ptr = 0; private static int len = 0; private static boolean hasNextByte() { if (ptr < len) return true; ptr = 0; try { len = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } return len > 0; } private static int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } static boolean hasNext() { byte c; while (hasNextByte() && ((c = buffer[ptr]) < '!' || c > '~')) ptr++; return hasNextByte(); } static String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (b >= '!' && b <= '~') { sb.append((char) b); b = readByte(); } return sb.toString(); } static char nextChar() { if (!hasNext()) throw new NoSuchElementException(); return (char) readByte(); } static long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; int sign = 1; int b = readByte(); if (b == '-') { sign = -1; b = readByte(); } if (b < '0' || '9' < b) throw new NumberFormatException(); while (true) { if ('0' <= b && b <= '9') n = n * 10 + b - '0'; else if (b == -1 || b < '!' || b > '~') return n * sign; else throw new NumberFormatException(); b = readByte(); } } static int nextInt() { if (!hasNext()) throw new NoSuchElementException(); int n = 0; int sign = 1; int b = readByte(); if (b == '-') { sign = -1; b = readByte(); } if (b < '0' || '9' < b) throw new NumberFormatException(); while (true) { if ('0' <= b && b <= '9') n = n * 10 + b - '0'; else if (b == -1 || b < '!' || b > '~') return n * sign; else throw new NumberFormatException(); b = readByte(); } } static double nextDouble() { return Double.parseDouble(next()); } static void out(String val) { out.println(val); } static void flush() { out.flush(); } }
JAVA
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include<bits/stdc++.h> using namespace std; #define pb push_back #define fi first #define se second typedef pair<int, int> ii; const int N = 5e3 + 5; int n, sum; vector< pair<int, ii> > vec; priority_queue<int> pq; int main () { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; ++i) { int h, p; cin >> h >> p; vec.pb( { h + p, { h, p } } ); } sort(vec.begin(), vec.end() ); for (auto i : vec) { if(sum <= i.se.fi) { pq.push(i.se.se); sum += i.se.se; } else { if(i.se.se < pq.top() ) { sum -= pq.top(); pq.pop(); sum += i.se.se; pq.push(i.se.se); } } } cout << pq.size(); return 0; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include <cstdio> #include <cstring> #include <cmath> #include <iostream> #include <algorithm> #include <queue> #include <set> #include <vector> #include <stack> #include <map> #define ri register #define inf 0x7fffffff #define E (1) #define mk make_pair #define int long long using namespace std; const int N=5010; inline int read() { int s=0, w=1; ri char ch=getchar(); while(ch<'0'||ch>'9') {if(ch=='-') w=-1; ch=getchar(); } while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+(ch^48), ch=getchar(); return s*w; } int n,dp[N]; struct Node{int h,p; }e[N]; inline bool cp(Node x,Node y) { return x.h+x.p<y.h+y.p; } signed main() { n=read(); for(ri int i=1;i<=n;i++) e[i].h=read(), e[i].p=read(); sort(e+1,e+1+n,cp); int res=0; for(ri int i=1;i<=n;i++) dp[i]=1e16; for(ri int i=1;i<=n;i++) {for(ri int j=i;j;j--)if(e[i].h>=dp[j-1])dp[j]=min(dp[j],dp[j-1]+e[i].p);} for(ri int i=0;i<=n;i++)if(dp[i]<1e16)res=i; printf("%lld\n",res); return 0; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include<stdio.h> #include<vector> #include<algorithm> using namespace std; int dp[5001][5001]; typedef pair<int, int>pii; int main() { int num; scanf("%d", &num); vector<pii>v; for (int i = 0; i < num; i++) { int za, zb; scanf("%d%d", &za, &zb); v.push_back(make_pair(za + zb, zb)); } sort(v.begin(), v.end()); reverse(v.begin(), v.end()); for (int i = 0; i <= num; i++)for (int j = 0; j <= num; j++)dp[i][j] = -1; dp[0][0] = 2100000000; for (int i = 0; i < num; i++) { for (int j = 0; j <= i; j++) { if (dp[i][j] >= 0) { dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]); dp[i + 1][j + 1] = max(dp[i + 1][j + 1], min(dp[i][j], v[i].first) - v[i].second); } } } int maxi = 0; for (int i = 0; i <= num; i++)if (dp[num][i] >= 0)maxi = max(maxi, i); printf("%d\n", maxi); }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
//Link : https://atcoder.jp/contests/m-solutions2019/tasks/m_solutions2019_c #include <bits/stdc++.h> using namespace std; #define ll long long #define N 5005 #define inf 1000000000000000000LL pair<int,int> in[N]; int H[N],P[N]; ll dp[N][N]; void solve() { int n;scanf("%d ", &n); for(int i=0;i<n;++i) { scanf("%d %d ", &H[i],&P[i]); in[i] = make_pair(H[i]+P[i],i); } sort(in,in+n); for(int i=0;i<=n;++i) { for(int j=0;j<=n;++j) { dp[i][j] = inf; } } dp[0][0] = 0; for(int i=0;i<n;++i) { int h = H[in[i].second],p = P[in[i].second]; for(int j=i;j>=0;--j) { if(dp[i][j]==inf) { continue; } if(dp[i][j]<=h) { dp[i+1][j+1] = min(dp[i+1][j+1], dp[i][j] + p); } dp[i+1][j] = min(dp[i+1][j], dp[i][j]); } } for(int i=n;i>=0;--i) { if(dp[n][i]!=inf) { printf("%d\n", i); return; } } } int main() { //freopen("input.txt","r",stdin); solve(); return 0; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
/* https://img.atcoder.jp/cf17-final/editorial.pdf 贪心+dp 关于为什么按 hi + pi 升序排列,官方题解说的很有道理。 这边提供另外一种证明:考虑一个合法的选的序列,对于每一个 i 必须满足 sum_{j <= i}{h_j} <= h_i + p_i, 因为前缀和递增所以 hi + pi 也必须递增。 */ #include <bits/stdc++.h> #define rep(i, x, y) for (int i = x; i <= y; i++) using namespace std; const int N = 5005; typedef long long ll; const ll inf = 0x3f3f3f3f3f3f3f3f; int n, a[N], b[N], c[N], id[N]; ll mn[N]; bool cmp(int x, int y) { return c[x] == c[y] ? b[x] < b[y] : c[x] < c[y]; } int main() { cin >> n; rep(i, 1, n) scanf("%d%d", &a[i], &b[i]), c[i] = a[i] + b[i], id[i] = i; sort(id + 1, id + n + 1, cmp); memset(mn, 0x3f, sizeof(mn)); mn[0] = 0; rep(i, 1, n) { for (int j = n - 1; j >= 0; j--) if (mn[j] != inf && mn[j] <= a[id[i]]) mn[j + 1] = min(mn[j + 1], mn[j] + b[id[i]]); } for (int i = n; i >= 0; i--) if (mn[i] != inf) return printf("%d\n", i), 0; return 0; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include<bits/stdc++.h> using namespace std; const int INF = 1 << 30; int main() { int N; cin >> N; vector< pair< int, int > > st(N); for(int i = 0; i < N; i++) { cin >> st[i].first >> st[i].second; } sort(begin(st), end(st), [&](pair< int, int > x, pair< int, int > y) { return (x.first - y.second < y.first - x.second); }); int dp[5001]; fill_n(dp, 5001, INF); dp[0] = 0; int ret = 0; for(int i = 0; i < N; i++) { for(int j = N - 1; j >= 0; j--) { if(dp[j] <= st[i].first) { dp[j + 1] = min(dp[j + 1], dp[j] + st[i].second); ret = max(ret, j + 1); } } } cout << ret << endl; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#pragma GCC optimize("Ofast") #include<bits/stdc++.h> #define int long long #define gmax(x,y) x=max(x,y) #define gmin(x,y) x=min(x,y) #define F first #define S second #define P pair #define FOR(i,a,b) for(int i=a;i<=b;i++) #define rep(i,a,b) for(int i=a;i<b;i++) #define V vector #define RE return #define ALL(a) a.begin(),a.end() #define MP make_pair #define PB emplace_back #define PF push_front #define FILL(a,b) memset(a,b,sizeof(a)) #define lwb lower_bound #define upb upper_bound using namespace std; int dp[5005],maxi=1e18; P<int,int> a[5005]; bool cmp(P<int,int> x,P<int,int> y){ RE x.F+x.S<y.F+y.S; } signed main(){ ios::sync_with_stdio(0); cin.tie(0); int n; cin>>n; FOR(i,1,n)cin>>a[i].F>>a[i].S,dp[i]=maxi; sort(a+1,a+n+1,cmp); FOR(i,1,n){ for(int j=n;j>=1;j--){ if(dp[j-1]<=a[i].F){ gmin(dp[j],dp[j-1]+a[i].S); } } } for(int i=n;i>=0;i--){ if(dp[i]!=maxi){ cout<<i<<'\n';RE 0; } } RE 0; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include <iostream> #include <algorithm> #include <vector> using namespace std; long long N; long long dp[5001][5001]; vector<long long> H, P; vector<pair<long long, long long> > HP; const long long inf = 1e15; int main() { cin >> N; for (int i = 0; i < N; i++) { long long h, p; cin >> h >> p; H.push_back(h); P.push_back(p); HP.push_back(make_pair(h + p, i)); } sort(HP.begin(), HP.end()); for (int i = 0; i <= N;i++) { for (int j = 1; j <= N;j++) { dp[i][j] = inf; } } long long ans = 0; for (long long i = 1; i <= N;i++) { long long idx = HP[i - 1].second; for (long long j = 0; j <= i; j++) { dp[i][j] = min(dp[i][j], dp[i - 1][j]); if (j - 1 >= 0 && dp[i - 1][j - 1] <= H[idx]) { dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + P[idx]); } if(dp[i][j] < inf) { ans = max(ans, j); } } } cout << ans << endl; return 0; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
import java.io.*; import java.util.*; class HP implements Comparable<HP> { final long h, p; HP(long h, long p) { this.h = h; this.p = p; } public int compareTo(HP hp) { return Long.compare(this.h + this.p, hp.h + hp.p); } } public class Main { private static final long INF = 3_000_000_000L; private static int solve(int n, long[][] hpsInput) { HP[] hps = new HP[n]; for (int i = 0; i < n; i++) { hps[i] = new HP(hpsInput[i][0], hpsInput[i][1]); } Arrays.sort(hps); long[] minHeight = new long[n + 1]; Arrays.fill(minHeight, INF); minHeight[0] = 0; for (HP hp : hps) { for (int i = n - 1; i >= 0; i--) { if (minHeight[i] <= hp.h) { minHeight[i + 1] = Math.min(minHeight[i + 1], minHeight[i] + hp.p); } } } for (int i = n; i >= 0; i--) { if (minHeight[i] < INF) { return i; } } throw new RuntimeException(); } private static void execute(ContestReader reader, ContestWriter out) { int n = reader.nextInt(); long[][] hps = reader.nextLong(n, 2); out.println(solve(n, hps)); } public static void main(String[] args) { ContestReader reader = new ContestReader(System.in); ContestWriter out = new ContestWriter(System.out); execute(reader, out); out.flush(); } } class ContestWriter extends PrintWriter { ContestWriter(PrintStream printeStream) { super(printeStream); } public void printList(List<? extends Object> list) { for (Object object : list) { println(object); } } public void printListOneLine(List<? extends Object> list) { List<String> stringList = new ArrayList<>(); for (Object object : list) { stringList.add(object.toString()); } println(String.join(" ", stringList)); } } class ContestReader { private static final int BUFFER_SIZE = 1024; private final InputStream stream; private final byte[] buffer; private int pointer; private int bufferLength; ContestReader(InputStream stream) { this.stream = stream; this.buffer = new byte[BUFFER_SIZE]; this.pointer = 0; this.bufferLength = 0; } private boolean hasNextByte() { if (pointer < bufferLength) { return true; } pointer = 0; try { bufferLength = stream.read(buffer); } catch (IOException e) { throw new RuntimeException(e); } return bufferLength > 0; } private int readByte() { if (hasNextByte()) { return buffer[pointer++]; } else { return -1; } } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while (hasNextByte() && !isPrintableChar(buffer[pointer])) { pointer++; } return hasNextByte(); } public String next() { if (!hasNext()) { throw new NoSuchElementException(); } StringBuilder sb = new StringBuilder(); while(true) { int b = readByte(); if (!isPrintableChar(b)) { break; } sb.appendCodePoint(b); } return sb.toString(); } public String nextLine() { if (!hasNext()) { throw new NoSuchElementException(); } StringBuilder sb = new StringBuilder(); while(true) { int b = readByte(); if (!isPrintableChar(b) && b != 0x20) { break; } sb.appendCodePoint(b); } return sb.toString(); } public char nextChar() { return next().charAt(0); } public int nextInt() { if (!hasNext()) { throw new NoSuchElementException(); } int n = 0; boolean minus = false; { int b = readByte(); if (b == '-') { minus = true; } else if ('0' <= b && b <= '9') { n = b - '0'; } else { throw new NumberFormatException(); } } while(true){ int b = readByte(); if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } } } public long nextLong() { if (!hasNext()) { throw new NoSuchElementException(); } long n = 0; boolean minus = false; { int b = readByte(); if (b == '-') { minus = true; } else if ('0' <= b && b <= '9') { n = b - '0'; } else { throw new NumberFormatException(); } } while(true){ int b = readByte(); if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } } } public double nextDouble() { return Double.parseDouble(next()); } public String[] next(int n) { String[] array = new String[n]; for (int i = 0; i < n; i++) { array[i] = next(); } return array; } public String[] nextLine(int n) { String[] array = new String[n]; for (int i = 0; i < n; i++) { array[i] = nextLine(); } return array; } public char[] nextChar(int n) { char[] array = new char[n]; for (int i = 0; i < n; i++) { array[i] = nextChar(); } return array; } public int[] nextInt(int n) { int[] array = new int[n]; for (int i = 0; i < n; i++) { array[i] = nextInt(); } return array; } public long[] nextLong(int n) { long[] array = new long[n]; for (int i = 0; i < n; i++) { array[i] = nextLong(); } return array; } public double[] nextDouble(int n) { double[] array = new double[n]; for (int i = 0; i < n; i++) { array[i] = nextDouble(); } return array; } public char[] nextCharArray() { return next().toCharArray(); } public String[][] next(int n, int m) { String[][] matrix = new String[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { matrix[i][j] = next(); } } return matrix; } public int[][] nextInt(int n, int m) { int[][] matrix = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { matrix[i][j] = nextInt(); } } return matrix; } public char[][] nextChar(int n, int m) { char[][] matrix = new char[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { matrix[i][j] = nextChar(); } } return matrix; } public long[][] nextLong(int n, int m) { long[][] matrix = new long[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { matrix[i][j] = nextLong(); } } return matrix; } public double[][] nextDouble(int n, int m) { double[][] matrix = new double[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { matrix[i][j] = nextDouble(); } } return matrix; } public char[][] nextCharArray(int n) { char[][] matrix = new char[n][]; for (int i = 0; i < n; i++) { matrix[i] = next().toCharArray(); } return matrix; } }
JAVA
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
""" 余裕の小さい人から先に座布団を積ませた方がよい。 余裕の大きさは自分の身長と他人のパワーで決まる。 いまここにAとBがいる。 このとき、Bp > AhならAが先に積んだ方がよい。 Ap > Bh ならBが先に積んだ方がよい。 これは書き換えるとAh - Bp < 0ならAがさき、Bh - Ap < 0ならBが先。 Ah - Bp < Bh - ApならAが先、Ah - Bp > Bh - ApならBが先である。 書き換えると、 Ah + Ap < Bh + Bp ならAが先、Ah + Ap > Bh + Bp ならBが先である。 したがって身長+パワーで昇順に並び替えればよい。 """ N = int(input()) HP = [list(map(int,input().split())) for _ in range(N)] HP.sort(key= lambda x:x[0]+x[1]) dp = [float("INF")]*(N+1) dp[0] = 0 for i in range(N): h,p = HP[i] for j in range(N,0,-1): if dp[j-1] <= h: dp[j] = min(dp[j],dp[j-1]+p) for j in range(N,-1,-1): if dp[j] != float("INF"): print(j) break
PYTHON3
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll,ll> P; typedef pair<int,P> P1; typedef pair<P,P> P2; #define pu push #define pb push_back #define mp make_pair #define eps 1e-7 #define INF 1000000000 #define mod 1000000007 #define fi first #define sc second #define rep(i,x) for(int i=0;i<x;i++) #define repn(i,x) for(int i=1;i<=x;i++) #define SORT(x) sort(x.begin(),x.end()) #define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end()) #define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) #define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) ll n,dp[5005][5005]; P p[5005]; bool cmp(const P&a,const P&b){ return a.fi+a.sc<b.fi+b.sc; } int main(){ rep(i,5005)rep(j,5005) dp[i][j]=1e18; cin>>n; repn(i,n){ll a,b;cin>>a>>b;p[i]=mp(a,b);} sort(p+1,p+n+1,cmp); dp[0][0] = 0; for(int i=0;i<n;i++){ for(int j=0;j<=n;j++){ dp[i+1][j] = min(dp[i+1][j],dp[i][j]); if(dp[i][j] > p[i+1].fi) continue; dp[i+1][j+1] = min(dp[i+1][j+1],dp[i][j]+p[i+1].sc); } } int ans = INF; for(int i=0;i<=n;i++){ if(dp[n][i] <= 1e17){ ans = i; } } cout<<ans<<endl; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
#pragma GCC optimize ("Ofast") #include <bits/stdc++.h> using namespace std; #define int long long int #define pi pair<int,int> #define pb push_back #define fi first #define se second #define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0) #ifndef LOCAL #define endl '\n' #endif const int N = 5005; pi a[N]; int dp[N][N]; bool cmp(pi &a, pi &b){ return a.fi+a.se < b.fi+b.se; } signed main() { #ifdef LOCAL freopen("input.txt","r",stdin); #endif IOS; int n; cin >> n; for(int i = 1; i <= n; i++) cin >> a[i].fi >> a[i].se; for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) dp[i][j] = 1e18; sort(a+1, a+n+1, cmp); dp[1][1] = a[1].se; for(int i = 2; i <= n; i++){ for(int j = 1; j <= i; j++){ dp[i][j] = dp[i-1][j]; if(a[i].fi >= dp[i-1][j-1]) dp[i][j] = min(dp[i][j], dp[i-1][j-1] + a[i].se); } } for(int i = n; i >= 1; i--) if(dp[n][i] != 1e18) return cout << i, 0; return 0; }
CPP
p03526 CODE FESTIVAL 2017 Final - Zabuton
In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively. Ringo is hosting a game of stacking zabuton (cushions). The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing. Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants? Constraints * 1 \leq N \leq 5000 * 0 \leq H_i \leq 10^9 * 1 \leq P_i \leq 10^9 Input Input is given from Standard Input in the following format: N H_1 P_1 H_2 P_2 : H_N P_N Output Print the maximum number of participants who can add zabuton to the stack. Examples Input 3 0 2 1 3 3 4 Output 2 Input 3 2 4 3 1 4 1 Output 3 Input 10 1 3 8 4 8 3 9 1 6 4 2 3 4 2 9 2 8 3 0 1 Output 5
6
0
import java.util.*; public class Main { static Person[] arr; static int[][] dp; //何人目、何人選択 public static void main (String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); arr = new Person[n]; dp = new int[n + 1][n + 1]; for (int i = 0; i < n; i++) { arr[i] = new Person(sc.nextInt(), sc.nextInt()); } Arrays.sort(arr, new Comparator<Person>() { public int compare(Person p1, Person p2) { return p1.h + p1.p - p2.h - p2.p; } }); for (int i = n; i >= 0; i--) { if (dfs(n, i) != Integer.MAX_VALUE) { System.out.println(i); return; } } } static int dfs(int x, int y) { if (y > x) { return Integer.MAX_VALUE; } if (y == 0) { return 0; } if (dp[x][y] != 0) { return dp[x][y]; } int tmp = dfs(x - 1, y - 1); if (tmp > arr[x - 1].h) { tmp = Integer.MAX_VALUE; } else { tmp += arr[x - 1].p; } int ans = Math.min(dfs(x - 1, y), tmp); dp[x][y] = ans; return ans; } static class Person { int h; int p; public Person(int h, int p) { this.h = h; this.p = p; } } }
JAVA