【构造】CF1853D

Problem - D - Codeforces

题意:

【构造】CF1853D_第1张图片

思路:

【构造】CF1853D_第2张图片 

【构造】CF1853D_第3张图片 

【构造】CF1853D_第4张图片 

Code:

#include 

using i64 = long long;

const int N = 1e6 + 10;

int ans[N];

void solve() {
    int n;
    std::cin >> n;

    std::vector > a(n + 1);
    for (int i = 1; i <= n; i ++) {
        std::cin >> a[i].first;
        a[i].second = i;
    }

    std::sort(a.begin() + 1, a.end());

    int l = 1, r = n;
    int idx = n;
    while(l <= r) {
        if (a[l].first == n - r) {
            ans[a[l ++].second] = -idx;
        }else if (a[r].first == n - l + 1) {
            ans[a[r --].second] = idx;
        }else {
            std::cout << "NO" << "\n";
            return;
        }
        idx --;
    }
    std::cout << "YES" << "\n";
    for (int i = 1; i <= n; i ++) {
        std::cout << ans[i] << " \n" [i == n];
    }
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int t;
    std::cin >> t;
    
    while (t--) {
        solve();
    }
    
    return 0;
}

 

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