-
Notifications
You must be signed in to change notification settings - Fork 187
/
1188.cc
120 lines (116 loc) · 2.51 KB
/
1188.cc
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
// https://cses.fi/problemset/task/1188/
#include <iostream>
#include <map>
#include <set>
#include <tuple>
using namespace std;
typedef tuple<int, int> ii;
typedef set<ii> sii;
typedef map<int, int, greater<int>> mii;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
string s;
cin >> s;
int n = s.size();
mii m;
sii a;
int c = 1;
char b = s[0];
for (int i = 1; i < n; i++) {
if (s[i] == b) c++;
else {
m[i - c] = c;
a.insert({c, i - c});
b = s[i];
c = 1;
}
}
m[n - c] = c;
a.insert({c, n - c});
int t, x;
cin >> t;
for (int i = 0; i < t; i++) {
cin >> x;
x--;
auto it = m.lower_bound(x);
a.erase({it->second, it->first});
if (it->first == x) {
if (it->second == 1) {
int k = it->first;
int c = it->second;
auto next = it;
next--;
auto prev = it;
prev++;
if (next != m.end()) {
c += next->second;
a.erase({next->second, next->first});
m.erase(next);
}
if (prev != m.end()) {
k = prev->first;
c += prev->second;
a.erase({prev->second, prev->first});
m.erase(prev);
}
m.erase(it);
a.insert({c, k});
m[k] = c;
} else {
int k = it->first;
int c = it->second;
auto prev = it;
prev++;
a.erase({it->second, it->first});
if (prev != m.end()) {
int k2, c2;
k2 = prev->first;
c2 = prev->second;
m.erase(prev);
a.erase({c2, k2});
m[k2] = c2 + 1;
a.insert({c2 + 1, k2});
m.erase(it);
} else {
m.erase(it);
m[k] = 1;
a.insert({1, k});
}
m[k + 1] = c - 1;
a.insert({c - 1, k + 1});
}
} else {
int k = it->first;
int c = it->second;
int d = x - it->first;
if (c == d + 1) {
auto next = it;
next--;
int c2 = 0;
if (next != m.end()) {
int k2 = next->first;
c2 = next->second;
m.erase(next);
a.erase({c2, k2});
}
m[x] = c2 + 1;
a.insert({c2 + 1, x});
} else {
m[x] = 1;
a.insert({1, x});
m[x + 1] = c - d - 1;
a.insert({c - d - 1, x + 1});
}
a.erase({c, k});
m.erase(it);
m[k] = d;
a.insert({d, k});
}
int c, k;
tie(c, k) = *a.rbegin();
cout << c;
if (i < t - 1) cout << " ";
}
cout << endl;
}