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.
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:
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
View Code
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
相关文章推荐
- hdu 4727 The Number Off of FFF 简单题( 2013 ACM/ICPC Asia Regional Online —— Warmup2 1012)
- HDU 4725 The Shortest Path in Nya Graph(最短路径)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)
- hdu 4722 Good Numbers 数位dp(2013 ACM/ICPC Asia Regional Online —— Warmup2 1007)
- hdu 4720 Naive and Silly Muggles 外接圆(计算几何) 2013 ACM/ICPC Asia Regional Online —— Warmup2 1005
- hdu 4726 Kia's Calculation 数论(2013 ACM/ICPC Asia Regional Online —— Warmup2 1011)
- HDU4722——Good Numbers——2013 ACM/ICPC Asia Regional Online —— Warmup2
- HDU 4717 The Moving Points(三分法)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)
- hdu 4722 Good Numbers 2013 ACM/ICPC Asia Regional Online —— Warmup2
- HDU 4722 Good Numbers(位数DP)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)
- HDU4726——Kia's Calculation——2013 ACM/ICPC Asia Regional Online —— Warmup2
- hdu 4717 The Moving Points 三分法(2013 ACM/ICPC Asia Regional Online —— Warmup2 1002)
- HDU 4710 Balls Rearrangement && 2013 ACM/ICPC Asia Regional Online —— Warmup
- hdu 4750 Count The Pairs 并查集+递推(2013 ACM/ICPC Asia Regional Nanjing Online 1003)
- hdu 4734 F(x)2013 ACM/ICPC Asia Regional Chengdu Online
- HDU 4707 Pet && 2013 ACM/ICPC Asia Regional Online —— Warmup
- 2013 ACM/ICPC Asia Regional Chengdu Online HDU 4731 Minimum palindrome(规律)
- hdu 4751 Divide Groups bfs (2013 ACM/ICPC Asia Regional Nanjing Online 1004)
- HDU 4758——Walk Through Squares——2013 ACM/ICPC Asia Regional Nanjing Online
- HDU 4706 Children's Day && 2013 ACM/ICPC Asia Regional Online —— Warmup
- HDU 4714 Tree2cycle 求树的分叉数 && 2013 ACM/ICPC Asia Regional Online —— Warmup