您的位置:首页 > 其它

HDU 4719 Oh My Holy FFF(DP+线段树)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)

2013-09-11 23:46 441 查看
Description

N soldiers from the famous "*FFF* army" is standing in a line, from left to right.
o   o   o   o   o   o   o   o   o   o   o   o   o   o   o   o   o   o /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \

You, as the captain of *FFF*, want to divide them into smaller groups, but each group should still be continous in the original line. Like this:
o   o   o  |  o   o   o   o  |  o   o   o   o   o   o  |  o   o   o   o   o /F\ /F\ /F\ | /F\ /F\ /F\ /F\ | /F\ /F\ /F\ /F\ /F\ /F\ | /F\ /F\ /F\ /F\ /F\ / \ / \ / \ | / \ / \ / \ / \ | / \ / \ / \ / \ / \ / \ | / \ / \ / \ / \ / \

In your opinion, the number of soldiers in each group should be no more than L. Meanwhile, you want your division be "holy". Since the soldier may have different heights, you decide that for each group except the first one, its last soldier(which is the rightmost one) should be strictly taller than the previous group's last soldier. That is, if we set bi as the height of the last soldier in group i. Then for i >= 2, there should be b i > b i-1. You give your division a score, which is calculated as

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;

const int MAXN = 100010;

LL dp[MAXN];
int n, L;
LL tree[MAXN << 2], maxt[MAXN << 2];

void pushdown(int x) {
int ll = x << 1, rr = ll ^ 1;
if(tree[x] != -1) {
tree[ll] = max(tree[x], tree[ll]);
tree[rr] = max(tree[x], tree[rr]);
maxt[ll] = max(maxt[ll], tree[x]);
maxt[rr] = max(maxt[rr], tree[x]);
tree[x] = -1;
}
}

void update(int x, int left, int right, int a, int b, LL val) {
if(a <= left && right <= b) {
tree[x] = max(tree[x], val);
maxt[x] = max(maxt[x], val);
}
else {
pushdown(x);
int ll = x << 1, rr = ll ^ 1;
int mid = (left + right) >> 1;
if(a <= mid) update(ll, left, mid, a, b, val);
if(mid < b) update(rr, mid + 1, right, a, b, val);
maxt[x] = max(maxt[x], max(maxt[ll], maxt[rr]));
}
}

LL query(int x, int left, int right, int a, int b) {
if(a <= left && right <= b) return maxt[x];
else {
pushdown(x);
int ll = x << 1, rr = ll ^ 1;
int mid = (left + right) >> 1;
LL ret = -1;
if(a <= mid) ret = max(ret, query(ll, left, mid, a, b));
if(mid < b) ret = max(ret, query(rr, mid + 1, right, a, b));
return ret;
}
}

struct Node {
int h, pos;
void read(int i) {
pos = i;
scanf("%d", &h);
}
bool operator < (const Node &rhs) const {
if(h != rhs.h) return h < rhs.h;
return pos > rhs.pos;
}
} a[MAXN];

LL solve() {
sort(a + 1, a + n + 1);
dp
= -1;
memset(tree, 255, sizeof(tree));
memset(maxt, 255, sizeof(maxt));
update(1, 0, n, 0, 0, 0);
for(int i = 1; i <= n; ++i) {
LL tmp = query(1, 0, n, max(0, a[i].pos - L), a[i].pos - 1);
if(tmp == -1) {
if(a[i].pos == n) break;
else continue;
}
dp[a[i].pos] = tmp + LL(a[i].h) * a[i].h;
if(a[i].pos == n) break;
update(1, 0, n, a[i].pos, a[i].pos, dp[a[i].pos] - a[i].h);
}
//for(int i = 1; i <= n; ++i) printf("%I64d\n", dp[i]);
return dp
;
}

int main() {
int T; scanf("%d", &T);
for(int t = 1; t <= T; ++t) {
scanf("%d%d", &n, &L);
for(int i = 1; i <= n; ++i) a[i].read(i);
LL ans = solve();
if(ans == -1) printf("Case #%d: No solution\n", t);
else printf("Case #%d: %I64d\n", t, ans);
}
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐