Submission #438874

# Submission time Handle Problem Language Result Execution time Memory
438874 2021-06-28T20:55:17 Z NintsiChkhaidze Sorting (IOI15_sorting) C++14
100 / 100
723 ms 28112 KB
#include "sorting.h"
#include <iostream>
#define s second
#define f first
#define pii pair<int,int>
using namespace std;
int idx[200005],a[200005];
pii d[600005];
 
int findSwapPairs(int N, int S[], int M, int x[], int y[], int p[], int q[]) {
    bool c = 1;
    for (int i=0;i<N;i++)
        if (S[i] != i) c=0;
    
    if (c) return 0;
    
    int l = 1,r = M,ans = 0;
    while(l <= r){
        int mid = (l+r)>>1;
        
        for (int i=0;i<N;i++){
            a[i] = S[i];
            idx[S[i]] = i;
        }
        
        for (int i = 0; i < mid; i++){
            swap(a[x[i]],a[y[i]]);
            swap(idx[a[x[i]]],idx[a[y[i]]]);
        }
       
        int cnt = 0;
        for (int i = 0; i < N; i++){
            if (a[i] == i) continue;
            
            d[cnt] = {a[i],i};
            cnt++;
            
            int ind = idx[i];
            swap(idx[a[i]],idx[i]);
            swap(a[i],a[ind]);
        }
        
        for (int i = cnt; i < M; i++)
          d[i] = {0,0};
        
        if (cnt <= mid){
            ans = mid;
            for (int i=0;i<N;i++){
                a[i] = S[i];
                idx[S[i]] = i;
            }
            
            for (int i = 0; i < M; i++){
                swap(a[x[i]],a[y[i]]);
                swap(idx[a[x[i]]],idx[a[y[i]]]);
                
                p[i] = d[i].f; q[i] = d[i].s;
                swap(idx[p[i]],idx[q[i]]);
                p[i] = idx[p[i]]; q[i] = idx[q[i]];
                swap(a[p[i]],a[q[i]]);
             }
            r = mid - 1;
        }
        else {
            l = mid + 1;
        }
    }
    
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 5 ms 588 KB Output is correct
22 Correct 5 ms 588 KB Output is correct
23 Correct 5 ms 588 KB Output is correct
24 Correct 4 ms 588 KB Output is correct
25 Correct 4 ms 588 KB Output is correct
26 Correct 5 ms 652 KB Output is correct
27 Correct 4 ms 588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 460 KB Output is correct
2 Correct 3 ms 460 KB Output is correct
3 Correct 3 ms 460 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 2 ms 460 KB Output is correct
6 Correct 3 ms 460 KB Output is correct
7 Correct 3 ms 460 KB Output is correct
8 Correct 3 ms 460 KB Output is correct
9 Correct 3 ms 460 KB Output is correct
10 Correct 3 ms 460 KB Output is correct
11 Correct 3 ms 460 KB Output is correct
12 Correct 2 ms 472 KB Output is correct
13 Correct 3 ms 460 KB Output is correct
14 Correct 3 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 460 KB Output is correct
2 Correct 3 ms 460 KB Output is correct
3 Correct 3 ms 460 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 2 ms 460 KB Output is correct
6 Correct 3 ms 460 KB Output is correct
7 Correct 3 ms 460 KB Output is correct
8 Correct 3 ms 460 KB Output is correct
9 Correct 3 ms 460 KB Output is correct
10 Correct 3 ms 460 KB Output is correct
11 Correct 3 ms 460 KB Output is correct
12 Correct 2 ms 472 KB Output is correct
13 Correct 3 ms 460 KB Output is correct
14 Correct 3 ms 460 KB Output is correct
15 Correct 357 ms 17152 KB Output is correct
16 Correct 411 ms 25284 KB Output is correct
17 Correct 554 ms 26712 KB Output is correct
18 Correct 47 ms 13380 KB Output is correct
19 Correct 525 ms 26052 KB Output is correct
20 Correct 516 ms 26812 KB Output is correct
21 Correct 435 ms 26848 KB Output is correct
22 Correct 324 ms 22208 KB Output is correct
23 Correct 404 ms 27716 KB Output is correct
24 Correct 614 ms 27372 KB Output is correct
25 Correct 543 ms 26984 KB Output is correct
26 Correct 553 ms 26820 KB Output is correct
27 Correct 610 ms 26180 KB Output is correct
28 Correct 527 ms 27252 KB Output is correct
29 Correct 633 ms 26728 KB Output is correct
30 Correct 511 ms 25828 KB Output is correct
31 Correct 723 ms 27492 KB Output is correct
32 Correct 423 ms 26712 KB Output is correct
33 Correct 635 ms 27328 KB Output is correct
34 Correct 487 ms 27256 KB Output is correct
35 Correct 490 ms 25628 KB Output is correct
36 Correct 424 ms 25284 KB Output is correct
37 Correct 631 ms 28112 KB Output is correct
38 Correct 591 ms 26916 KB Output is correct
39 Correct 602 ms 27088 KB Output is correct