UESTC-594 我要长高(动态规划+单调队列优化)

我要长高

Time Limit: 4000/2000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)
 

韩父有 N 个儿子,分别是韩一,韩二…韩 N 。由于韩家演技功底深厚,加上他们间的密切配合,演出获得了巨大成功,票房甚至高达 2000 万。舟子是名很有威望的公知,可是他表面上两袖清风实则内心阴暗,看到韩家红红火火,嫉妒心遂起,便发微薄调侃韩二们站成一列时身高参差不齐。由于舟子的影响力,随口一句便会造成韩家的巨大损失,具体亏损是这样计算的,韩一,韩二…韩 N 站成一排,损失即为 C× (韩 i 与韩 i+1 的高度差( 1i<N ))之和,搞不好连女儿都赔了.韩父苦苦思索,决定给韩子们内增高(注意韩子们变矮是不科学的只能增高或什么也不做),增高 1 cm是很容易的,可是增高 10 cm花费就很大了,对任意韩 i ,增高 H cm的花费是 H2 .请你帮助韩父让韩家损失最小。

Input

有若干组数据,一直处理到文件结束。

每组数据第一行为两个整数:韩子数量 N ( 1N50000 )和舟子系数 C ( 1C100 )

接下来 N 行分别是韩i的高度( 1hi100 )。

Output

对每组测试数据用一行输出韩家的最小损失。

Sample input and output

Sample Input Sample Output
5 2
2
3
5
1
4
15
分析:
dp[i][j]表示第i个人身高为j时的最小花销;
dp[i][j]=min(dp[i-1][k]+c*abs(j-k))+(j-hi[i])*(j-hi[i]);
但看此公式,需要三重循环,时间复杂度为O(n^3),会超时,故使用单调队列优化;
当当前这个人的身高高于上一个人时,即j>k,则 dp[i][j]=dp[i-1][k]-c*k+c*j+(j-hi[i])*(j-hi[i]);
反之, dp[i][j]=dp[i-1][k]+c*k-c*j+(j-hi[i])*(j-hi[i]);
对f[i-1][k]=dp[i-1][k]+c*k进行单调队列优化。
每当i进行一次循环的时候,都对上一个人的各个身高的花费进行加工运算,在与当前人的身高进行运算(可利用j的递增与递减保证到达j身高前上一个人的数据已获得)
用cur的1与0之间的转换,来表示上一个人与当前人。



(注意,只有形如 dp[i]=max/min (f[k]) + g[i]  (k<i && g[i]是与k无关的变量)才能用到单调队列进行优化。)


</pre><pre name="code" class="cpp">#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

#define INFS 0x3fffffff
#define BG 50005

int dp[2][105];//because the best height of current one is influenced by the previous one and his real height,we must hold a dp list meaning cost of all his possible height for the next person to refer to.And this list is also generated by the nest value of function--nowf(about all the variables of last person).
int q[105];//And this best value is token by Monotonous Queue Optimization method.
int main()
{
    int n,c;
    int cur;
    int nowf=0;
    while(cin>>n>>c)//scanf("%d%d",&n,&c)!=EOF)
    {
        int i,j;
        int res=INFS;
        int tail,head;
        int x=0;
        cur=0;
        scanf("%d",&x);
        memset(q,0,sizeof(int)*105);
        for(j=1;j<x;j++)
            dp[cur][j]=INFS;
        for(j=x;j<=100;j++)
            dp[cur][j]=(x-j)*(x-j);
        cur=1-cur;
        for(i=2;i<=n;i++)
        {
            //scanf("%d",&x);
            cin>>x;
            //suppose the current one is taller than the previous one.
            tail=head=0;//the queue q[105] is refreshed twice every time(i).
            for(j=1;j<=100;j++)//j is from 1 to 100
            {
                nowf=dp[1-cur][j]-c*j;//the previous one
                while(nowf<q[tail-1]&&head<tail)
                    tail--;
                q[tail++]=nowf;
                if(j<x)
                    dp[cur][j]=INFS;//the current one
                else
                    dp[cur][j]=q[head]+c*j+(j-x)*(j-x);
            }
            //suppose the current one is shorter than the previous one
            tail=head=0;//the second refresh
            for(j=100;j>=1;j--)//j is from 100 to 1(because the previous one is taller)
            {
                nowf=dp[1-cur][j]+c*j;
                while(nowf<q[tail-1]&&head<tail)
                    tail--;
                q[tail++]=nowf;
                if(j>=x)//the occasion where j<x is already set below.
                    dp[cur][j]=min(dp[cur][j],q[head]-c*j+(j-x)*(j-x));
                //refresh and compare the taller occasion and the shorter occasion and fetch the smaller one.
            }
            cur=1-cur;//the current one becomes the previous one(for next round).
        }
        for(j=1;j<=100;j++)
            res=min(res,dp[1-cur][j]);
        printf("%d\n",res);
    }
    return 0;
}








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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值