This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define y0 y12
#define y1 y22
#define INF 1987654321
#define PI 3.141592653589793238462643383279502884
#define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
#define fdn(i,a,b,c) for(int (i)=(a);(i)>=(b);(i)-=(c))
#define MEM0(a) memset((a),0,sizeof(a))
#define MEM_1(a) memset((a),-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define COMPRESS(a) sort(ALL(a));a.resize(unique(ALL(a))-a.begin())
#define SYNC ios_base::sync_with_stdio(false);cin.tie(0)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;
typedef pair<ld, ld> Pd;
typedef vector<int> Vi;
typedef vector<ll> Vll;
typedef vector<ld> Vd;
typedef vector<Pi> VPi;
typedef vector<Pll> VPll;
typedef vector<Pd> VPd;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef tuple<ll, ll, ll> LLL;
typedef vector<iii> Viii;
typedef vector<LLL> VLLL;
typedef complex<double> base;
const int MOD = 998244353;
ll POW(ll a, ll b, ll MMM = MOD) { ll ret = 1; for (; b; b >>= 1, a = (a*a) % MMM)if (b & 1)ret = (ret*a) % MMM; return ret; }
int dx[] = { 0,1,0,-1,1,1,-1,-1 }, dy[] = { 1,0,-1,0,1,-1,1,-1 };
int ddx[] = { -1,-2,1,-2,2,-1,2,1 }, ddy[] = { -2,-1,-2,1,-1,2,1,2 };
ll A[1000000],B[1000000],C[1000000];
ll t[1000000];
bool chk[1000000];
int main(){
int n;
scanf("%d",&n);
fup(i,0,n-1,1){
scanf("%lld%lld",A+i,B+i);
C[i]=B[i]-A[i];
}
ll l=0,r=1e9,res=-1;
while(l<=r){
ll k=l+r>>1;
t[0]=k;
t[n-1]=2*k+C[0];
int tt=0;
if(t[n-1]<0){
tt=-1;
}
if(t[n-1]>1e9){
tt=1;
}
if(!tt){
fdn(i,n-2,1,1){
t[i]=t[i+1]*2+C[i+1];
if(t[i]<0){
tt=-1;
break;
}
if(t[i]>1e9){
tt=1;
break;
}
}
if(!tt){
ll r=t[1]*2+C[1];
if(r<t[0])tt=-1;
else if(r>t[0])tt=1;
}
}
if(tt==0){
res=k;
break;
}
if(tt<0)l=k+1;
else r=k-1;
}
if(res<0)return !printf("No\n");
int tt=30;
while(tt--){
fup(i,0,n-1,1){
if(!t[i])continue;
int j=i+1;
if(j==n)j=0;
ll mn=min(t[i],A[i]>>1);
A[i]-=2*mn;
t[i]-=mn;
A[j]+=mn;
}
}
fup(i,0,n-1,1){
if(A[i]^B[i])return !printf("No\n");
}
puts("Yes");
}
Compilation message (stderr)
remittance.cpp: In function 'int main()':
remittance.cpp:10:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
10 | #define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
| ^
remittance.cpp:49:2: note: in expansion of macro 'fup'
49 | fup(i,0,n-1,1){
| ^~~
remittance.cpp:55:9: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
55 | ll k=l+r>>1;
| ~^~
remittance.cpp:11:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
11 | #define fdn(i,a,b,c) for(int (i)=(a);(i)>=(b);(i)-=(c))
| ^
remittance.cpp:66:4: note: in expansion of macro 'fdn'
66 | fdn(i,n-2,1,1){
| ^~~
remittance.cpp:10:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
10 | #define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
| ^
remittance.cpp:93:3: note: in expansion of macro 'fup'
93 | fup(i,0,n-1,1){
| ^~~
remittance.cpp:10:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
10 | #define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
| ^
remittance.cpp:103:2: note: in expansion of macro 'fup'
103 | fup(i,0,n-1,1){
| ^~~
remittance.cpp:48:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
48 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
remittance.cpp:50:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | scanf("%lld%lld",A+i,B+i);
| ~~~~~^~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |