HDU 4719 Oh My Holy FFF (线段树+DP)

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 bi > b i − 1 b_{i-1} bi1.

You give your division a score, which is calculated as ∑ ( b k 2 − b k − 1 ) \sum \left( b^{2}_{k}-b_{k-1}\right) (bk2bk1), b 0 b_0 b0 = 0 and 1 < = k < = M 1 <= k <= M 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 < = 1 0 5 1 <= L <= N <= 10^5 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 < = H i < = 1 0 5 1 <= H i <= 10^5 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

Solution
先考虑暴力的做法:
定义 d p [ i ] dp[i] dp[i] 为前 i i i 个人划分为若干组的最大得分,容易想到转移方程为
d p [ i ] = max ⁡ { d p [ j ] + b [ i ] 2 − b [ j ] } ( b [ j ] < b [ i ] , i − l ≤ j ≤ i − 1 ) dp\left[ i\right] =\max \left\{ dp\left[ j\right] +b\left[ i\right] ^{2}-b\left[ j\right] \right\}(b\left[j\right] < b\left[i\right],i - l \leq j \leq i-1) dp[i]=max{dp[j]+b[i]2b[j]}(b[j]<b[i],ilji1)
这样显然会TLE
我们再转换一下公式
d p [ i ] = b [ i ] 2 + max ⁡ { d p [ j ] − b [ j ] } dp\left[ i\right] =b\left[ i\right] ^{2}+\max \left\{ dp\left[ j\right] -b\left[ j\right] \right\} dp[i]=b[i]2+max{dp[j]b[j]}
会发现每次需要求出的是符合条件的 d p [ j ] − b [ j ] dp\left[ j\right] -b\left[ j\right] dp[j]b[j]的最大值,所以我们可以用线段树来维护
为了保证线段树中的值合法,我们还需要将所有值先按照身高从低到高,身高相同再按照原始编号从大到小排序
从而保证了每次线段树的查询都满足 ( b [ j ] < b [ i ] , i − l ≤ j ≤ i − 1 ) (b\left[j\right] < b\left[i\right],i - l \leq j \leq i-1) (b[j]<b[i],ilji1)

Code

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#define ls rt << 1
#define rs rt << 1 | 1
using namespace std;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int MX = 2e5 + 7;

int n,d;

struct node{
    int id;
    ll val;
    inline bool operator<(const node&it)const{
        if(val == it.val) return id > it.id;
        return val < it.val;
    }
}a[MX];

struct SegTree{
    int l,r;
    ll maxn;
}t[MX << 3];

void build(int rt,int l,int r){
    t[rt].l = l, t[rt].r = r;
    t[rt].maxn = -LINF;
    if(l == r) return ;
    int mid = (l + r) >> 1;
    build(ls,l,mid);
    build(rs,mid+1,r);
}

void push_up(int rt){
    t[rt].maxn = max(t[ls].maxn,t[rs].maxn);
}

void upd(int rt,int pos,ll k){
    int l = t[rt].l, r = t[rt].r;
    if(l == r){
        t[rt].maxn = k;
        return ;
    }
    int mid = (l + r) >> 1;
    if(pos <= mid) upd(ls,pos,k);
    else upd(rs,pos,k);
    push_up(rt);
}

ll query(int rt,int L,int R){
    int l = t[rt].l , r = t[rt].r;
    if((L <= l && r <= R) || l == r){
        return t[rt].maxn;
    }
    ll res = -LINF;
    int mid = (l + r) >> 1;
    if(L <= mid) res = max(res,query(ls,L,R));
    if(R > mid) res = max(res,query(rs,L,R));
    return res;
}

int main(){
    int T;scanf("%d",&T);
    int cas = 0;
    while(T--){
        scanf("%d%d",&n,&d);
        for(int i = 1;i <= n;++i){
            scanf("%lld",&a[i].val);
            a[i].id = i;
        }
        sort(a + 1,a + 1 + n);
        build(1,0,n);
        upd(1,0,0);//初始化dp[0]
        for(int i = 1;i <= n;++i){
            int L = max(0,a[i].id - d), R = a[i].id - 1;
            ll tmp = query(1,L,R);
            if(tmp == -LINF){//非法状态
                if(a[i].id == n) {printf("Case #%d: No solution\n",++cas);break;}
                continue;
            } else{
                // dp[a[i].id] = a[i].val * a[i].val + tmp;
                if(a[i].id == n) {printf("Case #%d: %lld\n",++cas,a[i].val * a[i].val + tmp);break;}
            }
            upd(1,a[i].id,a[i].val * a[i].val + tmp - a[i].val);
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值