其他分享
首页 > 其他分享> > P2885 [USACO07NOV]电话线Telephone Wire

P2885 [USACO07NOV]电话线Telephone Wire

作者:互联网

题目描述

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