题目描述

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]=(j−h[i])2+min(f[i−1][k]+c∗abs(j−k)),k表示第i-1棵树高度为k时。
O(nh2),TLE(调一下常数吸口氧气说不定能过)。
 
将式子分类展开   

  f[i][j]=(j−h[i])2+min(f[i−1][k]−c∗k+c∗j)  (k<=j)

  f[i][j]=(j−h[i])2+min(f[i−1][k]+c∗k−c∗j)  (k>=j)

进行单调队列优化

分两类

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

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

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

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

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

Code:

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

;
const int inf = 0x7f7f7f7f;

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

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

最新文章

  1. C语言-结构体struct-联合体union-枚举enum
  2. 解决远程连接mysql错误1130代码的方法
  3. js的动态加载、缓存、更新以及复用(三)
  4. 最详细eclipse汉化插件安装教程
  5. 【个人使用.Net类库】(2)Log日志记录类
  6. UITableViewCell 自适应高度 ios8特性
  7. CustomProgressBar
  8. 两个bootstrap插件bootstrap-select和bootstrap-paginator
  9. SpringMVC06以对象的方式获取前台的数据
  10. Java 操作mongodb
  11. Fourier分析基础(二)——由级数导出连续Fourier变换
  12. 用C写一个web服务器(一) 基础功能
  13. linux常用命令(CentOS)
  14. Linux开始结束ping命令
  15. 3、MHC主要组织相容性复合体
  16. [CF453B]Little Pony and Harmony Chest
  17. Webpack傻瓜式指南(转)
  18. Hadoop基础-MapReduce的Combiner用法案例
  19. iOS开发--画一条黑色的横线
  20. printf(&quot;%d&quot;,5.01)和printf(&quot;%f&quot;,5)的输出结果

热门文章

  1. break和continue能否跳出函数
  2. elasticsearch 深入 —— 结构化搜索
  3. ahocorasick使用
  4. 微信小程序(9)--音频及视频弹幕
  5. #6392. 「THUPC2018」密码学第三次小作业 / Rsa (exgcd求逆元+快速幂+快速乘)
  6. java类使用
  7. spring的统一进行异常处理
  8. elasticsearch查询与sql对应关系
  9. HashMap、HashTable、ConcurrentHashMap区别
  10. 存储过程如何传变量到like下