This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "sorting.h"
//#include "grader.cpp"
#include <vector>
#include <assert.h>
#include <iostream>
using namespace std;
int guessSubtask(int m, int *x, int *y)
{
bool subtask2 = true;
for(int i = 0;i<m;i++)
{
if(!(x[i]==0 && y[i]==0))
{
subtask2 = false;
break;
}
}
if(subtask2==true) return 2;
return -1;
}
bool checkSorted(int n, int *s)
{
for(int i = 1;i<n;i++)
{
if(s[i-1]>s[i]) return false;
}
return true;
}
int solve2(int n, int *s, int *p, int *q)
{
if(checkSorted(n, s)==true) return 0;
int swapInd = 0;
for(int i = 0;i<n;i++)
{
for(int j = i+1;j<n;j++)
{
if(s[j]==i)
{
swap(s[i], s[j]);
p[swapInd] = i;
q[swapInd] = j;
swapInd++;
if(checkSorted(n, s)==true) return swapInd;
break;
}
}
}
assert(false);
}
int solve3(int n, int *s, int m, int *x, int *y, int *p, int *q)
{
if(checkSorted(n, s)==true) return 0;
int swapInd = 0;
for(int i = 0;i<n;i++)
{
p[swapInd] = x[i];
q[swapInd] = y[i];
swapInd++;
for(int j = i+1;j<n;j++)
{
if(s[j]==i)
{
swap(s[i], s[j]);
p[swapInd] = i;
q[swapInd] = j;
swapInd++;
if(checkSorted(n, s)==true) return swapInd;
break;
}
}
}
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[])
{
int subtask = guessSubtask(M, X, Y);
if(subtask==2) return solve2(N, S, P, Q);
return solve3(N, S, M, X, Y, P, Q);
assert(false);
}
/*
6
0 1 2 3 4 5
5
0 0
0 0
0 0
0 0
0 0
*/
Compilation message (stderr)
sorting.cpp: In function 'int solve3(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:61:31: warning: unused parameter 'm' [-Wunused-parameter]
61 | int solve3(int n, int *s, int m, int *x, int *y, int *p, int *q)
| ~~~~^
sorting.cpp:86:1: warning: control reaches end of non-void function [-Wreturn-type]
86 | }
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |