首页 > 代码库 > hdu4719——Oh My Holy FFF

hdu4719——Oh My Holy FFF

Oh My Holy FFF

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


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 bi > bi-1.
You give your division a score, which is calculated as , b0 = 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 <= 105), 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 <= Hi <= 105)
 

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
 

Source
2013 ACM/ICPC Asia Regional Online —— Warmup2
 

Recommend

线段树dp
dp[i] = max(dp[j] - arr[j] + arr[i] * arr[i])

       这题需要从小的开始处理,因为小的不受大的影响,所以先处理没关系,但是如果遇到有相同的高度的点j, i时,我们需要先处理排在后面的i,因为如果先处理前面的j,而且很不巧刚好j有最大值,那么处理i的时候就会查到那个值,但是这其实是非法的;反之我们先处理后面的i,此时j没有插入到线段树上,所以i不受影响,而处理前面的j的时候,也是往左找,所以不受后面的i的影响,查询的时候只要保证查询区间长度不大与d就行了

#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 100010;
int n, d;
int arr[N];
__int64 dp[N];
struct data
{
	int x, pos;
}sorting[N];

struct node
{
	int l, r;
	__int64 val;
}tree[N << 2];

int cmp(data a, data b)
{
	if (a.x != b.x)
	{
		return a.x < b.x;
	}
	return a.pos > b.pos;
}

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

void update(int p, int pos, __int64 val)
{
	if (tree[p].l == tree[p].r)
	{
		tree[p].val = val;
		return;
	}
	int mid = (tree[p].l + tree[p].r) >> 1;
	if (pos <= mid)
	{
		update(p << 1, pos, val);
	}
	else
	{
		update(p << 1 | 1, pos, val);
	}
	tree[p].val = max(tree[p << 1].val, tree[p << 1 | 1].val);
}

__int64 query(int p, int l, int r)
{
	if (l <= tree[p].l && r >= tree[p].r)
	{
		return tree[p].val;
	}
	int mid = (tree[p].l + tree[p].r) >> 1;
	if (r <= mid)
	{
		return query(p << 1, l, r);
	}
	else if (l > mid)
	{
		return query(p << 1 | 1, l, r);
	}
	else
	{
		return max(query(p << 1, l, mid), query(p << 1 | 1, mid + 1, r));
	}
}

int main()
{
	int t, icase = 1;
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d%d", &n, &d);
		memset (dp, -1, sizeof(dp));
		arr[0] = 0;
		dp[0] = 0;
		for (int i = 1; i <= n; ++i)
		{
			scanf("%d", &arr[i]);
			sorting[i].x = arr[i];
			sorting[i].pos = i;
		}
		sort (sorting + 1, sorting + n + 1, cmp);
		build(1, 0, n);
		update(1, 0, 0);
		for (int i = 1; i <= n; ++i)
		{
			int j = sorting[i].pos;
			__int64 cur = query(1, max(0, j - d), j - 1);
			if (cur >= 0)
			{
				dp[j] = cur + (__int64)arr[j] * arr[j];
				update(1, j, dp[j] - arr[j]);
			}
		}
		printf("Case #%d: ", icase++);
		if (dp[n] <= 0)
		{
			printf("No solution\n");
		}
		else
		{
			printf("%I64d\n", dp[n]);
		}
	}
	return 0;
}


hdu4719——Oh My Holy FFF