Submission #580008

# Submission time Handle Problem Language Result Execution time Memory
580008 2022-06-20T13:17:00 Z AugustinasJucas Sorting (IOI15_sorting) C++14
100 / 100
165 ms 30160 KB
#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;

int n; int m;
vector<int> mas;
vector<pair<int, int> > swaps;

vector<int> simuliuok(int kiek) {
    vector<int> ret(n);
    for(int i = 0; i < n; i++) ret[i] = i;
    for(int i = 0; i < kiek; i++) {
        swap(ret[swaps[i].first], ret[swaps[i].second]);
    }
    return ret;
}
vector<int> iKuriaLekstutePerkelti(int kiek){
    vector<int> kuriLekstuteCiaAtsidurs = simuliuok(kiek);
  //  cout << "simluliantas: "; for(auto &x : kuriLekstuteCiaAtsidurs) cout << x << " ";
  //  cout << endl;
    vector<int> kurEisiu (n);
    for(int i = 0; i < n; i++) {
        kurEisiu[i] = kuriLekstuteCiaAtsidurs[mas[i]];
    }
    return kurEisiu;
}

bool f(int kiek) {
    vector<int> kurEisiu = iKuriaLekstutePerkelti(kiek);
    vector<bool> vis(n, false);
    int s = 0;
    for(int i = 0; i < n; i++) {
        if(vis[i]) continue;
        for(int j = kurEisiu[i]; j != i; j = kurEisiu[j]) {
            vis[j] = true;
            s++;
        }
        vis[i] = true;
    }
    return s <= kiek;
}
vector<pair<int, int> > raskEiliskuma(vector<int> kurEisiu) {
    vector<bool> vis(n, false);
    vector<pair<int, int> > ret;
    for(int i = 0; i < n; i++) {
        if(vis[i]) continue;
        vis[i] = true;
        vector<int> ratas = {i};
        for(int j = kurEisiu[i]; j != i; j = kurEisiu[j]) {
            ratas.push_back(j);
            vis[j] = true;
        }
     //   cout << "ratas: "; for(auto &x : ratas) cout << x << " ";
   //     cout << endl;
        for(int i = ratas.size() - 1; i > 0; i--) {
            ret.push_back({ratas[i], ratas[i-1]});
        }
    }
    return ret;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
    n = N; m = M;
    for(int i = 0; i < n; i++) {
        mas.push_back(S[i]);
    }
    for(int i = 0; i < m; i++) {
        swaps.push_back({X[i], Y[i]});
    }
    int ans;
    int l = 0; int r = m; int mid;
    while(l <= r) {
        mid = (l + r) / 2;
        if(f(mid)) {
            ans = mid;
            r = mid-1;
        }else {
            l = mid+1;
        }
    }

    vector<int> kurEisiu = iKuriaLekstutePerkelti(ans);
    vector<pair<int, int> > eiliskumas = raskEiliskuma(kurEisiu);
 //   cout << "kurEisiu: "; for(auto &x : kurEisiu) cout << x << " ";
 //   cout << endl << "eiliskumas: " << endl;
 //   for(auto &x : eiliskumas) {
 //       cout << "swp " << x.first << " lekstutes turini su " << x.second << " lekstutes turiniu" << endl;
  //  }
    vector<int> lekstute(n), kurLekstute(n);
    for(int i = 0; i < n; i++) lekstute[i] = i, kurLekstute[i] = i;
    //cout << endl;
    for(int i = 0; i < ans; i++) {

        kurLekstute[lekstute[X[i]]] = Y[i];
        kurLekstute[lekstute[Y[i]]] = X[i];
        swap(lekstute[X[i]], lekstute[Y[i]]);

        int A, B;
        if(i >= eiliskumas.size()) A = B = 0;
        else {
            A = eiliskumas[i].first;
            B = eiliskumas[i].second;
        }
        P[i] = kurLekstute[A];
        Q[i] = kurLekstute[B];
        //cout << "swap " << X[i] << " su " << Y[i] << ". Tada lekstutes issidesciusios taip:";
       // for(auto x : lekstute) cout << x << " "; cout << endl;
        //cout << "tada swapinsiu lekstutes " << A << " turini su lekstutes " << B << " turiniu " << endl;
        //cout << "T.y., swap " << P[i] << " su " << Q[i] << endl << endl;
    }

    return ans;
}


Compilation message

sorting.cpp: In function 'std::vector<std::pair<int, int> > raskEiliskuma(std::vector<int>)':
sorting.cpp:55:17: warning: declaration of 'i' shadows a previous local [-Wshadow]
   55 |         for(int i = ratas.size() - 1; i > 0; i--) {
      |                 ^
sorting.cpp:45:13: note: shadowed declaration is here
   45 |     for(int i = 0; i < n; i++) {
      |             ^
sorting.cpp:55:34: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   55 |         for(int i = ratas.size() - 1; i > 0; i--) {
      |                     ~~~~~~~~~~~~~^~~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:98:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   98 |         if(i >= eiliskumas.size()) A = B = 0;
      |            ~~^~~~~~~~~~~~~~~~~~~~
sorting.cpp:69:9: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
   69 |     int ans;
      |         ^~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 296 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 296 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 308 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 300 KB Output is correct
21 Correct 2 ms 692 KB Output is correct
22 Correct 1 ms 696 KB Output is correct
23 Correct 2 ms 724 KB Output is correct
24 Correct 2 ms 724 KB Output is correct
25 Correct 1 ms 724 KB Output is correct
26 Correct 2 ms 724 KB Output is correct
27 Correct 2 ms 696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 568 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 436 KB Output is correct
6 Correct 1 ms 436 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 2 ms 500 KB Output is correct
10 Correct 1 ms 568 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 504 KB Output is correct
13 Correct 1 ms 496 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 568 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 436 KB Output is correct
6 Correct 1 ms 436 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 2 ms 500 KB Output is correct
10 Correct 1 ms 568 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 504 KB Output is correct
13 Correct 1 ms 496 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
15 Correct 140 ms 26360 KB Output is correct
16 Correct 142 ms 27288 KB Output is correct
17 Correct 161 ms 28840 KB Output is correct
18 Correct 75 ms 22464 KB Output is correct
19 Correct 132 ms 24724 KB Output is correct
20 Correct 143 ms 25512 KB Output is correct
21 Correct 142 ms 25612 KB Output is correct
22 Correct 128 ms 23856 KB Output is correct
23 Correct 151 ms 29420 KB Output is correct
24 Correct 161 ms 29116 KB Output is correct
25 Correct 154 ms 28588 KB Output is correct
26 Correct 152 ms 25564 KB Output is correct
27 Correct 133 ms 24712 KB Output is correct
28 Correct 159 ms 28928 KB Output is correct
29 Correct 156 ms 27616 KB Output is correct
30 Correct 106 ms 24036 KB Output is correct
31 Correct 156 ms 28204 KB Output is correct
32 Correct 150 ms 28564 KB Output is correct
33 Correct 163 ms 28836 KB Output is correct
34 Correct 162 ms 28520 KB Output is correct
35 Correct 135 ms 24472 KB Output is correct
36 Correct 71 ms 23420 KB Output is correct
37 Correct 165 ms 30160 KB Output is correct
38 Correct 153 ms 28976 KB Output is correct
39 Correct 156 ms 29216 KB Output is correct