题目描述

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 ≤ height_i ≤ 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 \(X_2\).

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.

输入格式

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

* Lines \(2..N+1\): Line i+1 contains a single integer: \(height_i\)

输出格式

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

样例 #1

样例输入 #1

5 2
2
3
5
1
4

样例输出 #1

15

首先打出暴力dp,记录\(dp_{i,j}\)为前面\(i\)棵树且第\(i\)棵树的高度为\(j\)的最小代价。

\(dp_{i,k}=min(\forall h_i\le j\le 100,dp_{i-1,j}+(k-h[i])^2+|k-j|*c)\)

#include<bits/stdc++.h>
using namespace std;
const int N=100005;
int n,c,h[N],dp[N][105],ans=2e9;
int main()
{
memset(dp,0x3f,sizeof(dp));
scanf("%d%d",&n,&c);
for(int i=1;i<=n;i++)
scanf("%d",h+i);
for(int i=h[1];i<=100;i++)
dp[1][i]=(i-h[1])*(i-h[1]);
for(int i=2;i<=n;i++)
for(int k=h[i];k<=100;k++)
for(int j=0;j<=100;j++)
dp[i][k]=min(dp[i][k],dp[i-1][j]+(k-h[i])*(k-h[i])+abs(k-j)*c);
for(int i=0;i<=100;i++)
ans=min(ans,dp[n][i]);
printf("%d",ans);
return 0;
}

复杂度明显不过关,但是开个O2可以通过洛谷数据

开始考虑正解,在式子中,\((k-h_i)^2\)可以提到外面,同时可以把绝对值的两种情况分开考虑,要考虑的式子就是

\(dp_{i-1,j}+k*c-j*c(k\ge j)\)

和\(dp_{i-1,j}+j*c-k*c(k<j)\)

k*c也可以提到外面,然后对\(dp_{i-1,j}-j*c\)维护前缀最大值,对\(dp_{i-1,j}+j*c\)维护后缀最大值,就可以实现O(1)转移,可以通过此题。

#include<bits/stdc++.h>
using namespace std;
const int N=100005;
int n,c,h[N],dp[N][105],ans=2e9,s[N][105],t[N][105];
int main()
{
memset(dp,0x3f,sizeof(dp));
scanf("%d%d",&n,&c);
for(int i=1;i<=n;i++)
scanf("%d",h+i);
for(int i=h[1];i<=100;i++)
dp[1][i]=(i-h[1])*(i-h[1]);
s[1][0]=dp[1][0],t[1][100]=dp[1][100]+100*c;
for(int j=1;j<=100;j++)
s[1][j]=min(s[1][j-1],dp[1][j]-j*c);
for(int j=99;j>=0;j--)
t[1][j]=min(t[1][j+1],dp[1][j]+j*c);
for(int i=2;i<=n;i++)
{
for(int k=h[i];k<=100;k++)
dp[i][k]=min(s[i-1][k]+k*c,t[i-1][k]-k*c)+(k-h[i])*(k-h[i]);
s[i][0]=dp[i][0],t[i][100]=dp[i][100]+100*c;
for(int j=1;j<=100;j++)
s[i][j]=min(s[i][j-1],dp[i][j]-j*c);
for(int j=99;j>=0;j--)
t[i][j]=min(t[i][j+1],dp[i][j]+j*c);
}
for(int i=0;i<=100;i++)
ans=min(ans,dp[n][i]);
printf("%d",ans);
return 0;
}

[USACO2007NOVG] Telephone Wire G的更多相关文章

  1. [USACO07NOV]电话线Telephone Wire

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

  2. 【USACO07NOV】电话线Telephone Wire

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

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

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

  4. P2885 [USACO07NOV]电话线Telephone Wire

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

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

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

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

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

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

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

  8. [POJ3612] Telephone Wire(暴力dp+剪枝)

    [POJ3612] Telephone Wire(暴力dp+剪枝) 题面 有N根电线杆,初始高度为h[i],要给相邻的两根连线.可以选择拔高其中一部分电线杆,把一根电线杆拔高\(\Delta H\)的 ...

  9. [USACO 07NOV]电话线Telephone Wire

    题目描述 Farmer John's cows are getting restless about their poor telephone service; they want FJ to rep ...

  10. 【POJ3612】【USACO 2007 Nov Gold】 1.Telephone Wire 动态调节

    意甲冠军: 一些树高给出.行一种操作:把某棵树增高h,花费为h*h. 操作完毕后连线,两棵树间花费为高度差*定值c. 求两种花费加和最小值. 题解: 跟NOIP2014 D1T3非常像. 暴力动规是O ...

随机推荐

  1. 如何通过API接口获取淘宝的店铺所有商品详情

    在电子商务领域中,淘宝是亚洲最大的在线交易平台之一,拥有海量的商品资源和消费者.如果你是一名开发者,想要在自己的网站或者APP中嵌入淘宝商品资源,那么你就需要通过淘宝开放平台提供的API接口来获取这些 ...

  2. IOS苹果应用IPA一键签名工具(苹果重签名,企业签名,Windows平台,时间控制)

    苹果应用IPA一键签名工具可以在windows平台对苹果应用IPA文件重新签名,无需MAC苹果电脑和配置XCODE开发环境,便可以直接对IPA文件进行签名,同时支持修改BundleID, 不受描述文件 ...

  3. 试试用Markdown来设计表单

    相信很多后端开发.对于前端知识是比较零碎的,所以很多时候写表单这样的工作,一般就是复制黏贴,然后改改字段.对于HTML格式,一直觉得比较杂乱,不够简洁. 最近TJ发现了一个有趣的小工具:Create ...

  4. git命令和遇到的问题

    命令 1.快速关联/修改Git远程仓库地址 (1).删除本地仓库当前关联的无效远程地址,再为本地仓库添加新的远程仓库地址 git remote -v //查看git对应的远程仓库地址 git remo ...

  5. Solution -「CF 1477A」Nezzar and Board

    Description Link. $ n $ distinct integers $ x_1,x_2,\ldots,x_n $ are written on the board. Nezzar ca ...

  6. SonarQube系列-认证&授权的配置

    参考文档:https://docs.sonarqube.org/latest/instance-administration/security/ 概述 SonarQube具有许多全局安全功能: 认证和 ...

  7. Blackmail

    Blackmail Arthur Hailey The chief house officer, Ogilvie, who had declared he would appear at the Cr ...

  8. JuiceFS 目录配额功能设计详解

    JuiceFS 在最近 v1.1 版本中加入了社区中呼声已久的目录配额功能.已发布的命令支持为目录设置配额.获取目录配额信息.列出所有目录配额等.完整的详细信息,请查阅文档. 在设计此功能时,对于它的 ...

  9. 一次考试的T3

    啊这感觉不太可做观察性质,发现这个字符串只由ABC构成这个性质必须利用仅仅由3种字符组成意味着什么呢?这个字符串只有种可能性这个有什么用呢?只是说明暴力枚举的时间复杂度会小一些而已.不止是这些. 首先 ...

  10. 使用 Kubernetes 简化平台工程

    平台工程在现代应用程序开发和部署中发挥的作用至关重要.随着软件应用程序变得越来越复杂和分散,对稳健且可扩展的基础设施的需求变得越来越重要.这就是平台工程的作用所在,它是支持整个软件开发生命周期的支柱. ...