#HDU4719#Oh My Holy FFF(DP+线段树优化)

27 篇文章 0 订阅
12 篇文章 0 订阅

Oh My Holy FFF

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 1146    Accepted Submission(s): 322


Problem 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.
 

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

题意n(n < 1e5)个人排成一行,把它成若干堆,要求每一堆的长度不超过l(l < 1e5),并且每一堆的最右一个人的身高都要比前一堆的最右一个人的身高要高,对于每一种方案,它的分数是SUM(b[k]^2-b[k-1] )  b[k] 为第k堆最右一个人的身高 要求最高的分数。

注意是不能打乱顺序的

定义Dp[i]表示前i个人分成若干堆得到的最高分数,第i个人是结尾的那一个,所以很显然有状态转移:

Dp[i] = max(Dp[j] - H[j]) + H[i] ^ 2,i - l <= j <= i - 1

我写得很基础,线段树里面带了左右下标

线段树优化:

Status Accepted
Time 998ms
Memory 8136kB
Length 2005
Lang G++
Submitted
Shared
RemoteRunId 20861352
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
using namespace std;

typedef long long LL;

const int Max = 100005;
const int INF = 0x3f3f3f3f;

struct node{
	LL h;
	int p;
	bool operator<(const node & X)const{
		if(h == X.h)	return p > X.p;
		return h < X.h;
	}
}P[Max];

struct tree{
	int l, r;
	LL val;
}Tr[Max << 2];

int N, L;
LL Dp[Max];

void getll(LL & num){
    char c;	int flag = 1;	num = 0LL;
    while((c = getchar()) < '0' || c > '9')	if(c == '-')	flag = -1;
    while(c >= '0'&& c <= '9'){	num = num * 10 + c - 48; c = getchar();}
    num *= flag;
}

void getint(int & num){
    char c;	int flag = 1;	num = 0;
    while((c = getchar()) < '0' || c > '9')	if(c == '-')	flag = -1;
    while(c >= '0'&& c <= '9'){	num = num * 10 + c - 48; c = getchar();}
    num *= flag;
}

void build(int i, int l, int r){
	Tr[i].l = l, Tr[i].r = r;
	Tr[i].val = -1;
	if(l == r)	return ;
	int mid = (l + r) >> 1;
	build(i << 1, l, mid);
	build(i << 1 | 1, mid + 1, r);
}

void insert(int i, int p, LL val){
	if(Tr[i].r < p || Tr[i].l > p)	return ;
	if(Tr[i].l == p && Tr[i].r == p){
		Tr[i].val = max(Tr[i].val, val);
		return ;
	}
	insert(i << 1, p, val);
	insert(i << 1 | 1, p, val);
	Tr[i].val = max(Tr[i << 1].val, Tr[i << 1 | 1].val);
}

LL Query(int i, int l, int r){
	if(Tr[i].r < l || Tr[i].l > r)	return -1;
	if(l <= Tr[i].l && r >= Tr[i].r)	return Tr[i].val;
	return max(Query(i << 1, l, r), Query(i << 1 | 1, l, r));
}

int main(){
	int T, tot = 0;
	getint(T);
	while(T --){
		getint(N), getint(L);
		printf("Case #%d: ", ++ tot);
		for(int i = 1; i <= N; ++ i)
			getll(P[i].h), P[i].p = i;
		sort(P + 1, P + N + 1);
		build(1, 0, N);
		insert(1, 0, 0);
		for(int p = 1; p <= N; ++ p){
			int i = P[p].p;
			LL j = Query(1, max(i - L, 0), i - 1);
			if(j >= 0){
				Dp[i] = j + P[p].h * P[p].h;
				insert(1, i, Dp[i] - P[p].h);
			}
		}
		if(Dp[N] > 0)	printf("%I64d\n", Dp[N]);
		else puts("No solution");
		memset(Dp, -1, sizeof(Dp));
	}
	return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值