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

39 篇文章 0 订阅

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

题解:很明显我们第一思路是dp  dp[i] = max(dp[j] + a[i] * a[i] - a[j] , dp[i]) (i > j >= i - l  &&  a[j] < a[i]) 但是呢 数据为1e5  n^2的复杂度显然不合理,因为是找区间最优解问题,我们就可以想到用线段树优化一下,我们发现 对于dp[i] = dp[j] - a[j] + a[i] * a[i] 中a[i] 是一定的 ,然后我们就可以在区间(i - l, i - 1) 内取 dp[j] - a[j] 的最大值就好了,因为要满足 a[i] > a[j] 所以我们应先按照a[i] 的大小排序,对于相等的,编号大的先操作,这样就可以满足对于每个数,只能找到符合条件的了。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=1e5+10;
typedef long long ll;

struct node
{
    ll x;
    int id;
    node(){}
    node(ll x_,int id_):x(x_),id(id_){}
    bool operator <(const node &xx)const
    {
        if(x!=xx.x) return x<xx.x;
        else return id>xx.id;
    }
}a[N];

struct node1
{
    int l,r;
    ll maxx;
}tree[N<<2];

int n,l;
ll dp[N];

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

ll query(int pl,int pr,int cur)
{
    if(pl<=tree[cur].l&&tree[cur].r<=pr)
    {
        return tree[cur].maxx;
    }

    ll res=-1;
    if(pl<=tree[cur<<1].r) res=max(res,query(pl,pr,cur<<1));
    if(pr>=tree[cur<<1|1].l) res=max(res,query(pl,pr,cur<<1|1));
    return res;
}

void update(int pos,int cur,ll val)
{
    if(tree[cur].l==tree[cur].r)
    {
        tree[cur].maxx=val;
        return;
    }
    if(pos<=tree[cur<<1].r) update(pos,cur<<1,val);
    else update(pos,cur<<1|1,val);
    tree[cur].maxx=max(tree[cur<<1].maxx,tree[cur<<1|1].maxx);
}

int main()
{
    int T,nn=1;
    ll x;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&l);
        for(int i=1;i<=n;i++)
        {
           scanf("%lld",&x);
           a[i]=node(x,i);
           dp[i]=-1;
        }
        
        sort(a+1,a+1+n);
        build(0,n,1);
        
        for(int i=1;i<=n;i++)
        {
            int id=a[i].id;
            ll cnt=query(max(0,id-l),id-1,1);
            if(id-l<=0) cnt=max(cnt,0ll);
            if(cnt>=0)
            {
                dp[id]=cnt+a[i].x*a[i].x;
                update(id,1,dp[id]-a[i].x);
            }
            if(id==n) break;
        }
        printf("Case #%d: ",nn++);
        if(dp[n]==-1)printf("No solution\n");
        else cout<<dp[n]<<endl;
    }
    return 0;
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值