Codeforces Round 888 (Div. 3) D Prefix Permutation Sums

83 阅读1分钟
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
void solve() {
    int n;
    cin >> n;

    vector<long long> a(n);
    for (int i = 0; i < n - 1; i++) cin >> a[i + 1];
    vector<bool> cnt(n + 1);
    vector<long long> errs;
    for (int i = 1; i < n; i++) {
        long long val = a[i] - a[i-1];
        if (val <= n && val >= 1 && !cnt[val]) {
            cnt[val] = true;
        }
        else {
            errs.push_back(val);
        }
    }

    if (errs.size() >= 2) {
        cout << "No" << '\n';
        return;
    }
    if (errs.empty()) {
        cout << "Yes" << '\n';
        return;
    }

    long long sum = 0;
    for (int i = 0; i < n; i++) {
        if (!cnt[i + 1]) sum += i + 1;
    }
    if (sum == errs[0]) cout << "YES\n";
    else cout << "NO\n";
}
int main() {
    int T;
    cin >> T;
    while(T-- > 0) {
        solve();
    }
    return 0;
}