# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1018474 |
2024-07-10T06:02:22 Z |
huutuan |
Sorting (IOI15_sorting) |
C++14 |
|
234 ms |
596 KB |
#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
if (is_sorted(S, S+N)) return 0;
for (int i=0; i<M; ++i){
swap(S[X[i]], S[Y[i]]);
if (is_sorted(S, S+N)) return i+1;
vector<vector<int>> vv;
vector<int> vis(N);
int cnt=0;
for (int j=0; j<N; ++j) if (!vis[j]){
vv.emplace_back();
int u=j;
++cnt;
while (!vis[u]){
vv.back().push_back(u); vis[u]=cnt;
u=S[u];
}
}
if (i+1==M){
if ((int)vv.size()!=N-1) return -1;
for (auto &j:vv) if ((int)j.size()==2) P[i]=j[0], Q[i]=j[1];
return M;
}
int u=X[i+1], v=Y[i+1];
if (vis[u]==vis[v] && u!=v){
if ((int)vv[vis[u]-1].size()==2){
if ((int)vv.size()!=N-1){
int id=0;
while (id==vis[u]-1 || (int)vv[id].size()==1) ++id;
P[i]=vv[id][0], Q[i]=vv[id][1];
}
}else{
if (S[u]==v){
P[i]=v, Q[i]=S[v];
}else{
P[i]=u, Q[i]=S[u];
}
}
}else{
int id=0;
while ((int)vv[id].size()==1) ++id;
P[i]=vv[id][0], Q[i]=vv[id][1];
}
swap(S[P[i]], S[Q[i]]);
if (is_sorted(S, S+N)) return i+1;
}
return -1;
}
# |
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 |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
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 |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
412 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
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 |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
412 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Incorrect |
234 ms |
596 KB |
Integer parameter [name=R] equals to -1, violates the range [0, 14430] |
22 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
113 ms |
592 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
113 ms |
592 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |