QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#50651#4800. Oscar's Round Must Have a Constructive Problemalittlestory#AC ✓95ms11016kbC++2.5kb2022-09-28 13:38:572022-09-28 13:38:59

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-09-28 13:38:59]
  • 评测
  • 测评结果:AC
  • 用时:95ms
  • 内存:11016kb
  • [2022-09-28 13:38:57]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef long long ll;
typedef double db;
mt19937 mrand(random_device{}()); 
const ll mod=1e9+7;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a, ll b, ll mod) {ll res=1;a%=mod;
assert(b>=0);for(;b;b>>=1){if(b&1)res=res*a%mod;
a=a*a%mod;};return res;}
ll gcd(ll a, ll b) {return b?gcd(b, a%b):a;}
template<class T> 
void chkmin(T&x,T y){x=min(x,y);}
template<class T> 
void chkmax(T&x,T y){x=max(x,y);}
template<class T> 
void add(T&x,T y){x+=y;if(x>=mod)x-=mod;}
template<class T> 
void sub(T&x,T y){x-=y;if(x<0)x+=mod;}
// head 

bool cmp(pair<VI,int>& a, pair<VI,int>& b) {
    return a.fi.size() < b.fi.size();
}

bool is_per(VI& a) {
    int n = SZ(a);
    VI cnt(n, 0);
    for (int i : a)
        ++cnt[i];
    for (int i : cnt)
        if (i != 1) return false;
    return true;
}

void solve() {
    // to solve a single test case 
    int n, cnt = 0;
    cin >> n;
    VI a(n), ans(n);
    vector<pair<VI, int>> pos(n);

    for (int i = 0; i < n; i++) {
        cin >> a[i];
        --a[i];
        if (i) cnt += a[i] == a[i - 1];
        pos[a[i]].fi.pb(i);
        pos[i].se = i;
    }

    sort(all(pos), cmp);
    reverse(all(pos));



    if (cnt == n - 1) {
        cout << "NO\n";
        return;
    }

    cout << "YES\n";
    if (is_per(a)) {
        for (int i : a) 
            cout << (i + 1) % n + 1 << ' ';
        cout << '\n';
    } else {
        queue<int> res;
        int cur;
        for (int i = 0; i < n; i++)
            if (a[i] != pos[0].se) {
                res.push(i);
                cur = i;
                break;
            }

        for (auto [p, x] : pos) {
            //cout << x << ' ' << res.front() << '\n';
            ans[res.front()] = x; res.pop();
            for (int i : p)
                if (i != cur) res.push(i);
        }
        for (int i : ans) cout << i + 1 << ' ';
        cout << '\n';
    }
}   

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int tt;
    cin >> tt;
    //tt = 1;

    while (tt--) {
        solve();
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3696kb

input:

3
3
3 3 3
3
3 2 1
6
1 1 4 5 1 4

output:

NO
YES
1 3 2 
YES
4 5 1 2 6 3 

result:

ok ok

Test #2:

score: 0
Accepted
time: 55ms
memory: 3740kb

input:

50069
1
1
2
1 1
2
1 2
2
2 1
2
2 2
3
1 1 1
3
1 1 2
3
1 1 3
3
1 2 1
3
1 2 2
3
1 2 3
3
1 3 1
3
1 3 2
3
1 3 3
3
2 1 1
3
2 1 2
3
2 1 3
3
2 2 1
3
2 2 2
3
2 2 3
3
2 3 1
3
2 3 2
3
2 3 3
3
3 1 1
3
3 1 2
3
3 1 3
3
3 2 1
3
3 2 2
3
3 2 3
3
3 3 1
3
3 3 2
3
3 3 3
4
1 1 1 1
4
1 1 1 2
4
1 1 1 3
4
1 1 1 4
4
1 1 2 1
...

output:

NO
NO
YES
2 1 
YES
1 2 
NO
NO
YES
2 3 1 
YES
3 2 1 
YES
2 1 3 
YES
2 1 3 
YES
2 3 1 
YES
3 1 2 
YES
2 1 3 
YES
3 1 2 
YES
1 2 3 
YES
1 2 3 
YES
3 2 1 
YES
1 3 2 
NO
YES
3 1 2 
YES
3 1 2 
YES
3 2 1 
YES
3 2 1 
YES
1 3 2 
YES
1 2 3 
YES
1 3 2 
YES
1 3 2 
YES
2 3 1 
YES
2 3 1 
YES
1 2 3 
YES
2 1 3 
NO
...

result:

ok ok

Test #3:

score: 0
Accepted
time: 41ms
memory: 3700kb

input:

100000
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1...

output:

NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
...

result:

ok ok

Test #4:

score: 0
Accepted
time: 88ms
memory: 3528kb

input:

50000
10
3 3 3 3 3 6 3 3 3 3
10
4 5 5 5 5 5 5 5 5 5
10
8 8 8 8 8 8 8 1 8 8
10
6 6 6 6 6 6 6 6 6 2
10
4 4 4 5 4 4 4 4 4 4
10
4 5 4 4 4 10 10 4 5 10
10
8 10 10 6 4 8 4 7 10 4
10
8 8 8 8 8 8 8 8 8 8
10
4 4 4 9 10 10 10 10 4 1
10
4 4 4 4 4 1 4 4 4 4
10
5 5 5 5 6 6 5 6 5 6
10
10 10 10 10 10 10 10 10 10 1...

output:

YES
6 10 9 8 7 3 5 4 2 1 
YES
5 4 10 9 8 7 6 3 2 1 
YES
1 10 9 7 6 5 4 8 3 2 
YES
2 10 9 8 7 5 4 3 1 6 
YES
5 10 9 4 8 7 6 3 2 1 
YES
10 4 5 9 8 6 3 7 1 2 
YES
10 4 8 1 6 3 9 2 7 5 
NO
YES
10 7 6 3 4 9 1 8 5 2 
YES
1 10 9 8 7 4 6 5 3 2 
YES
6 10 9 8 5 3 7 2 4 1 
NO
NO
YES
3 6 10 9 8 4 7 2 5 1 
YES
3...

result:

ok ok

Test #5:

score: 0
Accepted
time: 68ms
memory: 3756kb

input:

5000
100
37 37 37 58 58 58 58 58 58 58 37 58 37 37 37 58 37 37 37 37 58 58 37 37 37 37 58 37 58 58 58 58 37 58 58 58 37 58 37 37 37 37 58 37 37 37 58 37 37 58 37 37 37 58 37 37 58 37 58 58 58 58 37 37 58 58 58 37 37 58 58 37 37 58 37 58 58 37 58 58 58 37 58 58 37 58 58 58 37 58 58 58 37 58 58 37 58 ...

output:

YES
58 80 81 37 27 28 29 30 31 32 82 33 83 84 85 34 86 87 51 89 35 36 90 91 92 93 50 94 39 40 41 42 95 43 44 45 96 46 97 98 99 100 47 88 1 52 48 53 54 49 55 56 57 38 59 60 2 61 3 4 5 6 62 76 7 8 9 64 65 10 11 66 67 12 68 26 14 69 15 16 17 70 18 19 71 20 21 22 72 23 24 25 73 13 77 74 78 75 63 79 
YES...

result:

ok ok

Test #6:

score: 0
Accepted
time: 83ms
memory: 3776kb

input:

500
1000
452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452 452...

output:

NO
NO
NO
YES
657 342 341 340 339 338 337 336 335 334 333 332 331 330 500 328 327 326 325 324 323 322 321 320 319 318 317 316 315 314 329 373 372 371 370 369 368 367 366 365 364 363 362 361 360 343 358 357 356 355 354 353 352 351 350 349 348 347 346 345 344 359 281 280 279 278 277 276 275 274 273 272...

result:

ok ok

Test #7:

score: 0
Accepted
time: 73ms
memory: 4512kb

input:

50
10000
9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9931 9...

output:

YES
1755 3338 3337 3336 3335 3334 3333 3332 3331 3330 3339 3328 3327 3326 3325 3324 3323 3322 3329 3358 3357 3356 3355 3354 3353 3352 3351 3350 3340 3348 3347 3346 3345 3344 3343 3342 3341 3282 3349 3300 3299 3298 3297 3296 3295 3294 3293 3321 3291 3290 3289 3288 3287 3286 3285 3284 3283 3292 3320 3...

result:

ok ok

Test #8:

score: 0
Accepted
time: 75ms
memory: 5620kb

input:

20
25000
2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2181 2...

output:

YES
15657 8329 8330 8331 8332 8333 8334 8335 8336 8337 8351 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8338 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8328 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8315 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 ...

result:

ok ok

Test #9:

score: 0
Accepted
time: 95ms
memory: 7312kb

input:

10
50000
32825 31708 22702 32825 22702 31708 32825 32825 9333 31708 32825 46864 22702 32825 31708 31708 22702 22702 31708 46864 9333 9333 1785 31708 22702 9333 1785 32825 31708 22702 46864 32825 9333 46864 9333 35050 31708 1785 46864 9333 32825 1785 22702 31708 22702 1785 46864 32825 1785 35050 9333...

output:

YES
9333 5503 22240 11590 22239 5502 11589 11331 22702 5501 11587 27910 22251 11586 5500 5499 22188 22187 5498 27909 31708 32825 44228 5497 22186 35050 44227 11585 5496 22185 27908 11584 1785 27907 46864 39062 5495 44213 27906 8079 11583 44225 22184 5507 22183 44224 27905 11582 44223 39061 16671 390...

result:

ok ok

Test #10:

score: 0
Accepted
time: 78ms
memory: 11016kb

input:

5
100000
25575 25575 25575 25575 38740 38740 25575 38740 25575 38740 25575 25575 25575 38740 38740 38740 25575 38740 25575 25575 25575 25575 38740 38740 38740 38740 25575 25575 25575 38740 38740 25575 25575 38740 25575 38740 25575 38740 38740 25575 38740 38740 25575 38740 25575 25575 38740 38740 255...

output:

YES
38740 83329 83330 83331 25575 92905 83332 33328 83333 33329 83334 83335 83336 33330 33331 33332 83337 33333 50001 83339 83340 83341 33334 33335 33336 33327 83342 83343 83344 33338 33339 83345 83346 33340 83347 33341 83348 33342 33343 83349 33344 33345 83338 33346 83303 83304 33347 33348 83305 83...

result:

ok ok