Submission #401471

# Submission time Handle Problem Language Result Execution time Memory
401471 2021-05-10T10:44:06 Z iulia13 Sorting (IOI15_sorting) C++14
100 / 100
174 ms 16588 KB
#include "sorting.h"
#include <iostream>


using namespace std;
const int N = 2e5;
int ss[N];
int poz[N];
int nr;
int pp[3 * N];
int qq[3 * N];
bool check(int n, int m, int s[], int x[], int y[])
{
    int i; nr = 0;
    for (i = 0; i < n; i++)
        ss[i] = s[i];
    for (i = 0; i < m; i++)
        swap(ss[x[i]], ss[y[i]]);
    for (i = 0; i < n; i++)
        poz[ss[i]] = i;
    for (i = 0; i < n; i++)
        if (ss[i] != i)
        {
            poz[ss[i]] = poz[i];
            ss[poz[i]] = ss[i];
            nr++;
            pp[nr - 1] = i;
            qq[nr - 1] = ss[i];
        }
    if (nr <= m)
    {
        for(i = nr; i < m; i++)
            pp[i] = qq[i] = 0;
        return true;
    }
    return false;
}
int findSwapPairs(int n, int s[], int m, int x[], int y[], int p[], int q[])
{
    int i, ok = 1;
    for (i = 0; i < n && ok; i++)
        if (s[i] != i)
            ok = 0;
    if (ok)
        return 0;
    int st = 1, dr = m, sol;
    while (st <= dr)
    {
        int mid = (st + dr) / 2;
        if (check(n, mid, s, x, y))
            dr = mid - 1, sol = mid;
        else
            st = mid + 1;
    }
    check(n, sol, s, x, y);
    m = sol;
    for (i = 0; i < n; i++)
        poz[s[i]] = i;
    for (i = 0; i < m; i++)
    {
        swap(poz[s[x[i]]], poz[s[y[i]]]);
        swap(s[x[i]], s[y[i]]);
        p[i] = poz[pp[i]];
        q[i] = poz[qq[i]];
        swap(poz[s[p[i]]], poz[s[q[i]]]);
        swap(s[p[i]], s[q[i]]);
    }
    return m;
}/*
int s[N];
int x[N];
int y[N];
int p[N];
int q[N];

int main()
{
    int n, i, m;
    cin >> n;
    for (i = 0; i < n; i++)
        cin >> s[i];
    cin >> m;
    for (i = 0; i < n; i++)
        cin >> x[i] >> y[i];
    cout << findSwapPairs(n, s, m, x, y, p, q);
    return 0;
}*/

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:46:25: warning: 'sol' may be used uninitialized in this function [-Wmaybe-uninitialized]
   46 |     int st = 1, dr = m, sol;
      |                         ^~~
# 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 308 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
6 Correct 1 ms 332 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 308 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
6 Correct 1 ms 332 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 296 KB Output is correct
11 Correct 1 ms 300 KB Output is correct
12 Correct 1 ms 300 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 336 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 308 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
6 Correct 1 ms 332 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 296 KB Output is correct
11 Correct 1 ms 300 KB Output is correct
12 Correct 1 ms 300 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 336 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 2 ms 588 KB Output is correct
22 Correct 2 ms 588 KB Output is correct
23 Correct 2 ms 588 KB Output is correct
24 Correct 2 ms 588 KB Output is correct
25 Correct 2 ms 588 KB Output is correct
26 Correct 2 ms 588 KB Output is correct
27 Correct 2 ms 588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 2 ms 460 KB Output is correct
3 Correct 2 ms 460 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 2 ms 452 KB Output is correct
6 Correct 2 ms 460 KB Output is correct
7 Correct 2 ms 460 KB Output is correct
8 Correct 2 ms 516 KB Output is correct
9 Correct 2 ms 584 KB Output is correct
10 Correct 2 ms 460 KB Output is correct
11 Correct 2 ms 460 KB Output is correct
12 Correct 2 ms 452 KB Output is correct
13 Correct 2 ms 460 KB Output is correct
14 Correct 2 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 2 ms 460 KB Output is correct
3 Correct 2 ms 460 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 2 ms 452 KB Output is correct
6 Correct 2 ms 460 KB Output is correct
7 Correct 2 ms 460 KB Output is correct
8 Correct 2 ms 516 KB Output is correct
9 Correct 2 ms 584 KB Output is correct
10 Correct 2 ms 460 KB Output is correct
11 Correct 2 ms 460 KB Output is correct
12 Correct 2 ms 452 KB Output is correct
13 Correct 2 ms 460 KB Output is correct
14 Correct 2 ms 460 KB Output is correct
15 Correct 141 ms 15056 KB Output is correct
16 Correct 153 ms 15396 KB Output is correct
17 Correct 169 ms 16068 KB Output is correct
18 Correct 42 ms 8096 KB Output is correct
19 Correct 124 ms 14072 KB Output is correct
20 Correct 135 ms 14456 KB Output is correct
21 Correct 136 ms 14608 KB Output is correct
22 Correct 126 ms 16328 KB Output is correct
23 Correct 149 ms 16488 KB Output is correct
24 Correct 174 ms 16312 KB Output is correct
25 Correct 170 ms 16068 KB Output is correct
26 Correct 133 ms 14568 KB Output is correct
27 Correct 121 ms 13972 KB Output is correct
28 Correct 172 ms 16156 KB Output is correct
29 Correct 164 ms 15500 KB Output is correct
30 Correct 107 ms 13336 KB Output is correct
31 Correct 164 ms 15888 KB Output is correct
32 Correct 151 ms 16000 KB Output is correct
33 Correct 173 ms 16156 KB Output is correct
34 Correct 160 ms 16308 KB Output is correct
35 Correct 139 ms 14076 KB Output is correct
36 Correct 63 ms 12268 KB Output is correct
37 Correct 174 ms 16588 KB Output is correct
38 Correct 163 ms 15972 KB Output is correct
39 Correct 164 ms 16104 KB Output is correct