-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathmain.cpp
154 lines (114 loc) · 2.9 KB
/
main.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
#include <bits/stdc++.h>
using namespace std; // NOLINT
constexpr int MAX_N = 100000;
constexpr bool RUN_TESTS = false;
int N;
//
// Fenwick Tree representing the number of remaining cards in A.
//
int T[MAX_N + 10] = {0};
//
// Increments the value for cards i in the Fenwick Tree.
//
void update(int i, int x) {
assert(i >= 0 && i < N);
i++;
while (i <= N) {
T[i] += x;
i += (i & -i);
}
}
//
// Counts the number of cards in A[0...i - 1] that remain.
//
int count(int i) {
assert(i >= 0 && i <= N);
int s = 0;
while (i > 0) {
s += T[i];
i -= (i & -i);
}
return s;
}
//
// Counts the number of cards in A[i...j - 1] that remain.
//
int range(int i, int j) {
assert(i >= 0 && i <= N);
assert(j >= i && j <= N);
return count(j) - count(i);
}
void test() {
cout << "Running tests..." << endl;
N = 4;
assert(range(0, 4) == 0);
update(0, 1);
update(1, 1);
update(3, 1);
assert(range(0, 1) == 1);
assert(range(0, 2) == 2);
assert(range(0, 4) == 3);
std::fill(&T[0], &T[0] + sizeof(T) / sizeof(T[0]), 0);
assert(range(0, N) == 0);
cout << "Tests pass!" << endl;
}
//
// Returns the index in indices that is closest to the right of i. If no such
// index exists the search "loops" back to i = 0 and continues.
//
int get_closest(int i, const std::set<int>& indices) {
assert(!indices.empty());
auto it = indices.lower_bound(i);
if (it != indices.end()) {
return *it;
}
return *indices.begin();
}
int main() {
if (RUN_TESTS) {
test();
}
cin >> N;
// Ordered mapping from number to the set of card indices with the number.
std::map<int, std::set<int>> index;
for (int i = 0; i < N; i++) {
int a;
cin >> a;
// Indicate that this card is currently in the set of unsorted cards.
update(i, 1);
index[a].insert(i);
}
// The values in index sorted in reverse order by key. This ensures we can
// access and pop the set of indices corresponding to the smallest number in
// our current stack in O(1) time.
std::stack<std::set<int>> cards;
for (auto it = index.rbegin(); it != index.rend(); ++it) {
cards.push(std::move(it->second));
}
// The index in A of the card currently at the top of the stack.
int offset = 0;
// The number of cards Vasily has taken from the top of the stack.
int64_t pops = 0;
while (!cards.empty()) {
// Get the closest index of the smallest card value.
auto& indices = cards.top();
int i = get_closest(offset, indices);
if (indices.size() == 1) {
cards.pop();
} else {
indices.erase(i);
}
// Pop everything still in the stack between the minimum card and the
// offset.
if (offset <= i) {
pops += range(offset, i + 1);
} else {
pops += count(i + 1) + range(offset, N);
}
offset = (i + 1) % N;
// Remove the minimum card from the stack.
update(i, -1);
}
cout << pops << endl;
return 0;
}