ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

P2885 [USACO07NOV]电话线Telephone Wire

2019-06-07 11:39:22  阅读:303  来源: 互联网

标签:Wire int Telephone wire poles P2885 new include define


题目描述

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
思路

先说说暴力做法吧,我们用f[i][j]表示前i棵树,其中第i棵树高度为j时的最小花费,于是我们有一个很好推的的dp式子了

f[i][j]=(j-h[i])^2+min(f[i-1][k]+c*abs(j-k))

于是对于每一棵树的每一种高度我们要枚举前面的那一棵树的所有高度来算一个最小值,于是复杂度大概是O(n*max(h)^2),在N ≤ 100,000N≤100,000,树的高度小于100100的数据下靠着比较优秀的常数以及O2卡了过去

 1 #include<iostream>
 2 #include<queue>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<cmath>
 6 #include<cstdlib>
 7 #define re register
 8 #define mp make_pair
 9 #define xx first
10 #define y second
11 #define maxn 100001
12 #define int long long
13 #define inf 9999999999
14 using namespace std;
15 int f[maxn][101],h[maxn];
16 int n,c,maxx,mid;
17 inline int read()
18 {
19     char c=getchar();
20     int x=0;
21     while(c<'0'||c>'9') c=getchar();
22     while(c>='0'&&c<='9')
23       x=(x<<3)+(x<<1)+c-48,c=getchar();
24     return x;
25 }
26 signed main()
27 {
28     n=read();
29     c=read();
30     for(re int i=1;i<=n;i++) h[i]=read(),maxx=max(h[i],maxx);
31     for(re int i=1;i<=n;i++)
32     for(re int j=0;j<=100;j++) f[i][j]=inf;
33     for(re int i=h[1];i<=maxx;i++)
34         f[1][i]=(i-h[1])*(i-h[1]);
35     for(re int i=2;i<=n;i++)
36     {
37         for(re int j=h[i];j<=maxx;j++)
38         {
39             for(re int k=h[i-1];k<=maxx;k++)
40             {
41                 f[i][j]=min(f[i][j],f[i-1][k]+c*abs(j-k)+(j-h[i])*(j-h[i]));
42             }
43         }
44     }
45     int ans=inf;
46     for(re int i=h[n];i<=maxx;i++)
47     ans=min(ans,f[n][i]);
48     cout<<ans<<endl;
49     return 0;
50 }

那么这个复杂度其实是明显不对的,我们用暴力刚过去也主要靠洛谷评测机的性能比较优秀

于是我们再去看dp的方程

有两种情况:

后面那些东西显然可以用单调队列优化一下

那么我们可以分类讨论一下,就可以解决这个恶心的绝对值了

至于我们如何分类讨论呢,我们只要巧妙的改变循环的顺序就可以做到这一点了

也就是说我们正着循环再倒着来一遍就好了

至于具体怎么搞,代码里说的应该很清楚了

 1 #include<iostream>
 2 #include<queue>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<cmath>
 6 #include<cstdlib>
 7 
 8 using namespace std;
 9 
10 inline int read()
11 {
12     char c=getchar();
13     int x=0;
14     while(c<'0'||c>'9') c=getchar();
15     while(c>='0'&&c<='9')
16       x=(x<<3)+(x<<1)+c-48,c=getchar();
17     return x;
18 }
19 
20 const int maxn=1e6+10; 
21 int dp[maxn][101];
22 int h[maxn];
23 int  maxx;
24 int  n;
25 const int inf=0x7fffffff;
26 int c;
27 
28 
29 int main()
30 {
31     //freopen("testdata.in","r",stdin);
32     n=read();
33     c=read();
34     for( int i=1;i<=n;i++) 
35     {h[i]=read();
36     maxx=max(h[i],maxx);
37     }
38     for( int i=1;i<=n;i++)
39     for( int j=0;j<=100;j++)
40      dp[i][j]=inf;
41     for( int i=h[1];i<=maxx;i++)
42         dp[1][i]=(i-h[1])*(i-h[1]);
43     
44     
45     
46 
47         
48         
49     for(int i=2;i<=n;i++)
50     {
51         for(int j=h[i];j<=maxx;j++)
52         {
53             for(int k=h[i-1];k<=maxx;k++)
54             {
55                 int qwq=j-k;
56                 dp[i][j]=min(dp[i][j],(dp[i-1][k]+c*abs(qwq)+(j-h[i])*(j-h[i])));
57             }
58         }
59     }
60     int ans=inf;
61     for(int i=h[n];i<=maxx;i++)
62     {
63         ans=min(ans,dp[n][i]);
64     }
65     printf("%d",ans);
66     return 0;
67 }

 

 

标签:Wire,int,Telephone,wire,poles,P2885,new,include,define
来源: https://www.cnblogs.com/2529102757ab/p/10878067.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有