Submission #791253

# Submission time Handle Problem Language Result Execution time Memory
791253 2023-07-23T19:38:11 Z TimDee Sorting (IOI15_sorting) C++17
100 / 100
98 ms 21644 KB
#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
#define forn(i,n) for(int i=0;i<n;++i)
#define pb push_back
#define pi pair<int,int>
#define f first
#define s second

int findSwapPairs(int n, int _p[], int m, int x[], int y[], int P[], int Q[]) {

    int z=1;
    forn(i,n) if (_p[i]!=i) z=0;
    if (z) return 0;

    int l=1, r=n; 
    while (l<r) {
        int m=(l+r)>>1;
        vector<int> p(n);
        forn(i,n) p[i]=_p[i];
        forn(i,m) {
            swap(p[x[i]],p[y[i]]);
        }
        vector<int> pos(n);
        forn(i,n) pos[p[i]]=i;

        int z=0;
        forn(i,n) {
            if (p[i]!=i) {
                z++;
                pos[p[i]]=pos[i];
                swap(p[pos[i]],p[i]);
            }
        }
        if (z<=m) r=m;
        else l=m+1;
    }

    for (int k=r; k<=m; ++k) {
        vector<int> p(n);
        forn(i,n) p[i]=_p[i];
        auto a=p;
        forn(i,k) {
            swap(p[x[i]],p[y[i]]);
        }
        vector<int> pos(n);
        forn(i,n) pos[p[i]]=i;

        vector<pi> v;
        forn(i,n) {
            if (p[i]!=i) {
                v.pb({i,p[i]});
                pos[p[i]]=pos[i];
                swap(p[pos[i]],p[i]);
            }
        }

        if (v.size()<=k) {
            while (v.size()<k) v.pb({0,0});
            forn(i,n) pos[a[i]]=i;
            vector<pi> e;
            forn(i,k) {
                swap(pos[a[x[i]]],pos[a[y[i]]]);
                swap(a[x[i]],a[y[i]]);
                e.pb({pos[v[i].f],pos[v[i].s]});
                swap(pos[v[i].f],pos[v[i].s]);
                swap(a[pos[v[i].f]],a[pos[v[i].s]]);
            }
            forn(i,k) P[i]=e[i].f, Q[i]=e[i].s;
            return k;
        }
    }
    return 0;

}

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:18:13: warning: declaration of 'int m' shadows a parameter [-Wshadow]
   18 |         int m=(l+r)>>1;
      |             ^
sorting.cpp:10:40: note: shadowed declaration is here
   10 | int findSwapPairs(int n, int _p[], int m, int x[], int y[], int P[], int Q[]) {
      |                                    ~~~~^
sorting.cpp:27:13: warning: declaration of 'z' shadows a previous local [-Wshadow]
   27 |         int z=0;
      |             ^
sorting.cpp:12:9: note: shadowed declaration is here
   12 |     int z=1;
      |         ^
sorting.cpp:58:21: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   58 |         if (v.size()<=k) {
      |             ~~~~~~~~^~~
sorting.cpp:59:28: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   59 |             while (v.size()<k) v.pb({0,0});
      |                    ~~~~~~~~^~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 296 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 300 KB Output is correct
10 Correct 1 ms 312 KB Output is correct
11 Correct 1 ms 304 KB Output is correct
12 Correct 1 ms 296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 308 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 296 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 300 KB Output is correct
10 Correct 1 ms 312 KB Output is correct
11 Correct 1 ms 304 KB Output is correct
12 Correct 1 ms 296 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 308 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 440 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 440 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 436 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 440 KB Output is correct
9 Correct 1 ms 404 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 440 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 436 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 440 KB Output is correct
9 Correct 1 ms 404 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 76 ms 19324 KB Output is correct
16 Correct 82 ms 19736 KB Output is correct
17 Correct 95 ms 20556 KB Output is correct
18 Correct 39 ms 13352 KB Output is correct
19 Correct 80 ms 19704 KB Output is correct
20 Correct 83 ms 20400 KB Output is correct
21 Correct 84 ms 20288 KB Output is correct
22 Correct 65 ms 16020 KB Output is correct
23 Correct 96 ms 21376 KB Output is correct
24 Correct 96 ms 21120 KB Output is correct
25 Correct 92 ms 20752 KB Output is correct
26 Correct 84 ms 20336 KB Output is correct
27 Correct 78 ms 19564 KB Output is correct
28 Correct 90 ms 20980 KB Output is correct
29 Correct 88 ms 20660 KB Output is correct
30 Correct 70 ms 18472 KB Output is correct
31 Correct 92 ms 21032 KB Output is correct
32 Correct 85 ms 20692 KB Output is correct
33 Correct 91 ms 21140 KB Output is correct
34 Correct 90 ms 20992 KB Output is correct
35 Correct 79 ms 19484 KB Output is correct
36 Correct 45 ms 16636 KB Output is correct
37 Correct 96 ms 21644 KB Output is correct
38 Correct 98 ms 20860 KB Output is correct
39 Correct 96 ms 20972 KB Output is correct