题目描述

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;
}
 

[USACO 07NOV]电话线Telephone Wire的更多相关文章

  1. [USACO07NOV]电话线Telephone Wire

    [USACO07NOV]电话线Telephone Wire 时间限制: 1 Sec  内存限制: 128 MB 题目描述 电信公司要更换某个城市的网线.新网线架设在原有的 N(2 <= N &l ...

  2. P2885 [USACO07NOV]电话线Telephone Wire

    P2885 [USACO07NOV]电话线Telephone Wire 最近,Farmer John的奶牛们越来越不满于牛棚里一塌糊涂的电话服务于是,她们要求FJ把那些老旧的电话线换成性能更好的新电话 ...

  3. 【USACO07NOV】电话线Telephone Wire

    题目描述 电信公司要更换某个城市的网线.新网线架设在原有的 N(2 <= N <= 100,000)根电线杆上, 第 i 根电线杆的高度为 height_i 米(1 <= heigh ...

  4. P2885 [USACO07NOV]电话线Telephone Wire——Chemist

    题目: https://www.luogu.org/problemnew/show/P2885 由于把每一根电线杆增加多少高度不确定,所以很难直接通过某种方法算出答案,考虑动态规划. 状态:f [ i ...

  5. [luoguP2885] [USACO07NOV]电话线Telephone Wire(DP + 贪心)

    传送门 真是诡异. 首先 O(n * 100 * 100) 三重循环 f[i][j] 表示到第 i 个柱子,高度是 j 的最小花费 f[i][j] = min(f[i - 1][k] + abs(k ...

  6. 【动态规划】bzoj1705: [Usaco2007 Nov]Telephone Wire 架设电话线

    可能是一类dp的通用优化 Description 最近,Farmer John的奶牛们越来越不满于牛棚里一塌糊涂的电话服务 于是,她们要求FJ把那些老旧的电话线换成性能更好的新电话线. 新的电话线架设 ...

  7. DP+滚动数组 || [Usaco2007 Nov]Telephone Wire 架设电话线 || BZOJ 1705 || Luogu P2885

    本来是懒得写题解的…想想还是要勤发题解和学习笔记…然后就滚过来写题解了. 题面:[USACO07NOV]电话线Telephone Wire 题解: F[ i ][ j ] 表示前 i 根电线杆,第 i ...

  8. BZOJ_1705_[Usaco2007 Nov]Telephone Wire 架设电话线_DP

    BZOJ_1705_[Usaco2007 Nov]Telephone Wire 架设电话线_DP Description 最近,Farmer John的奶牛们越来越不满于牛棚里一塌糊涂的电话服务 于是 ...

  9. bzoj1705[Usaco2007 Nov]Telephone Wire 架设电话线(dp优化)

    1705: [Usaco2007 Nov]Telephone Wire 架设电话线 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 441  Solved: ...

随机推荐

  1. Java 遍历某个目录

    import java.io.File; import java.io.IOException; public class DirErgodic { public static void find(S ...

  2. P4036 [JSOI2008]火星人(splay+hash+二分)

    P4036 [JSOI2008]火星人 Splay维护hash,查询二分 $a[x].vl=a[lc].vl*ha[a[rc].sz+1]+a[x].w*ha[a[rc].sz]+a[rc].vl$ ...

  3. Java 小技巧和在Java避免NullPonintException的最佳方法(翻译)

                前几天就g+里面看到有人引用这篇博文.看了一下.受益颇多. 所以翻译过来,希望和大家一起学习.本人英语水平有限,假设有错,请大家指正. 原文地址(须要翻墙):http://ja ...

  4. 图标,空格,大小尖括号,段落,换行,标题,div白板,span白板

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. python ORM的使用

    安装 >pip install sqlalchemy #coding=utf-8 ''' 原始的sql语句 CREATE TABLE user ( id INTEGER NOT NULL AUT ...

  6. 对VS2019进行32位汇编环境配置

    1.库文件(很重要) 用我这一份就行:https://www.lanzous.com/i6364hg 2.VS依赖库 打开VS2019,选择桌面向导 配置项目时,选择新项目. 选择生成依赖项 选中ma ...

  7. Activiti6.0 java项目框架 spring5 SSM 工作流引擎 审批流程

    工作流模块----------------------------------------------------------------------------------------------- ...

  8. GeneXus笔记本—常用函数(上)

    国庆放假没事怎么办?写点笔记充会儿电! ≖‿≖✧   哈哈哈 !!最近在参与公司的其中一个项目中,发现了一些函数自己没见过 也没使用过,但是这些函数都是GeneXus中自带的一些 这此记录的目的就是为 ...

  9. LOJ3119. 「CTS2019 | CTSC2019」随机立方体 二项式反演

    题目传送门 https://loj.ac/problem/3119 现在 BZOJ 的管理员已经不干活了吗,CTS(C)2019 和 NOI2019 的题目到现在还没与传上去. 果然还是 LOJ 好. ...

  10. webRTC脱坑笔记(三)— webRTC API之RTCPeerConnection

    RTCPeerConnection API是每个浏览器之间点对点连接的核心,RTCPeerConnection是WebRTC组件,用于处理对等体之间流数据的稳定和有效通信. RTCPeerConnec ...