Oh My Holy FFF HDU - 4719 (线段树优化dp)

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.

Sample Input

2
5 2
1 4 3 2 5
5 2
5 4 3 2 1

Sample Output

Case #1: 31
Case #2: No solution

 

第一次做线段树优化dp的题目,先按常规来想,设dp[i][j] 定义为把 j 做为第 i 段结尾的时的最大分数,易推出状态转移方程

dp[i][j] = max(dp[i - 1][k] - a[k]) + a[j] * a[j] , (max(j - L, 0) <= k < j) 并且a[k] < a[j], 但是,我们发现首先我们的m,也就是分成几部分时不确定的,所以我们不需要第一维, 设dp[ i ] 表示 以 i 作为某一段结尾时可获得的最大分数, 易推出状态转移方程

dp[ i ] = max(dp[k] - a[k]) + a[i] * a[i] , ( (max(i - L, 0) <= k < i ) , 同意看出 a[i] * a[i] 是不变的, 所以我们只需求出dp[k[ - a[k] 的最大值, 一开始我想到的是用单调对列来维护,后面没做出来, 改线段树, 这里要注意 a[k] 一定要小于 a[j], 所以我们可以将原序列从小到大排序, 若值相同, 则把位置后的放前面(因为题目的要求是严格大于)

 

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include <string.h>
using namespace std;

typedef long long LL;
#define MAX 1000000
#define INF 0x7fffffff
#define lc rt << 1
#define rc rt << 1 | 1
LL dp[MAX + 10];
struct date
{
	LL x;
	int st;
} a[MAX];

bool cmp(date A, date B){
	if(A.x != B.x){
		return A.x < B.x;
	} else{
		return A.st > B.st;
	}
}
LL tree[MAX << 2];
void PushUp(int rt){
	tree[rt] = max(tree[lc], tree[rc]);
}

void Build(int le, int ri, int rt){
	tree[rt] = -1;
	if(le == ri){
		return ;
	}

	int m = (le + ri) >> 1;
	Build(le, m, lc);
	Build(m + 1, ri, rc);

	PushUp(rt);
}

void Update(int L, LL C, int le, int ri, int rt){
	if(le == ri){
		tree[rt] = C;
		return ;
	}

	int m = (le + ri) >> 1;
	if(L <= m){
		Update(L, C, le, m, lc);
	}
	if(L > m){
		Update(L, C, m + 1, ri, rc);
	}

	PushUp(rt);
}

LL QuerySum(int L, int R, int le, int ri, int rt){
	if(L <= le && ri <= R){
		return tree[rt];
	}

	int m = (le + ri) >> 1;
	LL ans = -1;

	if(L <= m){
		ans = max(ans, QuerySum(L, R, le, m, lc));
	}
	if(R > m){
		ans = max(ans, QuerySum(L, R, m + 1, ri, rc));
	}

	return ans;
}
int main()
{
    int n, L;
    int t;
    scanf("%d", &t);
    int cnt = 1;
    while(t--){
    	scanf("%d%d", &n, &L);
    	Build(0, n, 1);
    	for(int i = 1; i <= n; i++){
    		scanf("%I64d", &a[i].x);
    		a[i].st = i;
    	}

    	Update(0, 0, 0, n, 1);
    	sort(a + 1, a + n + 1, cmp);
    	memset(dp, -1, sizeof(dp));
    	for(int i = 1; i <= n; i++){
    		int st = a[i].st;
    		LL x = a[i].x;
    		LL pre = QuerySum(max(0, st - L), st - 1, 0, n, 1); //若返回值为-1, 则表示没有状态能转移到dp[st]
    		if(pre >= 0){
    			dp[st] = pre + x * x;
    			Update(st, dp[st] - x, 0, n, 1);
    		}
    		if(st == n){
    			break;
    		}
    	}
    	printf("Case #%d: ", cnt++);
    	if(dp[n] < 0){
    		printf("No solution\n");
    	} else{
    		printf("%I64d\n", dp[n]);
    	}
    dp[i][j] = max(dp[i - 1][k] - a[k]) + a[j] * a[j] , (max(i - L, 0) <= k < i);
    return 0;   
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值