# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1065831 |
2024-08-19T12:05:24 Z |
anango |
Sorting (IOI15_sorting) |
C++17 |
|
1000 ms |
22444 KB |
#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
int n;
vector<vector<int>> getcycledecomposition(vector<int> p) {
vector<int> visited(n,0);
vector<vector<int>> cycles;
for (int i=0; i<n; i++) {
if (!visited[i]) {
visited[i] = 1;
vector<int> cycle={i};
while (p[cycle.back()]!=i) {
visited[p[cycle.back()]] = 1;
cycle.push_back(p[cycle.back()]);
}
cycles.push_back(cycle);
}
}
return cycles;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
int l = 0;
int r = M;
while (l<r) {
int m = (l+r)/2;
n=N;
vector<int> rev(n); for (int i=0; i<n; i++) rev[i] = i;
vector<int> revpos(n); for (int i=0; i<n; i++) revpos[i] = i;
vector<int> pos(n); for (int i=0; i<n; i++) pos[i] = i;
vector<int> seq(n); for (int i=0; i<n; i++) seq[i] = S[i];
for (int i=0; i<m; i++) {
swap(pos[X[i]],pos[Y[i]]);
}
vector<int> reqperm(n,-1); for (int i=0; i<n; i++) reqperm[i] = seq[pos[i]];
vector<int> reverse_finalpos(n,-1); for (int i=0; i<n; i++) reverse_finalpos[pos[i]] = i;
vector<vector<int>> cycles = getcycledecomposition(reqperm);
vector<pair<int,int>> swaps;
for (auto i:cycles) {
for (int j=1; j<i.size(); j++) {
swaps.push_back({i[0],i[j]});
}
}
reverse(swaps.begin(), swaps.end());
vector<int> state(n); for (int i=0; i<n; i++) state[i] = i;
if (swaps.size()>m) {
l=m+1;
r=r;
}
else {
l=l;
r=m;
}
}
M = l;
n=N;
vector<int> rev(n); for (int i=0; i<n; i++) rev[i] = i;
vector<int> revpos(n); for (int i=0; i<n; i++) revpos[i] = i;
vector<int> pos(n); for (int i=0; i<n; i++) pos[i] = i;
vector<int> seq(n); for (int i=0; i<n; i++) seq[i] = S[i];
for (int i=0; i<M; i++) {
swap(pos[X[i]],pos[Y[i]]);
}
vector<int> reqperm(n,-1); for (int i=0; i<n; i++) reqperm[i] = seq[pos[i]];
vector<int> reverse_finalpos(n,-1); for (int i=0; i<n; i++) reverse_finalpos[pos[i]] = i;
vector<vector<int>> cycles = getcycledecomposition(reqperm);
vector<pair<int,int>> swaps;
for (auto i:cycles) {
for (int j=1; j<i.size(); j++) {
swaps.push_back({i[0],i[j]});
}
}
reverse(swaps.begin(), swaps.end());
vector<int> state(n); for (int i=0; i<n; i++) state[i] = i; //the current 'pos'
//swaps={{0,3},{0,2}};
while (swaps.size()<M) {
swaps.push_back({0,0});
}
vector<int> revperm(n); for (int i=0; i<n; i++) revperm[S[i]] = i;
vector<int> perm(n); for (int i=0; i<n; i++) perm[i] = S[i];
int bloaters = 0;
for (int i=0; i<n; i++) {
if (perm[i]!=i) bloaters++;
}
int lastswap = -1;
for (int sw=0; sw<M; sw++) {
swap(revperm[perm[X[sw]]],revperm[perm[Y[sw]]]);
bloaters-=perm[X[sw]]!=X[sw];
bloaters-=perm[Y[sw]]!=Y[sw];
swap(perm[X[sw]],perm[Y[sw]]);
bloaters+=perm[X[sw]]!=X[sw];
bloaters+=perm[Y[sw]]!=Y[sw];
pair<int,int> swp = swaps[sw];
int a = swp.first;
int b = swp.second;
//cout << "trying2 " << a <<" " << b << endl;
a=revperm[a]; b=revperm[b];
//cout << "trying3 " << a <<" " << b << endl;
swap(revperm[perm[a]],revperm[perm[b]]);
bloaters-=perm[a]!=a;
bloaters-=perm[b]!=b;
swap(perm[a],perm[b]);
bloaters+=perm[a]!=a;
bloaters+=perm[b]!=b;
P[sw] = a; Q[sw] = b;
//cout << "swapping " << a <<" " << b<<endl;
//cout << "ITERATION " << sw << endl;
for (int i=0; i<n; i++) {
assert(revperm[perm[i]]==i);
}
if (bloaters==0) {
lastswap=sw;
break;
}
/*for (int i=0; i<n; i++) {
cout << revperm[i] <<" ";
}
cout << endl;
for (int i=0; i<n; i++) {
cout << perm[i] <<" ";
}
cout << endl;*/
}
/*for (int i=0; i<n; i++) {
cout << pos[i] << " ";
}
cout << endl;
for (int i=0; i<swaps.size(); i++) {
cout << swaps[i].first <<" " << swaps[i].second << endl;
}
cout << endl;
for (int i=0; i<n; i++) {
cout << reqperm[i] << " ";
}
cout << endl;*/
vector<int> cur(n); for (int i=0; i<n; i++) cur[i] = S[i];
/*for (int i=0; i<M; i++) {
swap(cur[X[i]],cur[Y[i]]);
swap(cur[P[i]],cur[Q[i]]);
}
for (int i=0; i<n; i++) {
assert(cur[i]==i);
if (cur[i]!=i) {
while (1) {
int o=0;
}
}
}*/
/*cout << "final result" << endl;
for (int i=0; i<n; i++) {
cout << cur[i] <<" ";
}
cout << endl;*/
return lastswap+1;
}
Compilation message
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:43:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for (int j=1; j<i.size(); j++) {
| ~^~~~~~~~~
sorting.cpp:49:25: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
49 | if (swaps.size()>m) {
| ~~~~~~~~~~~~^~
sorting.cpp:73:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | for (int j=1; j<i.size(); j++) {
| ~^~~~~~~~~
sorting.cpp:80:24: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
80 | while (swaps.size()<M) {
| ~~~~~~~~~~~~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
600 KB |
Output is correct |
23 |
Correct |
2 ms |
604 KB |
Output is correct |
24 |
Correct |
2 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
2 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
604 KB |
Output is correct |
2 |
Correct |
3 ms |
600 KB |
Output is correct |
3 |
Correct |
3 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
5 ms |
600 KB |
Output is correct |
6 |
Correct |
3 ms |
604 KB |
Output is correct |
7 |
Correct |
3 ms |
604 KB |
Output is correct |
8 |
Correct |
3 ms |
604 KB |
Output is correct |
9 |
Correct |
3 ms |
604 KB |
Output is correct |
10 |
Correct |
3 ms |
604 KB |
Output is correct |
11 |
Correct |
5 ms |
604 KB |
Output is correct |
12 |
Correct |
3 ms |
728 KB |
Output is correct |
13 |
Correct |
3 ms |
628 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
604 KB |
Output is correct |
2 |
Correct |
3 ms |
600 KB |
Output is correct |
3 |
Correct |
3 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
5 ms |
600 KB |
Output is correct |
6 |
Correct |
3 ms |
604 KB |
Output is correct |
7 |
Correct |
3 ms |
604 KB |
Output is correct |
8 |
Correct |
3 ms |
604 KB |
Output is correct |
9 |
Correct |
3 ms |
604 KB |
Output is correct |
10 |
Correct |
3 ms |
604 KB |
Output is correct |
11 |
Correct |
5 ms |
604 KB |
Output is correct |
12 |
Correct |
3 ms |
728 KB |
Output is correct |
13 |
Correct |
3 ms |
628 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Execution timed out |
1016 ms |
22444 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |