Submission #1140854

#TimeUsernameProblemLanguageResultExecution timeMemory
1140854SmuggingSpunRemittance (JOI19_remittance)C++20
100 / 100
232 ms16092 KiB
#include<bits/stdc++.h>
#define taskname "B"
using namespace std;
typedef long long ll;
const int lim = 1e6 + 5;
const ll INF = 1e16;
int n, a[lim], b[lim];
namespace sub1{
	void solve(){
		vector<vector<int>>state(1);
		int d = 0;
		for(int i = 1; i <= n; i++){
			state[0].emplace_back(a[i]);
			d += a[i] - b[i];
		}
		if(d < 0){
			return void(cout << "No");
		}
		for(int _ = 0; _ < d; _++){
			vector<vector<int>>next_state;
			for(auto& ve : state){
				for(int i = 0; i < n; i++){
					if(ve[i] > 1){
						ve[i] -= 2;
						ve[i == n - 1 ? 0 : i + 1]++;
						next_state.emplace_back(ve);
						ve[i] += 2;
						ve[i == n - 1 ? 0 : i + 1]--;
					}
				}
			}
			swap(state, next_state);
			sort(state.begin(), state.end());
			state.resize(unique(state.begin(), state.end()) - state.begin());
		}
		vector<int>target;
		for(int i = 1; i <= n; i++){
			target.emplace_back(b[i]);
		}
		cout << (binary_search(state.begin(), state.end(), target) ? "Yes" : "No");
	}
}
namespace sub2{
	void solve(){
		ll x = 0;
		for(int i = 1; i <= n; i++){
			x += (1LL << (i - 1)) * (a[i] - b[i]);
		}
		if(x < 0 || x % ((1 << n) - 1) != 0){
			return void(cout << "No");
		}
		x /= (1 << n) - 1;
		for(int i = n; i > 1; i--){
			if((x = (x << 1LL) + b[i] - a[i]) < 0){
				return void(cout << "No");
			}
			if(x > INF){
				break;
			}
		}
		cout << "Yes";
	}
}
namespace sub3{
	ll cnt[lim];
	void solve(){
		for(int i = 1; i <= n; i++){
			cnt[i - 1] += a[i] - b[i];
		}
		bool loop = true;
		while(loop){
			loop = false;
			for(int i = 0; i < n; i++){
				if(abs(cnt[i] > 1)){
					ll d = cnt[i] / 2LL;
					cnt[i == n - 1 ? 0 : i + 1] += d;
					cnt[i] -= d << 1LL;
					loop = true;
				}
			}
		}
		for(int i = 1; i < n; i++){
			if(cnt[i] != cnt[0]){
				return void(cout << "No");
			}
		}
		ll low = 0, high = 2e9, x = -1;
		while(low <= high){
			ll mid = (low + high) >> 1LL;
			for(int i = 1; i <= n; i++){
				cnt[i - 1] = a[i] - b[i] - mid;
			}
			for(int i = 0; i + 1 < n; i++){
				cnt[i + 1] += cnt[i] / 2LL;
				cnt[i] %= 2LL;
			}
			bool flag = true;
			for(int i = n - 1; i > -1; i--){
				if(cnt[i] != 0){
					if(cnt[i] < 0){
						flag = false;
					}
					break;
				}
			}
			if(flag){
				low = (x = mid) + 1;
			}
			else{
				high = mid - 1;
			}
		}
		if(x == -1){
			return void(cout << "No");
		}
		for(int i = n; i > 1; i--){
			if((x = (x << 1LL) + b[i] - a[i]) < 0){
				return void(cout << "No");
			}
			if(x > INF){
				break;
			}
		}
		cout << "Yes";
	}
}
int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	if(fopen(taskname".inp", "r")){
		freopen(taskname".inp", "r", stdin);
	}
	cin >> n;
	for(int i = 1; i <= n; i++){
		cin >> a[i] >> b[i];
	}
	if(n <= 7 && max(*max_element(a + 1, a + n + 1), *max_element(b + 1, b + n + 1)) <= 7){
		sub1::solve();
	}
	else if(*max_element(b + 1, b + n + 1) == 0){
		cout << (*max_element(a + 1, a + n + 1) == 0 ? "Yes" : "No");
	}
	else if(n <= 20){
		sub2::solve();
	}
	else{
		sub3::solve();
	}
}

Compilation message (stderr)

remittance.cpp: In function 'int main()':
remittance.cpp:130:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  130 |                 freopen(taskname".inp", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...