mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-11-08 13:58:48 +00:00
2288. [POJ Challenge] 生日礼物
https://hydro.ac/d/bzoj/record/633e34286d08baa0485db65a
This commit is contained in:
parent
8df881adf0
commit
02f7508daf
80
BZOJ/2288/2288.cpp
Normal file
80
BZOJ/2288/2288.cpp
Normal file
@ -0,0 +1,80 @@
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <functional>
|
||||
#include <queue>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
using std::cin;
|
||||
using std::cout;
|
||||
const char endl = '\n';
|
||||
|
||||
const int N = 1e5 + 5;
|
||||
|
||||
int n, m, p = 1, a[N], pre[N], suf[N], ans;
|
||||
bool vis[N];
|
||||
std::priority_queue<
|
||||
std::pair<int, int>,
|
||||
std::vector<std::pair<int, int>>,
|
||||
std::greater<>>
|
||||
q;
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
cin >> n >> m;
|
||||
|
||||
for (int i = 1, x; i <= n; i++) {
|
||||
cin >> x;
|
||||
|
||||
if (a[p] * x < 0) {
|
||||
a[++p] = x;
|
||||
} else {
|
||||
a[p] += x;
|
||||
}
|
||||
}
|
||||
|
||||
int cnt = 0;
|
||||
for (int i = 1; i <= p; i++) {
|
||||
pre[i] = i - 1;
|
||||
suf[i] = i + 1;
|
||||
|
||||
if (a[i] > 0) {
|
||||
ans += a[i];
|
||||
cnt++;
|
||||
}
|
||||
|
||||
q.emplace(std::abs(a[i]), i);
|
||||
}
|
||||
|
||||
while (cnt > m) {
|
||||
while (vis[q.top().second]) q.pop();
|
||||
|
||||
auto o = q.top();
|
||||
q.pop();
|
||||
|
||||
if (pre[o.second] != 0 && suf[o.second] != p + 1 || a[o.second] > 0) {
|
||||
cnt--;
|
||||
ans -= o.first;
|
||||
|
||||
int l = pre[o.second],
|
||||
r = suf[o.second];
|
||||
|
||||
a[o.second] += a[l] + a[r];
|
||||
q.emplace(std::abs(a[o.second]), o.second);
|
||||
|
||||
pre[suf[l]] = pre[l];
|
||||
suf[pre[l]] = suf[l];
|
||||
vis[l] = true;
|
||||
|
||||
pre[suf[r]] = pre[r];
|
||||
suf[pre[r]] = suf[r];
|
||||
vis[r] = true;
|
||||
}
|
||||
}
|
||||
|
||||
cout << ans << endl;
|
||||
|
||||
return 0;
|
||||
}
|
BIN
BZOJ/2288/data/1.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/2288/data/1.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2288/data/1.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/2288/data/1.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2288/data/10.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/2288/data/10.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2288/data/10.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/2288/data/10.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2288/data/2.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/2288/data/2.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2288/data/2.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/2288/data/2.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2288/data/3.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/2288/data/3.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2288/data/3.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/2288/data/3.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2288/data/4.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/2288/data/4.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2288/data/4.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/2288/data/4.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2288/data/5.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/2288/data/5.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2288/data/5.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/2288/data/5.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2288/data/6.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/2288/data/6.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2288/data/6.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/2288/data/6.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2288/data/7.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/2288/data/7.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2288/data/7.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/2288/data/7.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2288/data/8.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/2288/data/8.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2288/data/8.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/2288/data/8.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2288/data/9.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/2288/data/9.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2288/data/9.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/2288/data/9.out
(Stored with Git LFS)
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user