hdu 4719 Oh My Holy FFF(dp线段树优化)

Oh My Holy FFF

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)

Total Submission(s): 848    Accepted Submission(s): 219

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

zhuyuanchen520

题意:有n个数,划分为多个部分,如果M份,每份不能多于L个。每一个数有一个h[i],每份最右边的那个数要大于前一份最右边的那个数。

设每份最右      
  边的数为b[i]。求最大的sum{b[i]2 - b[i - 1]},1≤i≤M,当中b[0] = 0。

题解:dp[i]表示前i个数能达到的最大值。dp[i]=max(dp[i],dp[j]+h[i]*h[i]-h[j]),i-L<=j<i.

线段树优化:先排个序。按h从小到大排。相等的pos大的在前。这是为了保证严格升序。

然后逐个插入,更新a[i].pos位置的单点值,再维护区间最大值,具体见代码。

#include<cstring>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#define lson l,mid,idx<<1
#define rson mid+1,r,idx<<1|1
#define lc idx<<1
#define rc idx<<1|1
#define N 100010
#define ll long long

using namespace std;

int n,m;

struct node {
    int pos;
    ll num;
} a[N];

ll tree[N<<2];

void build(int l,int r,int idx) {
    tree[idx]=-1;
    if(r==l)return;
    int mid=(l+r)>>1;
    build(lson);
    build(rson);
}

void update(int l,int r,int idx,int x,ll k) {///把x位置的值更新
    if(l==r) {
        tree[idx]=max(tree[idx],k);
        return;
    }
    int mid=(l+r)>>1;
    if(x<=mid)update(lson,x,k);
    else      update(rson,x,k);
    tree[idx]=max(tree[lc],tree[rc]);
}

ll query(int l,int r,int idx,int x,int y) {
    if(l>=x&&y>=r) {
        return tree[idx];
    }
    int mid=(l+r)>>1;
    ll ans=-1;
    if(x<=mid)ans=max(ans,query(lson,x,y));
    if(y>mid) ans=max(ans,query(rson,x,y));
    return ans;
}

bool cmp(node a,node b) {
    if(a.num==b.num)return a.pos>b.pos;
    return a.num<b.num;
}

int main() {
    //freopen("test.in","r",stdin);
    int t;
    cin>>t;
    int ca=1;
    while(t--) {
        scanf("%d%d",&n,&m);
        for(int i=1; i<=n; i++) {
            scanf("%I64d",&a[i].num);
            a[i].pos=i+1;
        }
        sort(a+1,a+n+1,cmp);
        n++;
        build(1,n,1);
        update(1,n,1,1,0);
        ll ans=-1;
        for(int i=1; i<n; i++) {
            int l=(a[i].pos-m)>0?

(a[i].pos-m):1,r=a[i].pos-1;///能一组的最左区间和最右区间-1
            ll it=query(1,n,1,l,r);
            if(it==-1&&a[i].pos==n) {///-1表示分不了组
                break;
            }
            if(it==-1) continue;
            it+=a[i].num*a[i].num;
            if(a[i].pos==n) {//已经更新到n了。直接跳出
                ans=it;
                break;
            }
            update(1,n,1,a[i].pos,it-a[i].num);//减去当前a[i].num,非常巧妙
        }
        printf("Case #%d: ",ca++);
        if(ans==-1)printf("%s\n","No solution" );
        else         printf("%I64d\n",ans );
    }
    return 0;
}
时间: 2024-08-03 08:08:07

hdu 4719 Oh My Holy FFF(dp线段树优化)的相关文章

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

Oh My Holy FFF Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 606    Accepted Submission(s): 141 Problem Description N soldiers from the famous "*FFF* army" is standing in a line, from le

hdu 4719 Oh My Holy FFF(线段数+dp)

题目链接:hdu 4719 Oh My Holy FFF 题目大意:队伍里有n个人,给出每个人的身高,他们按照顺序排列,现在要将这n个人分成若干组,每一组的人数不得大于l,并且第i组的最后一个人的身高一定要大于第i?1组的最后一个人的身高.要求最后的权值最大,权值计算方法在题目中,k为组号. 解题思路:dp[i]表示以第i个人作为结尾的最大权值,那么dp[i]肯定是从前面的l-1个中转移过来的,即dp[i]=dp[j]+h[i]2?h[j] 要求h[i]>h[j]. 但是这样的复杂度为o(n2)

题解 HDU 3698 Let the light guide us Dp + 线段树优化

http://acm.hdu.edu.cn/showproblem.php?pid=3698 Let the light guide us Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 62768/32768 K (Java/Others) Total Submission(s): 759    Accepted Submission(s): 253 Problem Description Plain of despair was

hdu 3450 离散化+dp+线段树优化

链接:http://acm.hdu.edu.cn/showproblem.php?pid=3450 题意: 给你一串长度为n的序列,给一个d,要求找出有几个子序列能够满足两个相邻的元素之间差值不超过d. 思路: dp.定义dp[i]表示以第i个为结束的满足条件的子序列的个数. 转移方程:dp[i]=(∑i?1j=1dp[j])+1(abs(num[i]?num[j])<=d) 答案就是dp数组的总和最后扣掉n就可以了. 此时会发现更新的时间复杂度是O(n2),这个显然是过不了的. 转移的复杂度是

HDU 6155 Subsequence Count(矩阵 + DP + 线段树)题解

题意:01串,操作1:把l r区间的0变1,1变0:操作2:求出l r区间的子序列种数 思路:设DP[i][j]为到i为止以j结尾的种数,假设j为0,那么dp[i][0] = dp[i - 1][1] + dp[i -1][0] (0结尾新串) + dp[i - 1][0] (0结尾旧串) - dp[i - 1][0] (重复) + 1(0本身被重复时去除). 那么可以得到转移时的矩阵 $$ \left( \begin{matrix} dp[i - 1][0] & dp[i - 1][1] &am

hdu3698 Let the light guide us dp+线段树优化

http://acm.hdu.edu.cn/showproblem.php?pid=3698 Let the light guide us Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 62768/32768 K (Java/Others) Total Submission(s): 821    Accepted Submission(s): 285 Problem Description Plain of despair was

【uva1502/hdu4117-GRE Words】DP+线段树优化+AC自动机

这题我的代码在hdu上AC,在uva上WA. 题意:按顺序输入n个串以及它的权值di,要求在其中选取一些串,前一个必须是后一个的子串.问d值的和最大是多少. (1≤n≤2×10^4 ,串的总长度<=3*10^5) 题解: 这题一开始我的方向就错了,想了很久d[x][y]表示在AC自动机上的节点x.下一个串要大于y的dp.然而这样做数组要10^4*10^5=10^9级别,开都开不了,妥妥超时. 后来看了一眼题解...觉得自己智商真是感人... 用f[i]表示以第i个串为结尾的时候最大的d值,这样做

ZOJ 3650(多米诺骨牌 dp + 线段树优化)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3650 题意: 给你n个骨牌,每个骨牌有一个在x轴上的位置和高度,每个骨牌可以想左推也可以向右推,问最少多少步可以把骨牌全部推倒. 思路: 之前Goodbye 2014 上的E题就是一道多米诺骨牌的题目,虽然跟这道题目不太一样但是还是提供了一下思路, 附题解的链接: http://blog.csdn.net/u013649253/article/details/4

[后缀数组+dp/AC自动机+dp+线段树] hdu 4117 GRE Words

题意: 给你N个字符串, N(1 <= N <= 2w), 所有串的长度加一起不超过30w.每个串有个值.这个值[-1000, 1000]. 问不打乱字符串顺序,从中取若干个字符串,使得前一个串是后一个串的子串,求满足前面调条件的字符串值得和最大,求这个值. 思路: 其实就是一个很明显的dp. dp[i]代表以第i个字符串结尾的最大权值. 但是就是子串这个问题怎么处理. 由于这题数据比较水可以用后缀数组处理这个问题. 将所有字符串拼接,做sa. 每次在height数组里往上和往下寻找公共前缀等