Submission #419250

# Submission time Handle Problem Language Result Execution time Memory
419250 2021-06-06T15:25:18 Z Mounir Sorting (IOI15_sorting) C++14
100 / 100
194 ms 16476 KB
#include "sorting.h"
#include <bits/stdc++.h>
#define sz(x) (int)x.size()
#define chmax(x, v) x = max(x, v)
#define chmin(x, v) x = min(x, v)
#define all(x) x.begin(), x.end()
#define pb push_back
#define pii pair<int, int>
#define x first
#define y second
using namespace std;

const int N_VALS = 1e6;

int nVals, nTours;
vector<int> vals, tmp;
vector<pii> modifs;
bool vu[N_VALS];

int nCycles(){
	for (int ind = 0; ind < nVals; ++ind)
		vu[ind] = false;
	
	int nb = 0;
	
	//cout << "BEGIN" << endl;

	for (int depart = 0; depart < nVals; ++depart){
		if (!vu[depart]){
			nb++;

			vu[depart] = true;
			int noeud = vals[depart];
			while (noeud != depart){
				vu[noeud] = true;
				noeud = vals[noeud];
			}
		}
	}

	//cout << "END" << endl;

	return nb;
}

void faireSwaps(int borneMax){
	for (int ind = 0; ind < nVals; ++ind)
		vals[ind] = tmp[ind];
	for (int ind = 1; ind <= borneMax; ++ind)
		swap(vals[modifs[ind].x], vals[modifs[ind].y]);
}

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
    //initialisation.
    nVals = N, nTours = M;
    vals.resize(nVals), tmp.resize(nVals);
    for (int ind = 0; ind < nVals; ++ind)
    	vals[ind] = tmp[ind] = S[ind];

    modifs.resize(nTours + 1);
    for (int ind = 0; ind < nTours; ++ind)
    	modifs[ind + 1] = {X[ind], Y[ind]};


    //Dichotomie pour trouver R.
    int gauche = 0, droite =  nTours + 1; //vu que la fin est exclue
    while (droite > gauche){
    	int milieu = (droite + gauche)/2;
    	faireSwaps(milieu);

    //	cout << "m " << milieu << " " << nCycles() << endl;

    	if (milieu >= nVals - nCycles())
    		droite = milieu;
    	else
    		gauche = milieu + 1;
    //	cout << gauche << " " << droite << endl;
    }

    

    int res = droite;
    faireSwaps(res); 

    vector<int> p, q;
/*
    for (int val : vals)
    	cout << val << " ";
    cout << endl;
*/
    int nb = 0;
    for (int ind = 0; ind < nVals; ++ind){
    	while (vals[ind] != ind){
    		p.pb(ind);
    		q.pb(vals[ind]);
    		swap(vals[ind], vals[vals[ind]]);
    		nb++;
    	}
    }

    while (nb < res){
 		p.pb(0);
 		q.pb(0);
 		nb++;
    }
/*
 	for (int ind = 0; ind < res; ++ind)
 		cout << p[ind] << " " << q[ind] << endl;
*/


    vector<int> permutation(nVals);
    for (int ind = 0; ind < nVals; ++ind)
    	permutation[ind] = ind;

    for (int iModif = res; iModif >= 2; --iModif){
    	swap(vals[permutation[modifs[iModif].x]], vals[permutation[modifs[iModif].y]]);
    	swap(permutation[modifs[iModif].x], permutation[modifs[iModif].y]);
    	p[iModif - 2] = vals[p[iModif - 2]];
    	q[iModif - 2] = vals[q[iModif - 2]];             
    }


    for (int ind = 0; ind < res; ++ind){
    	P[ind] = p[ind];
    	Q[ind] = q[ind];
    }
/*
    for (int ind = 0; ind < res; ++ind){
    	P[ind] = P[ind + 1];
    	Q[ind] = Q[ind + 1];
    }
*/

   // cout << endl;//
//    cout << "SALUT\n";
   for (int ind = 0; ind < res; ++ind){
    //	cout << "TOUR\n";
  //  	cout << X[ind] << " " << Y[ind] << endl;
    //	cout << P[ind] << " " << Q[ind] << endl;
    	swap(vals[X[ind]], vals[Y[ind]]);
    	swap(vals[P[ind]], vals[Q[ind]]);

    	/*for (int val : vals)
    		cout << val << " ";
    	cout << endl;*/
    }


	return res;
}


# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 0 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 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 0 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 340 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 0 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 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 0 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 340 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 0 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 0 ms 204 KB Output is correct
21 Correct 1 ms 460 KB Output is correct
22 Correct 2 ms 516 KB Output is correct
23 Correct 1 ms 460 KB Output is correct
24 Correct 1 ms 480 KB Output is correct
25 Correct 2 ms 460 KB Output is correct
26 Correct 1 ms 460 KB Output is correct
27 Correct 2 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 460 KB Output is correct
2 Correct 2 ms 472 KB Output is correct
3 Correct 2 ms 460 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 332 KB Output is correct
7 Correct 2 ms 460 KB Output is correct
8 Correct 2 ms 460 KB Output is correct
9 Correct 2 ms 460 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
12 Correct 1 ms 460 KB Output is correct
13 Correct 2 ms 460 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 460 KB Output is correct
2 Correct 2 ms 472 KB Output is correct
3 Correct 2 ms 460 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 332 KB Output is correct
7 Correct 2 ms 460 KB Output is correct
8 Correct 2 ms 460 KB Output is correct
9 Correct 2 ms 460 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
12 Correct 1 ms 460 KB Output is correct
13 Correct 2 ms 460 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 147 ms 14436 KB Output is correct
16 Correct 153 ms 14768 KB Output is correct
17 Correct 161 ms 15592 KB Output is correct
18 Correct 66 ms 11972 KB Output is correct
19 Correct 138 ms 14308 KB Output is correct
20 Correct 149 ms 14776 KB Output is correct
21 Correct 146 ms 14832 KB Output is correct
22 Correct 138 ms 15792 KB Output is correct
23 Correct 194 ms 16048 KB Output is correct
24 Correct 176 ms 15864 KB Output is correct
25 Correct 180 ms 15712 KB Output is correct
26 Correct 167 ms 14776 KB Output is correct
27 Correct 135 ms 14312 KB Output is correct
28 Correct 172 ms 15680 KB Output is correct
29 Correct 177 ms 15308 KB Output is correct
30 Correct 99 ms 13840 KB Output is correct
31 Correct 178 ms 15688 KB Output is correct
32 Correct 158 ms 15600 KB Output is correct
33 Correct 176 ms 15792 KB Output is correct
34 Correct 177 ms 15804 KB Output is correct
35 Correct 144 ms 14192 KB Output is correct
36 Correct 54 ms 12868 KB Output is correct
37 Correct 189 ms 16476 KB Output is correct
38 Correct 186 ms 15716 KB Output is correct
39 Correct 184 ms 16092 KB Output is correct