# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
984838 | 2024-05-17T06:48:51 Z | LucaIlie | Sorting (IOI15_sorting) | C++17 | 2 ms | 2652 KB |
#include "sorting.h" #include <bits/stdc++.h> using namespace std; const int MAX_N = 2e5; int t[MAX_N], perm[MAX_N], vis[MAX_N], pos[MAX_N]; vector<pair<int, int>> sol; int getR( int n, int s[], int k, int x[], int y[], int p[], int q[] ) { for ( int i = 0; i < n; i++ ) t[i] = i; for ( int i = 0; i < k; i++ ) swap( t[x[i]], t[y[i]] ); for ( int i = 0; i < n; i++ ) perm[t[i]] = s[i]; for ( int i = 0; i < n; i++ ) vis[i] = false; sol.clear(); for ( int i = 0; i < n; i++ ) { if ( vis[i] ) continue; int j = i; vector<pair<int, int>> v; do { vis[j] = true; if ( perm[j] != j && j != i ) v.push_back( { j, perm[j] } ); j = perm[j]; } while ( j != i ); reverse( v.begin(), v.end() ); for ( pair<int, int> swp: v ) sol.push_back( swp ); } return sol.size(); } int findSwapPairs( int n, int s[], int m, int x[], int y[], int p[], int q[] ) { int l = 0, r = m; while ( r - l > 1 ) { int k = (l + r) / 2; int rr = getR( n, s, k, x, y, p, q ); //printf( "%d %d\n", k, rr ); if ( rr > k ) l = k; else r = k; } getR( n, s, r, x, y, p, q ); for ( int i = 0; i < n; i++ ) pos[s[i]] = i; for ( int i = 0; i < r; i++ ) { //for ( int j = 0; j < n; j++ ) // printf( "%d ", pos[j] ); //printf( "\n" ); swap( pos[s[x[i]]], pos[s[y[i]]] ); swap( s[x[i]], s[y[i]] ); //for ( int j = 0; j < n; j++ ) // printf( "%d ", pos[j] ); //printf( "\n" ); if ( i < sol.size() ) { // printf( "%d %d\n", sol[i].first, sol[i].second ); p[i] = pos[sol[i].first]; q[i] = pos[sol[i].second]; swap( pos[sol[i].first], pos[sol[i].second] ); swap( s[p[i]], s[q[i]] ); } else { p[i] = 0; q[i] = 0; } // for ( int j = 0; j < n; j++ ) // printf( "%d ", pos[j] ); // printf( "\n\n" ); } return r; }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 2392 KB | Output is correct |
2 | Correct | 1 ms | 2648 KB | Output is correct |
3 | Correct | 1 ms | 2392 KB | Output is correct |
4 | Correct | 1 ms | 2392 KB | Output is correct |
5 | Incorrect | 1 ms | 2396 KB | Output isn't correct |
6 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 2392 KB | Output is correct |
2 | Correct | 1 ms | 2648 KB | Output is correct |
3 | Correct | 1 ms | 2392 KB | Output is correct |
4 | Correct | 1 ms | 2392 KB | Output is correct |
5 | Incorrect | 1 ms | 2396 KB | Output isn't correct |
6 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 2396 KB | Output is correct |
2 | Incorrect | 1 ms | 2496 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 2392 KB | Output is correct |
2 | Correct | 1 ms | 2648 KB | Output is correct |
3 | Correct | 1 ms | 2392 KB | Output is correct |
4 | Correct | 1 ms | 2392 KB | Output is correct |
5 | Incorrect | 1 ms | 2396 KB | Output isn't correct |
6 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 2 ms | 2652 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 2 ms | 2652 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |