-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2020 S4.cpp
188 lines (125 loc) · 6.03 KB
/
2020 S4.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
2020 S4 - Swapping Seats
Difficulty: Medium/Hard
I had to look up a solution to this problem.
The reason this problem is rated Medium is because it's an extension to 2021 J4 "Arranging Books". Nonetheless, it's still quite a challenging problem and definitely an S4
Key insights to solving:
Due to the time limit on this problem, a time complexity of O(N) will be needed since the table can be up to 1 000 000 people
It's best to do swaps that get 2 people in the right place at the same time first
Afterwards you must do inefficient swaps. Aka only one person ends up in the right place after swapping
To know what needs to be swapped, we will be dividing the table into sections, section A, section B and section C with each section being where all the corresponding letters should be
However, these sections can be placed anywhere and in any order, therefore we must check all possible section arrangements
There are 6 permutations of ABC which are
ABC
ACB
BAC
BCA
CAB
CBA
Note however that since the table is circular, ABC == CAB == BCA, and ACB == CBA == BAC. This means we only need to check these two orders of ABC
As for checking section offset, we will be using a rolling window.
I will be using a map to store the number of A's, B's and C's in a section
Finally, the minimum number of swaps for a certain section arrangment is:
# of non A's in section A + # of non B's in section B - min(# of A's in section B, # of B's in section A)
This formula is quite intuitive since it first calculates the total amount of inefficient swaps needed and then subtracts the total amount of optimal swaps that can be made.
Note that C is not needed in this formula because if A and B are sorted, then C must be sorted as well
*/
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
int main(){
std::string table; //Represent table
std::cin >> table;
int table_size = table.size();
table += table; //Since the table is circular, we need to connect it's ends for our sliding window to work. You could try to accomplish a rolling window with negative indexing but that's too much work
int countA = 0; //Total number of A's
int countB = 0; //Total number of B's
int countC = 0; //Total number of C's
//Count each letter frequency
for (int i = 0; i < table_size; i++){
if (table[i] == 'A'){
countA++;
}
else if (table[i] == 'B'){
countB++;
}
else{
countC++;
}
}
//Our sections, the key is the letter in that section and the corresponding value is the frequency of that letter
//For example sectionA['A'] == 5 means that there are 5 A's in section A
std::map<char, int> sectionA;
std::map<char, int> sectionB;
std::map<char, int> sectionC;
sectionA['A'] = 0, sectionA['B'] = 0, sectionA['C'] = 0;
sectionB['A'] = 0, sectionB['B'] = 0, sectionB['C'] = 0;
sectionC['A'] = 0, sectionC['B'] = 0, sectionC['C'] = 0;
//Calculate letter frequency in each section
//Section A
for (int i = 0; i < countA; i++){
sectionA[table[i]]++;
}
//Section B
for (int i = countA; i < countA + countB; i++){
sectionB[table[i]]++;
}
//Section C
for (int i = countA + countB; i < countA + countB + countC; i++){
sectionC[table[i]]++;
}
//Calculate minimum number of swaps for initial arrangement (no offset)
int num_of_swaps = sectionA['B'] + sectionA['C'] + sectionB['A'] + sectionB['C'] - std::min(sectionA['B'], sectionB['A']);
std::set<int> swaps; //I will just be inserting all values into a set, sets are ordered therefore at the end I will print the minimum value of the set
swaps.insert(num_of_swaps);
//Rolling window begin
for (int i = 0; i < table_size - 1; i++){
sectionA[table[i]]--; //Remove first part of section A
sectionA[table[i + countA]]++; //Add to section A
sectionB[table[i + countA]]--; //Remove first part of section B
sectionB[table[i + countA + countB]]++; //Add to section B
sectionC[table[i + countA + countB]]--; //Remove first part of section C
sectionC[table[i + countA + countB + countC]]++; //Add to section C
//Calculate minimum number of swaps for this particular arrangement and positioning
num_of_swaps = sectionA['B'] + sectionA['C'] + sectionB['A'] + sectionB['C'] - std::min(sectionA['B'], sectionB['A']);
swaps.insert(num_of_swaps);
}
//
// Previously, we just calculated all possible possitions for arrangement ABC, we must now do the same for CBA
//
//Reset sections
sectionA['A'] = 0, sectionA['B'] = 0, sectionA['C'] = 0;
sectionB['A'] = 0, sectionB['B'] = 0, sectionB['C'] = 0;
sectionC['A'] = 0, sectionC['B'] = 0, sectionC['C'] = 0;
//Count frequencies once more, except in CBA order now
//Section C
for (int i = 0; i < countC; i++){
sectionC[table[i]]++;
}
//Section B
for (int i = countC; i < countC + countB; i++){
sectionB[table[i]]++;
}
//Section A
for (int i = countC + countB; i < countC + countB + countA; i++){
sectionA[table[i]]++;
}
num_of_swaps = sectionA['B'] + sectionA['C'] + sectionB['A'] + sectionB['C'] - std::min(sectionA['B'], sectionB['A']);
swaps.insert(num_of_swaps);
//Rolling window once again
for (int i = 0; i < table_size - 1; i++){
sectionC[table[i]]--; //Remove first part of section C
sectionC[table[i + countC]]++; //Add to section C
sectionB[table[i + countC]]--; //Remove first part of section B
sectionB[table[i + countC + countB]]++; //Add to section B
sectionA[table[i + countC + countB]]--; //Remove first part of section A
sectionA[table[i + countC + countB + countA]]++; //Add to section A
num_of_swaps = sectionA['B'] + sectionA['C'] + sectionB['A'] + sectionB['C'] - std::min(sectionA['B'], sectionB['A']);
swaps.insert(num_of_swaps);
}
//Output minimum number of swaps
std::cout << *swaps.begin();
return 0;
}