HDU 4719 Oh My Holy FFF(DP+线段树)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)
Description
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
, b 0 = 0 and 1 <= k <= M, if there are M groups in total. Note that M can equal to 1. Given the heights of all soldiers, please tell us the best score you can get, or declare the division as impossible.
Input
Output
题目大意:有n个数,划分为多个部分,假设M份,每份不能多于L个。每个数有一个h[i],每份最右边的那个数要大于前一份最右边的那个数。设每份最右边的数为b[i],求最大的sum{b[i]² - b[i - 1]},1≤i≤M,其中b[0] = 0。
思路:朴素DP为,dp[i]表示以i为结尾的最大划分。那么dp[i] = max{dp[j] - h[j] + h[i]²},1≤i-j≤L,h[j]<h[i]。这种会超时,采取线段树优化。因为有两个限制,考虑到若h[j]≥h[i],那么求i的时候一定不会用到j,那么先按h排序再DP(h相同的,i大的排前面)。
PS:又忘了把int改成long long >_<
代码(781MS):
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL; const int MAXN = ; LL dp[MAXN];
int n, L;
LL tree[MAXN << ], maxt[MAXN << ]; void pushdown(int x) {
int ll = x << , rr = ll ^ ;
if(tree[x] != -) {
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] = -;
}
} 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 << , rr = ll ^ ;
int mid = (left + right) >> ;
if(a <= mid) update(ll, left, mid, a, b, val);
if(mid < b) update(rr, mid + , 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 << , rr = ll ^ ;
int mid = (left + right) >> ;
LL ret = -;
if(a <= mid) ret = max(ret, query(ll, left, mid, a, b));
if(mid < b) ret = max(ret, query(rr, mid + , 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 + , a + n + );
dp[n] = -;
memset(tree, , sizeof(tree));
memset(maxt, , sizeof(maxt));
update(, , n, , , );
for(int i = ; i <= n; ++i) {
LL tmp = query(, , n, max(, a[i].pos - L), a[i].pos - );
if(tmp == -) {
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(, , 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[n];
} int main() {
int T; scanf("%d", &T);
for(int t = ; t <= T; ++t) {
scanf("%d%d", &n, &L);
for(int i = ; i <= n; ++i) a[i].read(i);
LL ans = solve();
if(ans == -) printf("Case #%d: No solution\n", t);
else printf("Case #%d: %I64d\n", t, ans);
}
}
HDU 4719 Oh My Holy FFF(DP+线段树)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)的更多相关文章
- HDU 4722 Good Numbers(位数DP)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)
Description If we sum up every digit of a number and the result can be exactly divided by 10, we say ...
- HDU 4717 The Moving Points(三分法)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)
Description There are N points in total. Every point moves in certain direction and certain speed. W ...
- HDU 4714 Tree2cycle(树状DP)(2013 ACM/ICPC Asia Regional Online ―― Warmup)
Description A tree with N nodes and N-1 edges is given. To connect or disconnect one edge, we need 1 ...
- HDU 4725 The Shortest Path in Nya Graph(最短路径)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)
Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...
- HDU 5889 Barricade 【BFS+最小割 网络流】(2016 ACM/ICPC Asia Regional Qingdao Online)
Barricade Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- HDU4719-Oh My Holy FFF(DP线段树优化)
Oh My Holy FFF Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) T ...
- HDU 4747 Mex(线段树)(2013 ACM/ICPC Asia Regional Hangzhou Online)
Problem Description Mex is a function on a set of integers, which is universally used for impartial ...
- hdu 4747 Mex (2013 ACM/ICPC Asia Regional Hangzhou Online)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4747 思路: 比赛打得太菜了,不想写....线段树莽一下 实现代码: #include<iost ...
- HDU 4729 An Easy Problem for Elfness(主席树)(2013 ACM/ICPC Asia Regional Chengdu Online)
Problem Description Pfctgeorge is totally a tall rich and handsome guy. He plans to build a huge wat ...
随机推荐
- webpack——安装报错及解决办法
①先删除 删除全局webpack-cli webpack4.x开始官方文档是说要安装cli所以如果的在用4.+就需要卸载cli npm uninstall -g webpack-cli # 注释给我这 ...
- 如何配置Java环境变量
百度经验 | 百度知道 | 百度首页 | 登录 | 注册 新闻 网页 贴吧 知道 经验 音乐 图片 视频 地图 百科 文库 帮助 发布经验 首页 分类 任务 回享 商城 特色 知道 百度经验 &g ...
- Unity3D usage Experience
I have been using Unity3D to make game for half one year. I began to lean Unity3D with some books, o ...
- ref、refs使用的注意事项
ref是被用来给元素或子组件注册引用信息.引用信息将注册在父组件的 $refs 对象身上.如果在普通的DOM元素身上使用,引用指向就是DOM元素:如果用在子组件身上,引用就是指向组件实例. 当v-fo ...
- (暴力碾标算)NOIP模拟赛 宗教仪式
题意: 一个模式串,一个文本串,现定义最大失配值为k 求文本中最大失配值<=k的长度为len(模式串)的字串个数 失配值=当前字串中与模式串不同的字符个数 思路: 暴力,加一个跳出剪枝,居然过了 ...
- spring的事务处理
说到事务,无非就是事务的提交commit和回滚rollback. 事务是一个操作序列,这些操作要么全部都执行成功,事务去提交,要么就是有一个操作失败,事务去回滚. 要知道事务的4大特性ACID.即原子 ...
- springmvc请求数据的流程。
验证了我说的,从model层中拿来的数据,不管什么类型,都是通过隐含模型,中转,放入request中的.除非你特意把这些数据放到session域中 流程含义解释:(来自网友)(1)HTTP请求到达we ...
- haystack+Elasticsearch搜素引擎
搜索引擎原理 通过搜索引擎进行数据查询时,搜索引擎并不是直接在数据库中进行查询,而是搜索引擎会对数据库中的数据进行一遍预处理,单独建立起一份索引结构数据. 我们可以将索引结构数据想象成是字典书籍的索引 ...
- STM32(7)——通用定时器PWM输出
1.TIMER输出PWM基本概念 脉冲宽度调制(PWM),是英文“Pulse Width Modulation”的缩写,简称脉宽调制,是利用微处理器的数字输出来对模拟电路进行控制的一种 ...
- Python全栈day 02
Python全栈day 02 一.循环语句 while 用法 num = 1 while num <= 10: print(num) num += 1 # 循环打印输出1-10 while el ...