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,000N100,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 }

 

 

转载于:https://www.cnblogs.com/2529102757ab/p/10878067.html

分数阶傅里叶变换(Fractional Fourier Transform, FRFT)是对传统傅里叶变换的拓展,它通过非整数阶的变换方式,能够更有效地处理非线性信号以及涉及时频局部化的问题。在信号处理领域,FRFT尤其适用于分析非平稳信号,例如在雷达、声纳和通信系统中,对线性调频(Linear Frequency Modulation, LFM)信号的分析具有显著优势。LFM信号是一种频率随时间线性变化的信号,因其具有宽频带和良好的时频分辨率,被广泛应用于雷达和通信系统。FRFT能够更精准地捕捉LFM信号的时间和频率信息,相比普通傅里叶变换,其性能更为出色。 MATLAB是一种强大的数值计算和科学计算工具,拥有丰富的函数库和用户友好的界面。在MATLAB中实现FRFT,通常需要编写自定义函数或利用信号处理工具箱中的相关函数。例如,一个名为“frft”的文件可能是用于执行分数阶傅里叶变换的MATLAB脚本或函数,并展示其在信号处理中的应用。FRFT的正确性验证通常通过对比变换前后信号的特性来完成,比如评估信号的重构质量、信噪比等。具体而言,可以通过计算原始信号与经过FRFT处理后的信号之间的相似度,或者对比LFM信号的关键参数(如初始频率、扫频率和持续时间)是否在变换后得到准确恢复。 在MATLAB代码实现中,通常包含以下步骤:首先,生成LFM信号模型,设定其初始频率、扫频率、持续时间和采样率等参数;其次,利用自定义的frft函数对LFM信号进行分数阶傅里叶变换;接着,使用MATLAB的可视化工具(如plot或imagesc)展示原始信号的时域和频域表示,以及FRFT后的结果,以便直观对比;最后,通过计算均方误差、峰值信噪比等指标来评估FRFT的性能。深入理解FRFT的数学原理并结合MATLAB编程技巧,可以实现对LFM信号的有效分析和处理。这个代码示例不仅展示了理论知识在
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值