CF1398E-Two Types of Spells
题目:
题目描述:
Polycarp plays a computer game (yet again). In this game, he fights monsters using magic spells.
There are two types of spells: fire spell of power $ x $ deals $ x $ damage to the monster, and lightning spell of power $ y $ deals $ y $ damage to the monster and doubles the damage of the next spell Polycarp casts. Each spell can be cast only once per battle, but Polycarp can cast them in any order.
For example, suppose that Polycarp knows three spells: a fire spell of power $ 5 $ , a lightning spell of power $ 1 $ , and a lightning spell of power $ 8 $ . There are $ 6 $ ways to choose the order in which he casts the spells:
- first, second, third. This order deals $ 5 + 1 + 2 \cdot 8 = 22 $ damage;
- first, third, second. This order deals $ 5 + 8 + 2 \cdot 1 = 15 $ damage;
- second, first, third. This order deals $ 1 + 2 \cdot 5 + 8 = 19 $ damage;
- second, third, first. This order deals $ 1 + 2 \cdot 8 + 2 \cdot 5 = 27 $ damage;
- third, first, second. This order deals $ 8 + 2 \cdot 5 + 1 = 19 $ damage;
- third, second, first. This order deals $ 8 + 2 \cdot 1 + 2 \cdot 5 = 20 $ damage.
Initially, Polycarp knows $ 0 $ spells. His spell set changes $ n $ times, each time he either learns a new spell or forgets an already known one. After each change, calculate the maximum possible damage Polycarp may deal using the spells he knows.
输入格式:
The first line contains one integer $ n $ ( $ 1 \le n \le 2 \cdot 10^5 $ ) — the number of changes to the spell set.
Each of the next $ n $ lines contains two integers $ tp $ and $ d $ ( $ 0 \le tp_i \le 1 $ ; $ -10^9 \le d \le 10^9 $ ; $ d_i \neq 0 $ ) — the description of the change. If $ tp_i $ if equal to $ 0 $ , then Polycarp learns (or forgets) a fire spell, otherwise he learns (or forgets) a lightning spell.
If $ d_i > 0 $ , then Polycarp learns a spell of power $ d_i $ . Otherwise, Polycarp forgets a spell with power $ -d_i $ , and it is guaranteed that he knew that spell before the change.
It is guaranteed that the powers of all spells Polycarp knows after each change are different (Polycarp never knows two spells with the same power).
输出格式:
After each change, print the maximum damage Polycarp can deal with his current set of spells.
样例:
样例输入 1:
1
2
3
4
5
6
7
| 6
1 5
0 10
1 -5
0 5
1 11
0 -10
|
样例输出 1:
1
2
3
4
5
6
| 5
25
10
15
36
21
|
思路:
实现:
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
| /*
*User: ybw051114
*Time: 2020.08.14 22:35:09
*/
#include <bits/stdc++.h>
using namespace std;
#ifndef use_ios11
#define use_ios11
using namespace std;
struct ins
{
int ans;
ins() { ans = 1; }
#define endl '\n'
void read()
{
}
void read1(char &s)
{
char c = getchar();
for (; !isprint(c) || c == ' ' || c == '\n' || c == '\t'; c = getchar())
;
s = c;
if (c == EOF)
ans = 0;
}
void read1(string &s)
{
s = "";
char c = getchar();
for (; !isprint(c) || c == ' ' || c == '\n' || c == '\t'; c = getchar())
;
for (; isprint(c) && c != ' ' && c != '\n' && c != '\t'; c = getchar())
s += c;
if (c == EOF)
ans = 0;
}
template <typename T>
void read1(T &n)
{
T x = 0;
int f = 1;
char c = getchar();
for (; !isdigit(c); c = getchar())
{
if (c == '-')
f = -1;
if (c == EOF)
{
ans = 0;
return;
}
}
for (; isdigit(c); c = getchar())
x = x * 10 + c - 48;
n = x * f;
if (c == EOF)
ans = 0;
if (c != '.')
return;
T l = 0.1;
while ((c = getchar()) <= '9' && c >= '0')
x = x + (c & 15) * l, l *= 0.1;
n = x * f;
if (c == EOF)
ans = 0;
}
void write() {}
void write1(string s)
{
int n = s.size();
for (int i = 0; i < n; i++)
putchar(s[i]);
}
void write1(const char *s)
{
int n = strlen(s);
for (int i = 0; i < n; i++)
putchar(s[i]);
}
void write1(char s) { putchar(s); }
void write1(float s, int x = 6)
{
char y[10001];
sprintf(y, "%%.%df", x);
printf(y, s);
}
void write1(double s, int x = 6)
{
char y[10001];
sprintf(y, "%%.%dlf", x);
printf(y, s);
}
void write1(long double s, int x = 6)
{
char y[10001];
sprintf(y, "%%.%dLf", x);
printf(y, s);
}
template <typename T>
void write1(T n)
{
if (n < 0)
n = -n, putchar('-');
if (n > 9)
write1(n / 10);
putchar('0' + n % 10);
}
template <typename T>
friend ins operator>>(ins x, T &n);
template <typename T>
friend ins operator<<(ins x, T n);
operator bool() { return ans; }
};
template <typename T>
ins operator>>(ins x, T &n)
{
if (!x.ans)
return x;
x.read1(n);
return x;
}
template <typename T>
ins operator<<(ins x, T n)
{
x.write1(n);
return x;
}
ins yin;
ins yout;
#endif
set<long long> q, q1, x;
int t, t1;
long long ans = 0;
int main()
{
int TTT;
yin >> TTT;
q.insert(0);
x.insert(0);
while (TTT--)
{
int tp;
long long dd;
yin >> tp >> dd;
if (tp == 0)
{
if (dd > 0)
x.insert(dd);
else
x.erase(-dd);
}
while (q1.size() < t && q.size() > 1)
{
q1.insert(*q.rbegin());
ans += *q.rbegin();
set<long long>::iterator x = q.end();
x--;
q.erase(x);
}
while (!q1.empty() && (*q.rbegin()) > (*q1.begin()))
{
q1.insert(*q.rbegin());
q.insert(*q1.begin());
set<long long>::iterator x = q.end();
x--;
ans += (*q.rbegin()) - (*q1.begin());
q.erase(x);
x = q1.begin();
q1.erase(x);
}
if (dd < 0)
{
dd = -dd;
if (tp)
t--;
if (q1.count(dd))
{
q1.erase(dd);
ans -= dd * 2;
while (q1.size() < t && q.size() > 1)
{
q1.insert(*q.rbegin());
ans += *q.rbegin();
set<long long>::iterator x = q.end();
x--;
q.erase(x);
}
while (!q1.empty() && (*q.rbegin()) > (*q1.begin()))
{
q1.insert(*q.rbegin());
q.insert(*q1.begin());
set<long long>::iterator x = q.end();
x--;
ans += (*q.rbegin()) - (*q1.begin());
q.erase(x);
x = q1.begin();
q1.erase(x);
}
}
else
{
q.erase(dd);
ans -= dd;
}
}
else
{
q.insert(dd);
ans += dd;
if (tp)
t++;
while (q1.size() < t && q.size() > 1)
{
q1.insert(*q.rbegin());
set<long long>::iterator x = q.end();
x--;
ans += *q.rbegin();
q.erase(x);
}
while (!q1.empty() && (*q.rbegin()) > (*q1.begin()))
{
q1.insert(*q.rbegin());
q.insert(*q1.begin());
set<long long>::iterator x = q.end();
x--;
ans += (*q.rbegin()) - (*q1.begin());
q.erase(x);
x = q1.begin();
q1.erase(x);
}
}
while (q1.size() > t)
{
q.insert(*q1.begin());
set<long long>::iterator x = q1.begin();
ans -= *q1.begin();
q1.erase(x);
}
if (q1.empty() || (*x.rbegin()) >= (*q1.begin()))
yout << ans << endl;
else
yout << ans - ((*q1.begin()) - (*x.rbegin())) << endl;
}
return 0;
}
|