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 , 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

The first line has a number T (T <= 10) , indicating the number of test cases.  For each test case, first line has two numbers N and L (1 <= L <= N <= 10 5), as described above.  Then comes a single line with N numbers, from H1 to Hn, they are the height of each soldier in the line, from left to right. (1 <= H i <= 10 5)
 

Output

For test case X, output "Case #X: " first, then output the best score.

题目大意:有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);
}
}

最新文章

  1. C#之设计模式
  2. 再谈 $* 和 $@ 在 Bash 中的表现
  3. sql经常会遇到“将截断二进制或字符串”的错误——处理办法
  4. ArrayList、Vector、HashMap、HashTable、HashSet的默认初始容量、加载因子、扩容增量
  5. 动手学习TCP:客户端状态变迁
  6. 对List里的对象元素进行排序
  7. 记录C++学习历程
  8. spring mvc中的valid
  9. python的资料
  10. 【技术贴】关闭CMD错误提示声音
  11. 无法更新 EntitySet“GuigeInfo”,因为它有一个 DefiningQuery,而 &lt;ModificationFunctionMapping&gt; 元素中没有支持当前操作的 &lt;InsertFunction&gt; 元素。
  12. threejs 组成的3d管道,寻最短路径问题
  13. JUnit4测试出错(一)
  14. reserve的使用
  15. thymeleaf标签必须由匹配的结束标记终止
  16. 算法之Python实现 - 002 : 换钱的最少货币数补充(每种货币只能使用一次)
  17. Oracle 序列(sequence)
  18. 使用Java泛型返回动态类型
  19. c++动态库封装及调用(3、windows下动态库调用)
  20. Uboot USB模式(RK3288变砖头的解决办法)

热门文章

  1. 全局变量&amp;局部变量&amp;Static存储&amp;Register变量
  2. Spring 事务声明无效果(转)
  3. JS中遍历数组、对象的方式
  4. Webpack学习笔记九 webpack优化总结
  5. DevOps - 项目私库 - Nexus Repository
  6. django环境搭建和学习
  7. 阿里云提醒 网站被WebShell木马后门的处理过程
  8. ctf题目writeup(4)
  9. C语言实例解析精粹学习笔记——39(简单的文本编辑器)
  10. NO-ZERO(空格补全)