Rating changes for last rounds are temporarily rolled back. They will be returned soon. ×

comingsoon.cpp's blog

By comingsoon.cpp, history, 5 hours ago, In English

What exactly am I not getting right? I've been trying to solve this problem for so long now and so far, a correct solution still eludes me despite all my best debugging efforts. Someone please help me identify the flaw in this logic.

#include <iostream>
#include <vector>
#define nl '\n'
#define ll long long
using namespace std;
ll bin(ll l , ll r , ll t){
	while(l <= r){
		ll mid = l + (r - l) / 2;
		if(mid == t){
			return mid + 1;
		}
		if(mid < t){
			l = mid + 1;
		}else{
			r = mid - 1;
		}
	}
	return 0;
}
static void paveway(){
	ll n , m; cin >> n >> m; vector<ll> a(n); vector<ll> b(m); vector<ll> c(n);
	for(ll i = 0; i < n; i++) {cin >> a[i];}
	for(ll i = 0; i < m; i++){cin >> b[i];} sort(b.begin(),b.end());
	a[0] = min(a[0] , b[0] - a[0]);
	a[n-1] = max(a[n-1] , b[m-1] - a[n-1]);
	for(ll i = 1; i < n - 1; i++){
		if(a[i] > a[i-1]){
			ll l = a[i-1] + a[i] , r = 2 * a[i] , res = 0;
			for(ll j = 0; j < m; j++){
				res = bin(l , r , b[j]);
				if(res){
					a[i] = b[j] - a[i];
					break;
				}
			}
		}
		if(a[i] < a[i-1]){
			ll l = a[i-1] + a[i] , r = max(a[i+1]+a[i], l) , res = 0;
			for(ll j = 0; j < m; j++){
				res = bin(l , r , b[j]);
				if(res){
					a[i] = b[j] - a[i];
					break;
				}
			}
			if(!res) break;
		}
	}
	for(ll i = 1; i < n; i++){
		if(a[i] < a[i-1]){
			cout << "NO" << nl;
			return;
		}
	}
	cout << "YES" << nl;
} 
int main(){ 
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	//ll t = 1;
	ll t; cin >> t;
	for(;t--;)
	paveway();
return 0;
}

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
5 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

It is often a good idea not only to make your decision understood by others, but also to understand it yourself and find the error, you need to write comments in important places of the code.