codeforces1373F. Network Coverage

可以二分b[1]给a[1]的值,看了standing学了种O(n)的办法,每次求a[i]-b[i-1],若大于0,则累加到下一次(a[i]只能有b[i]和b[i+1])组成,然后判断这个值和b[i]的大小

#include 
using namespace std;
typedef long long ll;

int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	int t;
	cin >> t;
	while (t--) {
		int n;
		cin >> n;
		vector a(n), b(n);
		ll balance = 0;
		for (ll& v : a) cin >> v, balance += v;
		for (ll& v : b) cin >> v, balance -= v;
		ll worst = 0, ok = 1;
		for (int i = 1; i <2 * n; i++) {
			worst = max(0ll, worst) + a[i % n] - b[(i - 1) % n];
			if (worst > b[i % n]) ok = 0;
		}
		cout << (balance > 0 || ok == 0 ? "NO" : "YES") << endl;
	}
}

 

你可能感兴趣的:(算法)