Submission #419208

# Submission time Handle Problem Language Result Execution time Memory
419208 2021-06-06T14:40:10 Z Mounir Sorting (IOI15_sorting) C++14
20 / 100
1 ms 332 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 << " " << gauche << " " << droite << endl;

    	if (milieu >= nVals - nCycles())
    		droite = milieu;
    	else
    		gauche = milieu + 1;
    }

    

    int res = gauche;
    faireSwaps(res); 

    int nb = 0;
    for (int ind = 0; ind < nVals; ++ind){
    	while (vals[ind] != ind){
    		P[nb] = ind;
    		Q[nb] = vals[ind];
    		swap(vals[ind], vals[vals[ind]]);
    		nb++;
    	}
    }

    while (nb < res)
 		P[nb] = Q[nb++] = 0;

    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] = vals[P[iModif]];
    	Q[iModif] = vals[Q[iModif]];             
    }
	return res;
}


Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:95:16: warning: operation on 'nb' may be undefined [-Wsequence-point]
   95 |    P[nb] = Q[nb++] = 0;
      |              ~~^~
sorting.cpp:95:16: warning: operation on 'nb' may be undefined [-Wsequence-point]
# 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 296 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 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 0 ms 204 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 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 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 332 KB Output is correct
12 Correct 1 ms 308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Incorrect 1 ms 204 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 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 296 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 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 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 332 KB Output is correct
12 Correct 1 ms 308 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Incorrect 1 ms 204 KB Output isn't correct
15 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -