-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path298Div2Handshakes.cpp
65 lines (55 loc) · 1.96 KB
/
298Div2Handshakes.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
#include <algorithm>
#include <iostream>
#include <set>
using namespace std;
typedef pair<int, int> HandshakePair;
const int MAX_STUDENTS = 1000001;
int main() {
int studentCount;
cin >> studentCount;
set<HandshakePair> handshakesByModulo[3];
int resultOrder[MAX_STUDENTS];
// Reading input and categorizing based on modulo 3
for (int i = 0; i < studentCount; i++) {
int handshakes;
cin >> handshakes;
handshakesByModulo[handshakes % 3].insert(make_pair(handshakes, i));
// Debugging: Show how handshakes are categorized
cout << "Handshake: " << handshakes << " goes into handshakesByModulo["
<< (handshakes % 3) << "]" << endl;
}
int currentHandshakes = 0;
for (int i = 0; i < studentCount; i++) {
auto it = handshakesByModulo[currentHandshakes % 3].lower_bound(
make_pair(currentHandshakes, -1));
HandshakePair selectedStudent;
if (it != handshakesByModulo[currentHandshakes % 3].end() &&
it->first == currentHandshakes) {
selectedStudent = *it;
resultOrder[i] = selectedStudent.second + 1;
// cout << "Selected exact match: " << selectedStudent.first << " at index
// "
// << selectedStudent.second + 1 << endl;
} else {
if (it == handshakesByModulo[currentHandshakes % 3].begin()) {
cout << "Impossible" << endl;
return 0;
} else {
it--;
selectedStudent = *it;
resultOrder[i] = selectedStudent.second + 1;
// cout << "Selected lower match: " << selectedStudent.first
// << " at index " << selectedStudent.second + 1 << endl;
}
}
handshakesByModulo[currentHandshakes % 3].erase(selectedStudent);
currentHandshakes = selectedStudent.first + 1;
// cout << "Updated currentHandshakes to " << currentHandshakes << endl;
}
// If the loop completes, print the order
cout << "Possible" << endl;
for (int i = 0; i < studentCount; i++) {
cout << resultOrder[i] << " ";
}
cout << endl;
}