链接
http://acm.hdu.edu.cn/showproblem.php?pid=6047
题意
给两个长度为n的序列a, b,现要求max(sum(a[n + 1] + … + a[2n])),其中a[i] = max(a[b[k]] … a[i - 1]),其中b[k]为b[1 … n]中的一个,每个a[i]选择的b[k]不能重复
思路
写这个博客蹭点访问量2333
肯定先选能选到的最大的数,贪心就好,用一个线段树维护区间最值
代码
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
template<class T1, class T2> inline void gmax(T1& a, T2 b) { if (a < b) a = b; }
typedef long long LL;
const int MAXN = 3e5 + 5;
const int MAXM = 1 << 21;
const int MOD = 1e9 + 7;
int n, m;
int seg_tree[MAXM], BASE;
int a[MAXN << 1], b[MAXN];
void update(int x) {
x += BASE;
seg_tree[x] = a[x - BASE] - x + BASE;
for (x >>= 1; x; x >>= 1) seg_tree[x] = max(seg_tree[x << 1], seg_tree[x << 1 | 1]);
}
int query(int l, int r) {
int ret = 0;
l += BASE - 1; r += BASE + 1;
for (; l ^ r ^ 1; l >>= 1, r >>= 1) {
if (~ l & 1) gmax(ret, seg_tree[l ^ 1]);
if (r & 1) gmax(ret, seg_tree[r ^ 1]);
}
return ret;
}
int main() {
while (~scanf("%d", &n)) {
for (BASE = 1; BASE <= 2 * n + 1; BASE <<= 1);
for (int i = 0; i <= BASE << 1; ++i) seg_tree[i] = 0;
for (int i = 1; i <= n; ++i) {
scanf("%d", a + i);
update(i);
}
for (int i = 0; i < n; ++i) scanf("%d", b + i);
sort(b, b + n);
LL ans = 0;
for (int i = 0; i < n; ++i) {
a[i + n + 1] = query(b[i], i + n);
update(i + n + 1);
(ans += a[i + n + 1]) %= MOD;
}
printf("%I64d\n", ans);
}
}
本文解析了HDU 6047题目的解题思路,通过贪心策略选取最大值,并利用线段树维护区间最值以求得序列的最大累积和。
6397

被折叠的 条评论
为什么被折叠?



