# LOJ3158 [NOI2019]序列

## Description

$T \leq 10, 1 \leq L \leq K \leq n \leq 2 \times 10^5, \sum n \leq 10^6, 1 \leq a_i, b_i \leq 10^9$

## Solution

$ans = \max_{i = L}^{n - K + L} f_i + g_{i + 1}$

​这个东西实现起来比较复杂，后缀只需要用一个堆不断维护前$L - K$大的$b_i$。前缀需要用$2$个堆去维护，其中一个堆维护在$a_i$的前缀范围内选择了$a_i, b_i$的位置。另一个维护其它的选择了$a_i, b_i$的位置，转移就是不断尝试在当前位置选择$a_i, b_i$能不能得到更优的答案

## Code

###### 30pts
#include <bits/stdc++.h>

using namespace std;

#define fst first
#define snd second
#define mp make_pair
#define squ(x) ((LL)(x) * (x))
#define debug(...) fprintf(stderr, __VA_ARGS__)

typedef long long LL;
typedef pair<int, int> pii;

template<typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
template<typename T> inline bool chkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }

template<typename T> inline T read() {
T sum = 0, fg = 1; char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') fg = -1;
for (; isdigit(c); c = getchar()) sum = (sum << 3) + (sum << 1) + (c ^ 0x30);
return fg * sum;
}

const int maxn = 155;

pii A[maxn];
LL f[maxn][maxn][maxn];

int main() {
freopen("sequence.in", "r", stdin);
freopen("sequence.out", "w", stdout);

int T = read<int>();
while (T--) {
int n = read<int>(), K = read<int>(), L = read<int>();
for (int i = 1; i <= n; i++) A[i].fst = read<int>();
for (int i = 1; i <= n; i++) A[i].snd = read<int>();
sort(A + 1, A + n + 1, greater<pii>());
memset(f, 0, sizeof f);
for (int i = 1; i <= n; i++)
for (int j = 0, e = min(L, i); j <= e; j++)
for (int k = 0, e = min(K, i - j); k <= e; k++) {
if (j) chkmax(f[i][j][k], f[i - 1][j - 1][k] + A[i].fst + A[i].snd);
if (i - j <= K - L) {
chkmax(f[i][j][k], f[i - 1][j][k] + A[i].fst);
if (k) chkmax(f[i][j][k], f[i - 1][j][k - 1] + A[i].fst + A[i].snd);
} else {
chkmax(f[i][j][k], f[i - 1][j][k]);
if (k) chkmax(f[i][j][k], f[i - 1][j][k - 1] + A[i].snd);
}
}
printf("%lld\n", f[n][L][K - L]);
}

return 0;
}

###### 100pts
#include <bits/stdc++.h>

using namespace std;

#define fst first
#define snd second
#define mp make_pair
#define squ(x) ((LL)(x) * (x))
#define debug(...) fprintf(stderr, __VA_ARGS__)

typedef long long LL;
typedef pair<int, int> pii;

template<typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
template<typename T> inline bool chkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }

template<typename T> inline T read() {
T sum = 0, fg = 1; char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') fg = -1;
for (; isdigit(c); c = getchar()) sum = (sum << 3) + (sum << 1) + (c ^ 0x30);
return fg * sum;
}

const int maxn = 2e5 + 10;

pii A[maxn];
int a[maxn], cnt;
pii _a[maxn];

LL f[maxn], g[maxn];

int main() {
freopen("sequence.in", "r", stdin);
freopen("sequence.out", "w", stdout);

int T = read<int>();
while (T--) {
int n = read<int>(), K = read<int>(), L = read<int>(), R = n - K + L + 1;
for (int i = 1; i <= n; i++) A[i].fst = read<int>();
for (int i = 1; i <= n; i++) A[i].snd = read<int>();
sort(A + 1, A + n + 1, greater<pii>());
static bool b[maxn];
memset(f, 0, sizeof f), memset(g, 0, sizeof g), memset(b, 0, sizeof b);

for (int i = 1; i <= K; i++) _a[i] = mp(A[i].snd, i), f[L] += A[i].fst;
for (int i = 1; i <= L; i++) f[L] += A[i].snd, b[i] = 1;
priority_queue<pii, vector<pii>, greater<pii> > Q1(_a + 1, _a + L + 1);
for (int i = L + 1; i <= K; i++) {
f[i] = f[i - 1];
Q1.push(_a[i]), f[i] += _a[i].fst, b[i] = 1;
f[i] -= Q1.top().fst, b[Q1.top().snd] = 0, Q1.pop();
}

priority_queue<int, vector<int>, greater<int> > Q3;

for (int i = K + 1; i <= n; i++) {
int tt = K - Q3.size();
while (tt) if (b[tt]) Q3.push(A[tt].fst + A[tt].snd), b[tt] = 0, --tt; else break;
f[i] = f[i - 1];
int Max = INT_MIN, pos = 0, val = A[i].fst + A[i].snd;
if (!Q3.empty()) if(chkmax(Max, val - Q3.top())) pos = 1;
int pp = K - Q3.size();
if (!Q1.empty()) {
while (!Q1.empty() && !b[Q1.top().snd]) Q1.pop();
if (!Q1.empty() && chkmax(Max, val - A[pp].fst - Q1.top().fst)) pos = 2;
}
if (Max <= 0) continue;
f[i] += Max, Q3.push(val), b[i] = 1;
if (pos == 1) Q3.pop();
if (pos == 2) b[Q1.top().snd] = 0, Q1.pop();
}

for (int i = R; i <= n; i++) g[R] += (a[i] = A[i].snd);
priority_queue<int, vector<int>, greater<int> > Q2(a + R, a + n + 1);
for (int i = R - 1; i > L; i--) {
g[i] = g[i + 1];
Q2.push(A[i].snd), g[i] += A[i].snd;
g[i] -= Q2.top(), Q2.pop();
}

LL ans = 0;
for (int i = L; i < R; i++) chkmax(ans, f[i] + g[i + 1]);
printf("%lld\n", ans);
}

return 0;
}

posted @ 2019-09-17 10:58  xunzhen  阅读(83)  评论(1编辑  收藏