-
Notifications
You must be signed in to change notification settings - Fork 0
/
contest2.cpp
76 lines (61 loc) · 1.12 KB
/
contest2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <bits/stdc++.h>
using namespace std;
bool findMinSwap(vector<int> &A, int N)
{
if (A.size() == 2)
{
if (A[0] > A[1])
{
return false;
}
else
{
return true;
}
}
if (is_sorted(A.begin(), A.end()))
{
return true;
}
vector<pair<int, int>> m(N);
for (int i = 0; i < N; i++)
{
m[i].first = A[i];
m[i].second = i;
}
sort(m.begin(), m.end());
int swaps = 0;
int x = 0;
while (x < N)
{
if (m[x].second == x or m[x].first == A[x])
{
++x;
continue;
}
else
{
swap(m[x].first, m[m[x].second].first);
swap(m[x].second, m[m[x].second].second);
if (m[x].second != x)
x--;
}
swaps++;
++x;
}
if (swaps == 2)
{
return true;
}
else
{
return false;
}
}
int main()
{
vector<int> A = {1,2,3};
int n = A.size();
cout << is_sorted(A.begin(), A.end()) << endl;
cout << findMinSwap(A, n) << '\n';
}