[USACO 07NOV]电话线Telephone Wire

题目描述

Farmer John's cows are getting restless about their poor telephone service; they want FJ to replace the old telephone wire with new, more efficient wire. The new wiring will utilize N (2 ≤ N ≤ 100,000) already-installed telephone poles, each with some heighti meters (1 ≤ heighti ≤ 100). The new wire will connect the tops of each pair of adjacent poles and will incur a penalty cost C × the two poles' height difference for each section of wire where the poles are of different heights (1 ≤ C ≤ 100). The poles, of course, are in a certain sequence and can not be moved.

Farmer John figures that if he makes some poles taller he can reduce his penalties, though with some other additional cost. He can add an integer X number of meters to a pole at a cost of X2.

Help Farmer John determine the cheapest combination of growing pole heights and connecting wire so that the cows can get their new and improved service.

给出若干棵树的高度,你可以进行一种操作:把某棵树增高h,花费为h*h。

操作完成后连线,两棵树间花费为高度差*定值c。

求两种花费加和最小值。

 

输入格式

* Line 1: Two space-separated integers: N and C

* Lines 2..N+1: Line i+1 contains a single integer: heighti

输出格式

* Line 1: The minimum total amount of money that it will cost Farmer John to attach the new telephone wire.

输入输出样例

输入 #1
5 2
2
3
5
1
4
输出 #1
  15
 
 
显然DP。
 
先暴力,设f[i][j]表示前i棵树,第i棵树高度为j时的最小话费。
  f[i][j]=(jh[i])2+min(f[i1][k]+cabs(jk)), k表示第i-1棵树高度为k时。
O(nh2),TLE(调一下常数吸口氧气说不定能过)。
 
将式子分类展开    

  f[i][j]=(jh[i])2+min(f[i1][k]ck+cj)  (k<=j)

  f[i][j]=(jh[i])2+min(f[i1][k]+ckcj)  (k>=j)

 

进行单调队列优化

分两类

  1令mi = min(f[i-1][k] - c*k);要使min(f[i1][k]ck+cj)最小,就让mi最小。

分两类:1. k小于j,预处理mi即可。2. k等于j,在顺序枚举j的时候k=j即可。在这两种情况中,显然我们能保证k<=j。

2:令mi = min(f[i-1][k] + c*k);要使min(f[i1][k]+ckcj)最小,就让mi最小。

此时倒序枚举即可。能根据答案单调性质保证答案最优且k>=j。

 

 

下面now^1的意思就是只记录当前的和前一个(奇偶性变化,省空间)。

 

Code:
#include<iostream>
#include<cmath>
using namespace std;

const int maxn = 100001;
const int inf = 0x7f7f7f7f;

int f[2][101],n,c,m;
int h[maxn];
int ans = inf;

int main(){
    cin>>n>>c;
    int now = 1;
    for(int i = 1;i<=n;++i)cin>>h[i],m = max(m,h[i]);
    for(int i = 0;i<=m;++i)f[0][i] = f[1][i] = inf;
    for(int i = h[1];i<=m;++i)f[now][i] = (i-h[1])*(i-h[1]);
    for(int i = 2;i<=n;++i){
        now ^= 1;
        int mi = inf;for(int j = h[i-1];j<=m;j++){//从h[i-1]开始,因为高度不会下降到h[i-1]以下。
            mi = min(k,f[now^1][j]-j*c);
            if(j >= h[i])f[now][j] = mi+(j-h[i])*(j-h[i])+c*j;
        }
        mi = inf;
        for(int j = m;j>=h[i];--j){
            mi = min(k,f[now^1][j]+j*c);
            f[now][j] = min(f[now][j],mi-c*j+(j-h[i])*(j-h[i]));
        }
        for(int i = 0;i<=m;++i)f[now^1][i] = inf;
    }
    for(int i=h[n];i<=m;i++)
    ans=min(ans,f[now][i]);
    cout<<ans<<endl;
}

 

 

 

转载于:https://www.cnblogs.com/guoyangfan/p/11241998.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值