-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03_sorting-a-three-valued-sequence.cpp
79 lines (70 loc) · 1.61 KB
/
03_sorting-a-three-valued-sequence.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
77
78
79
/*
ID: skyline12
PROG: sort3
LANG: C++11
*/
// Xiaoyan Wang 5/5/2016
#include <fstream>
#include <algorithm>
#include <vector>
using std::vector;
using std::min;
int N; // number of values
vector<int> values;
vector<int> num(4, 0);
vector<vector<int> > count(4, vector<int>(4, 0));
void countnum();
int countswap();
int main() {
std::ifstream fin("sort3.in");
std::ofstream fout("sort3.out");
// read the input
fin >> N;
values.reserve(N+1);
values.push_back(0);
for(int i = 0; i < N; ++i) {
int temp;
fin >> temp;
values.push_back(temp);
++num[temp];
}
fin.close();
// process the numbers
countnum();
// calculate the number of swaps
int swap = countswap();
fout << swap << std::endl;
fout.close();
return 0;
}
void countnum() {
int curr = 1;
for(int i = 1; i <= 3; ++i) {
for(int j = curr; j < curr + num[i]; ++j)
++count[i][values[j]];
curr += num[i];
}
}
int countswap() {
int swap = 0;
// count the number of misplaced numbers that could
// be swapped in a single step
for(int i = 1; i <= 2; ++i)
for(int j = i+1; j <= 3; ++j) {
int temp = min(count[i][j], count[j][i]);
swap += temp;
// "swapping" the numbers
// adding the swapped number into the correct
// location isn't necessary as i won't use that
count[i][j] -= temp;
count[j][i] -= temp;
}
// the rest are the values that required swapping twice
// at this time, there could only be a single type of misplaced
// value
if(count[1][2] != 0)
swap += 2 * count[1][2];
else if(count[1][3] != 0)
swap += 2 * count[1][3];
return swap;
}