Submission #200637

# Submission time Handle Problem Language Result Execution time Memory
200637 2020-02-07T18:15:26 Z tincamatei Remittance (JOI19_remittance) C++14
100 / 100
545 ms 42744 KB
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 1000000;
const int INF = 1000000000;
const long long INFLL = (long long)MAX_N * INF;

long long A[MAX_N], B[MAX_N];
bool viz[MAX_N];
long long finalSent[MAX_N];

bool solveRequests(int i, long long reqAmount, int N, bool addSent = false) {
	bool ok = false;
	int steps = 0;
	while(steps <= N && !ok) {
		if(reqAmount > INFLL)
			return false;
		A[i] -= reqAmount;
		if(addSent) {
			finalSent[i] += reqAmount / 2;
		}

		if(A[i] < 0) {
			reqAmount = -A[i] * 2;
			A[i] = 0;
			i = (i - 1 + N) % N;
		} else
			ok = true;
		addSent = true;
		++steps;
	}

	if(steps == N + 1)
		return false;

	return true;
}

void answerIsNo() {
	printf("No");
	exit(0);
}

// A[i] - 2 * sent[i] + sent[i - 1] == 0
// A[i] == 2 * sent[i] - sent[i - 1]

long long sent[MAX_N];
long long Ac[MAX_N];

int check(long long val, int N, long long overflow) {
	sent[0] = val;
	
	if(overflow - val < 0)
		return +1;
	overflow -= val;

	for(int i = 1; i < N; ++i) {
		// A[i] = 2 * sent[i] - sent[i - 1]
		// sent[i] = (A[i] + sent[i - 1]) / 2
		
		sent[i] = (A[i] + sent[i - 1]) / 2;
		overflow -= sent[i];
		if(overflow < 0)
			return +1;
	}
	
	if(overflow < 0)
		return +1;
	else if(overflow > 0)
		return -1;
	return 0;
}

bool launderMoney(int N) {
	long long st = -1, dr = INFLL + 1;
	long long overflow = 0LL;

	for(int i = 0; i < N; ++i)
		overflow += A[i];

	while(dr - st > 1) {
		int mid = (st + dr) / 2;
		int rez = check(mid, N, overflow);
		if(rez == -1) // Too small
			st = mid;
		else if(rez == 1) // Too big
			dr = mid;
		else { // perfect
			for(int i = 0; i < N; ++i) {
				if(A[i] != 2 * sent[i] - sent[(i - 1 + N) % N])
					return false;
				finalSent[i] += sent[i];
			}
			return true;
		}
	}

	return false;
}

bool checkSent(int N) {
	for(int _i = 0; _i < 3 * N; ++_i) {
		int i = _i % N;
		long long send = min(finalSent[i], Ac[i] / 2);
		Ac[i] -= 2 * send;
		Ac[(i + 1) % N] += send;
		finalSent[i] -= send;
	}

	for(int i = 0; i < N; ++i)
		if(finalSent[i] != 0 || Ac[i] != B[i])
			return false;
	return true;
}

int main() {
	int N;
	
	scanf("%d", &N);
	
	for(int i = 0; i < N; ++i) {
		scanf("%lld%lld", &A[i], &B[i]);
		Ac[i] = A[i];
	}

	for(int i = 0; i < N; ++i) {
		if(!solveRequests(i, B[i], N))
			answerIsNo();
	}

	if(!launderMoney(N))
		answerIsNo();

	if(!checkSent(N))
		answerIsNo();

	printf("Yes");

	return 0;
}

Compilation message

remittance.cpp: In function 'int main()':
remittance.cpp:120:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &N);
  ~~~~~^~~~~~~~~~
remittance.cpp:123:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld%lld", &A[i], &B[i]);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 5 ms 376 KB Output is correct
12 Correct 5 ms 376 KB Output is correct
13 Correct 5 ms 376 KB Output is correct
14 Correct 5 ms 376 KB Output is correct
15 Correct 5 ms 376 KB Output is correct
16 Correct 5 ms 376 KB Output is correct
17 Correct 5 ms 376 KB Output is correct
18 Correct 5 ms 376 KB Output is correct
19 Correct 5 ms 376 KB Output is correct
20 Correct 5 ms 376 KB Output is correct
21 Correct 5 ms 376 KB Output is correct
22 Correct 5 ms 376 KB Output is correct
23 Correct 5 ms 376 KB Output is correct
24 Correct 5 ms 380 KB Output is correct
25 Correct 5 ms 376 KB Output is correct
26 Correct 5 ms 376 KB Output is correct
27 Correct 5 ms 376 KB Output is correct
28 Correct 5 ms 376 KB Output is correct
29 Correct 5 ms 376 KB Output is correct
30 Correct 5 ms 376 KB Output is correct
31 Correct 5 ms 376 KB Output is correct
32 Correct 5 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 5 ms 376 KB Output is correct
12 Correct 5 ms 376 KB Output is correct
13 Correct 5 ms 376 KB Output is correct
14 Correct 5 ms 376 KB Output is correct
15 Correct 5 ms 376 KB Output is correct
16 Correct 5 ms 376 KB Output is correct
17 Correct 5 ms 376 KB Output is correct
18 Correct 5 ms 376 KB Output is correct
19 Correct 5 ms 376 KB Output is correct
20 Correct 5 ms 376 KB Output is correct
21 Correct 5 ms 376 KB Output is correct
22 Correct 5 ms 376 KB Output is correct
23 Correct 5 ms 376 KB Output is correct
24 Correct 5 ms 380 KB Output is correct
25 Correct 5 ms 376 KB Output is correct
26 Correct 5 ms 376 KB Output is correct
27 Correct 5 ms 376 KB Output is correct
28 Correct 5 ms 376 KB Output is correct
29 Correct 5 ms 376 KB Output is correct
30 Correct 5 ms 376 KB Output is correct
31 Correct 5 ms 376 KB Output is correct
32 Correct 5 ms 376 KB Output is correct
33 Correct 5 ms 504 KB Output is correct
34 Correct 5 ms 376 KB Output is correct
35 Correct 5 ms 376 KB Output is correct
36 Correct 5 ms 376 KB Output is correct
37 Correct 5 ms 376 KB Output is correct
38 Correct 5 ms 376 KB Output is correct
39 Correct 5 ms 376 KB Output is correct
40 Correct 5 ms 376 KB Output is correct
41 Correct 5 ms 380 KB Output is correct
42 Correct 5 ms 380 KB Output is correct
43 Correct 5 ms 376 KB Output is correct
44 Correct 5 ms 376 KB Output is correct
45 Correct 5 ms 376 KB Output is correct
46 Correct 5 ms 376 KB Output is correct
47 Correct 5 ms 376 KB Output is correct
48 Correct 5 ms 376 KB Output is correct
49 Correct 6 ms 376 KB Output is correct
50 Correct 5 ms 376 KB Output is correct
51 Correct 5 ms 376 KB Output is correct
52 Correct 5 ms 376 KB Output is correct
53 Correct 5 ms 376 KB Output is correct
54 Correct 5 ms 376 KB Output is correct
55 Correct 5 ms 380 KB Output is correct
56 Correct 5 ms 376 KB Output is correct
57 Correct 5 ms 376 KB Output is correct
58 Correct 5 ms 380 KB Output is correct
59 Correct 5 ms 376 KB Output is correct
60 Correct 5 ms 376 KB Output is correct
61 Correct 5 ms 380 KB Output is correct
62 Correct 5 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 5 ms 376 KB Output is correct
12 Correct 5 ms 376 KB Output is correct
13 Correct 5 ms 376 KB Output is correct
14 Correct 5 ms 376 KB Output is correct
15 Correct 5 ms 376 KB Output is correct
16 Correct 5 ms 376 KB Output is correct
17 Correct 5 ms 376 KB Output is correct
18 Correct 5 ms 376 KB Output is correct
19 Correct 5 ms 376 KB Output is correct
20 Correct 5 ms 376 KB Output is correct
21 Correct 5 ms 376 KB Output is correct
22 Correct 5 ms 376 KB Output is correct
23 Correct 5 ms 376 KB Output is correct
24 Correct 5 ms 380 KB Output is correct
25 Correct 5 ms 376 KB Output is correct
26 Correct 5 ms 376 KB Output is correct
27 Correct 5 ms 376 KB Output is correct
28 Correct 5 ms 376 KB Output is correct
29 Correct 5 ms 376 KB Output is correct
30 Correct 5 ms 376 KB Output is correct
31 Correct 5 ms 376 KB Output is correct
32 Correct 5 ms 376 KB Output is correct
33 Correct 5 ms 504 KB Output is correct
34 Correct 5 ms 376 KB Output is correct
35 Correct 5 ms 376 KB Output is correct
36 Correct 5 ms 376 KB Output is correct
37 Correct 5 ms 376 KB Output is correct
38 Correct 5 ms 376 KB Output is correct
39 Correct 5 ms 376 KB Output is correct
40 Correct 5 ms 376 KB Output is correct
41 Correct 5 ms 380 KB Output is correct
42 Correct 5 ms 380 KB Output is correct
43 Correct 5 ms 376 KB Output is correct
44 Correct 5 ms 376 KB Output is correct
45 Correct 5 ms 376 KB Output is correct
46 Correct 5 ms 376 KB Output is correct
47 Correct 5 ms 376 KB Output is correct
48 Correct 5 ms 376 KB Output is correct
49 Correct 6 ms 376 KB Output is correct
50 Correct 5 ms 376 KB Output is correct
51 Correct 5 ms 376 KB Output is correct
52 Correct 5 ms 376 KB Output is correct
53 Correct 5 ms 376 KB Output is correct
54 Correct 5 ms 376 KB Output is correct
55 Correct 5 ms 380 KB Output is correct
56 Correct 5 ms 376 KB Output is correct
57 Correct 5 ms 376 KB Output is correct
58 Correct 5 ms 380 KB Output is correct
59 Correct 5 ms 376 KB Output is correct
60 Correct 5 ms 376 KB Output is correct
61 Correct 5 ms 380 KB Output is correct
62 Correct 5 ms 376 KB Output is correct
63 Correct 362 ms 23776 KB Output is correct
64 Correct 251 ms 39416 KB Output is correct
65 Correct 537 ms 39448 KB Output is correct
66 Correct 248 ms 39544 KB Output is correct
67 Correct 271 ms 40756 KB Output is correct
68 Correct 301 ms 42744 KB Output is correct
69 Correct 545 ms 39544 KB Output is correct
70 Correct 223 ms 42744 KB Output is correct
71 Correct 231 ms 42740 KB Output is correct
72 Correct 234 ms 32632 KB Output is correct
73 Correct 498 ms 39544 KB Output is correct
74 Correct 189 ms 29688 KB Output is correct
75 Correct 202 ms 28152 KB Output is correct
76 Correct 232 ms 28664 KB Output is correct
77 Correct 360 ms 24056 KB Output is correct
78 Correct 434 ms 42648 KB Output is correct
79 Correct 391 ms 34808 KB Output is correct
80 Correct 428 ms 42488 KB Output is correct
81 Correct 409 ms 34680 KB Output is correct
82 Correct 291 ms 42488 KB Output is correct
83 Correct 165 ms 26872 KB Output is correct
84 Correct 235 ms 42616 KB Output is correct
85 Correct 505 ms 39676 KB Output is correct
86 Correct 459 ms 31736 KB Output is correct
87 Correct 397 ms 42432 KB Output is correct
88 Correct 424 ms 42232 KB Output is correct