Oh My Holy FFF
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 904 Accepted Submission(s): 232
Problem Description
N soldiers from the famous "*FFF* army" is standing in a line, from left to right.
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:
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.
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)
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[i]=max(DP[k])+h[i]*h[i]-h[k]; 条件:k>=i-l , h[i]>h[k] . 但是可以看到题目数据有10^5,所以单纯的DP肯定不行,所以要用线段树优化。优化值是区间上的最大值,具体做法是单点优化,区间查询。当然在此之前要把H排序,H相等的位置靠后的放前面,最后挨个查询就行了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
struct mine
{
int p;
long long h;
};
struct mine h[100005];
struct tree
{
int l,r;
long long c;
};
struct tree d[400005];
int T,t,n,l;
long long dp[100005];
void qsort(int low,int high)
{
int i=low,j=high,t2=h[(i+j)/2].p;
long long t1=h[(i+j)/2].h;
while (i<=j)
{
while (h[i].h<t1 || (h[i].h==t1 && h[i].p>t2)) i++;
while (h[j].h>t1 || (h[j].h==t1 && h[j].p<t2)) j--;
if (i<=j)
{
int k;
k=h[i].h; h[i].h=h[j].h; h[j].h=k;
k=h[i].p; h[i].p=h[j].p; h[j].p=k;
i++; j--;
}
}
if (i<high) qsort(i,high);
if (low<j) qsort(low,j);
}
void buildtree(int i,int l,int r)
{
d[i].l=l; d[i].r=r; d[i].c=-1;
if (l==r) return;
else
{
buildtree(i*2,l,(l+r)/2);
buildtree(i*2+1,(l+r)/2+1,r);
}
}
void _insert(int i,int l,int r,long long c)
{
if (d[i].c<c) d[i].c=c;
int mid=(d[i].l+d[i].r)/2;
if (d[i].l==l && d[i].r==r)
d[i].c=c;
else if (r<=mid) _insert(i*2,l,r,c);
else if (l>mid) _insert(i*2+1,l,r,c);
else
{
_insert(i*2,l,mid,c);
_insert(i*2+1,mid+1,r,c);
}
}
long long _count(int i,int l,int r)
{
int mid=(d[i].l+d[i].r)/2;
if (d[i].l==l && d[i].r==r)
return d[i].c;
else if (r<=mid) return _count(i*2,l,r);
else if (l>mid) return _count(i*2+1,l,r);
else
{
long long k1=_count(i*2,l,mid),k2=_count(i*2+1,mid+1,r);
if (k1>k2) return k1;
else return k2;
}
}
int main()
{
scanf("%d",&T);
t=T;
while (T--)
{
memset(h,0,sizeof(h));
memset(dp,0,sizeof(dp));
memset(d,0,sizeof(d));
scanf("%d%d",&n,&l);
for(int i=1;i<=n;i++)
{
scanf("%I64d",&h[i].h);
h[i].p=i;
}
long long k1=h[n].h;
qsort(1,n);
buildtree(1,1,n);
for (int i=1;i<=n;i++) dp[i]=-1;
for(int i=1;i<=n;i++)
{
long long k=_count(1,max(h[i].p-l,1),max(h[i].p-1,1));
if (k!=-1) dp[ h[i].p ]=k+h[i].h*h[i].h-h[i].h;
else if (h[i].p-l<=0) dp[h[i].p]=h[i].h*(h[i].h-1);
_insert(1,h[i].p,h[i].p,dp[h[i].p]);
}
printf("Case #%d: ",t-T);
if (dp[n]!=-1)
{
dp[n]+=k1;
printf("%I64d\n",dp[n]);
}
else printf("No solution\n");
}
return 0;
}