#include <iostream>
using namespace std;
/*
Let T[i] be the total number of yen transferred from i to i+1
A[i] - B[i] == 2*T[i] - T[i-1]
A[i+1] - B[i+1] == 2*T[i+1] - T[i]
T[i] = 2*T[i+1] + B[i+1] - A[i+1]
*/
long long N;
long long A[1000001];
long long B[1000001];
long long sumT = 0;
void b_search(long long l, long long r)
{
//cout << l << ' ' << r << '\n';
if(l == r)
{
long long temp = r;
long long S = r;
//cout << r << ' ';
for(long long i = N-1; i >= 1; i--)
{
temp = 2*temp + B[i+1] - A[i+1];
if(temp < 0)
{
cout << "No\n";
return;
}
S += temp;
// cout << temp << ' ';
}
//cout << '\n';
if(r == 2*temp + B[1] - A[1] && S == sumT) cout << "Yes\n";
else cout << "No\n";
return;
}
long long m = (l+r)/2;
long long S = m;
long long temp = m;
for(long long i = N-1; i >= 1; i--)
{
temp = 2*temp + B[i+1] - A[i+1];
if(temp < 0)
{
b_search(m+1, r);
return;
}
S += temp;
}
if(S >= sumT) b_search(l, m);
else b_search(m+1, r);
}
int main()
{
cin >> N;
int sumB;
for(long long i = 1; i <= N; i++)
{
cin >> A[i] >> B[i];
sumT += A[i] - B[i];
sumB += B[i];
}
if(sumT < 0 || sumB == 0)
{
cout << "No\n";
return 0;
}
b_search(0, 1000000000000000L);
}
Compilation message
remittance.cpp: In function 'int main()':
remittance.cpp:73:17: warning: 'sumB' may be used uninitialized in this function [-Wmaybe-uninitialized]
73 | if(sumT < 0 || sumB == 0)
| ~~~~~~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |