Submission #532835

# Submission time Handle Problem Language Result Execution time Memory
532835 2022-03-04T03:34:11 Z ivan24 Sorting (IOI15_sorting) C++14
100 / 100
706 ms 27948 KB
#include "sorting.h"
#include<bits/stdc++.h>
using namespace std;

using ll = long long int;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll,ll> ii;
typedef vector<ii> vii;
typedef vector<vii> vvii;
#define F first
#define S second

ll min (ll x, ll y){
    return ((x < y) ? x : y);
}

ll max (ll x, ll y){
    return ((x > y) ? x : y);
}

const ll INF = 1e18;

int findSwapPairs(int n, int s[], int m, int x[], int y[], int p[], int q[]) {
    cout.setstate(ios_base::failbit);

    p[0] = 0; q[0] = 1;
    bool perfect = true;
    for (ll i = 0; n > i; i++){
        if (s[i] != i) perfect = false;
    }
    if (perfect) return 0;

    ll lo = 1,hi = m,md;
    md = (lo+hi/2);
    while (hi >= lo){
        md = (lo+hi)/2;
        cout << "md: " << md << endl;
        vi v,v_idx;
        v.resize(n);
        v_idx.resize(n);
        for (ll i = 0; n > i; i++){
            v[i] = s[i]; v_idx[v[i]] = i;
        }
        for (ll i = 0; md > i; i++){
            swap(v[x[i]],v[y[i]]);
            swap(v_idx[v[x[i]]],v_idx[v[y[i]]]);
        }
        vi cur,cur_idx;
        cur.resize(n);
        cur_idx.resize(n);
        vii swaps;
        swaps.assign(md,ii(0,0));
        for (ll i = 0; n > i; i++){
            cur[i] = i; cur_idx[i] = i;
        }
        /*
        cout << "v: ";
        for (auto i: v) cout << i << " ";
        cout << endl;
        cout << "v_idx: ";
        for (auto i: v_idx) cout << i << " ";
        cout << endl;
        cout << "cur: ";
        for (auto i: cur) cout << i << " ";
        cout << endl;
        cout << "cur_idx: ";
        for (auto i: cur_idx) cout << i << " ";
        cout << endl;
        */
        ll ptr = 0;
        for (ll i = md-1; i >= 0; i--){
            while (n > ptr && cur_idx[ptr] == v_idx[ptr]) ptr++;
            if (ptr >= n) continue;
            //cout << ptr << endl;
            swaps[i] = ii(cur_idx[ptr],cur_idx[v[cur_idx[ptr]]]);
            swap(cur[cur_idx[ptr]],cur[cur_idx[v[cur_idx[ptr]]]]);
            swap(cur_idx[ptr],cur_idx[v[cur_idx[ptr]]]);
            /*
            cout << "swapped: " << swaps[i].F << " and " << swaps[i].S << " ptr: " << ptr << endl;
            cout << "cur: ";
            for (auto i: cur) cout << i << " ";
            cout << endl;
            cout << "cur_idx: ";
            for (auto i: cur_idx) cout << i << " ";
            cout << endl;
            cout << "v: ";
            for (auto i: v) cout << i << " ";
            cout << endl;
            cout << "v_idx: ";
            for (auto i: v_idx) cout << i << " ";
            cout << endl;
            */

            swap(v_idx[v[x[i]]], v_idx[v[y[i]]]);
            swap(v[x[i]],v[y[i]]);
            swap(cur_idx[cur[x[i]]],cur_idx[cur[y[i]]]);
            swap(cur[x[i]],cur[y[i]]);
        }
        bool works = true;
        for (ll i = 0; n > i; i++) works = works && (cur[i] == v[i]);
        /*
        cout << "after: \n";
        cout << "v: ";
        for (auto i: v) cout << i << " ";
        cout << endl;
        cout << "v_idx: ";
        for (auto i: v_idx) cout << i << " ";
        cout << endl;
        cout << "cur: ";
        for (auto i: cur) cout << i << " ";
        cout << endl;
        cout << "cur_idx: ";
        for (auto i: cur_idx) cout << i << " ";
        cout << endl;
        */
        if (works){
            hi = md;
            for (ll i = 0; md > i; i++){
                p[i] = swaps[i].F;
                q[i] = swaps[i].S;
            }
        }else
            lo = md+1;
        if (hi == lo && hi == md) return md;
    }
	return md;
}

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:11:11: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
   11 | #define F first
sorting.cpp:120:33: note: in expansion of macro 'F'
  120 |                 p[i] = swaps[i].F;
      |                                 ^
sorting.cpp:12:11: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
   12 | #define S second
sorting.cpp:121:33: note: in expansion of macro 'S'
  121 |                 q[i] = swaps[i].S;
      |                                 ^
sorting.cpp:125:42: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  125 |         if (hi == lo && hi == md) return md;
      |                                          ^~
sorting.cpp:127:9: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  127 |  return md;
      |         ^~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 296 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 292 KB Output is correct
5 Correct 1 ms 292 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 296 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 292 KB Output is correct
5 Correct 1 ms 292 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 288 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 300 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 300 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 292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 296 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 292 KB Output is correct
5 Correct 1 ms 292 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 288 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 300 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 300 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 292 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 684 KB Output is correct
24 Correct 2 ms 588 KB Output is correct
25 Correct 2 ms 712 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 460 KB Output is correct
2 Correct 3 ms 560 KB Output is correct
3 Correct 2 ms 460 KB Output is correct
4 Correct 1 ms 300 KB Output is correct
5 Correct 2 ms 432 KB Output is correct
6 Correct 2 ms 476 KB Output is correct
7 Correct 2 ms 560 KB Output is correct
8 Correct 3 ms 412 KB Output is correct
9 Correct 3 ms 460 KB Output is correct
10 Correct 3 ms 428 KB Output is correct
11 Correct 4 ms 460 KB Output is correct
12 Correct 2 ms 460 KB Output is correct
13 Correct 3 ms 488 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 460 KB Output is correct
2 Correct 3 ms 560 KB Output is correct
3 Correct 2 ms 460 KB Output is correct
4 Correct 1 ms 300 KB Output is correct
5 Correct 2 ms 432 KB Output is correct
6 Correct 2 ms 476 KB Output is correct
7 Correct 2 ms 560 KB Output is correct
8 Correct 3 ms 412 KB Output is correct
9 Correct 3 ms 460 KB Output is correct
10 Correct 3 ms 428 KB Output is correct
11 Correct 4 ms 460 KB Output is correct
12 Correct 2 ms 460 KB Output is correct
13 Correct 3 ms 488 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 373 ms 24540 KB Output is correct
16 Correct 513 ms 25012 KB Output is correct
17 Correct 656 ms 26428 KB Output is correct
18 Correct 41 ms 13328 KB Output is correct
19 Correct 341 ms 27140 KB Output is correct
20 Correct 373 ms 27808 KB Output is correct
21 Correct 346 ms 27820 KB Output is correct
22 Correct 428 ms 21868 KB Output is correct
23 Correct 470 ms 27432 KB Output is correct
24 Correct 688 ms 27012 KB Output is correct
25 Correct 572 ms 26668 KB Output is correct
26 Correct 414 ms 27900 KB Output is correct
27 Correct 295 ms 27208 KB Output is correct
28 Correct 632 ms 27752 KB Output is correct
29 Correct 544 ms 27296 KB Output is correct
30 Correct 256 ms 27416 KB Output is correct
31 Correct 633 ms 27928 KB Output is correct
32 Correct 594 ms 26424 KB Output is correct
33 Correct 706 ms 27044 KB Output is correct
34 Correct 642 ms 26944 KB Output is correct
35 Correct 388 ms 26676 KB Output is correct
36 Correct 95 ms 27556 KB Output is correct
37 Correct 676 ms 27948 KB Output is correct
38 Correct 685 ms 26700 KB Output is correct
39 Correct 700 ms 26852 KB Output is correct