题目描述

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. 洛谷 P1589 泥泞路 & 2019青岛市竞赛(贪心)

    题目链接 https://www.luogu.org/problemnew/show/P1589 解题思路 用结构体存下每一段泥泞路的左端点和右端点,然后用sort根据左端点排序,采用贪心的思想,从左 ...

  2. mysql双主+keepalived架构

    架构展示 操作系统 centos6.5 数据库 mysql5.7 master1 10.0.254.148 master2 10.0.254.147 VIP 10.0.254.88 (keepaliv ...

  3. 2018CCPC吉林赛区(重现赛)

    http://acm.hdu.edu.cn/contests/contest_show.php?cid=867 A题,直接分块,不知道正解是什么. #include<bits/stdc++.h& ...

  4. angularjs calling order

    Here's the calling order: app.config()app.run()directive's compile functions (if they are found in t ...

  5. textarea实现高度自适应

    css部分 #textarea { display: block; margin:0 auto; overflow: hidden; width: 550px; font-size: 14px; he ...

  6. 2.Web中使用iReport 整合----------创建html格式的

    转自:https://wenku.baidu.com/view/104156f9770bf78a65295462.html 1.

  7. centos7解决ssh登录速度慢的问题

    先备份/etc/ssh/sshd_config,备份命令为 cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak 1.su                  ...

  8. smbd - 向客户提供SMB/CIFS服务的服务器

    总览 SYNOPSIS smbd [-D] [-F] [-S] [-i] [-h] [-V] [-b] [-d <debug level>] [-l <log directory&g ...

  9. Git --06 Git-gui安装

    目录 1.Git-gui安装 1.Git-gui安装

  10. linux篇—Nginx反向代理负载均衡

    一.环境准备 反向代理功能架构 3台web服务器,组建出web服务器集群 web01 10.0.0.7 172.16.1.7 web02 10.0.0.8 172.16.1.8 web03 10.0. ...