澳门威利斯人_威利斯人娱乐「手机版」

来自 澳门威利斯人 2019-11-16 11:51 的文章
当前位置: 澳门威利斯人 > 澳门威利斯人 > 正文

学习滚动数组

poj 3280 区间dp

题意是给您三个串 让您增添字母和去掉字母把它成为回文串 扩充或删除室友费用的 问最小开支;

能够看到增删性质是千篇生机勃勃律的 全数去在这之中相当的小的 dp【i】【j】 表示i到j的微小花费 若是str【i】==str【j】 则dp【i】【j】=dp【i 1】【j-1】不然

dp[i][j]=min(dp[i 1][j] cost[str[i]-'a'],dp[i][j-1] cost[str[j]-'a']); 直接暴力就能够了

 

#include
#include
#include
using namespace std;

#define INF 0x3f3f3f3f
int min(int a,int b)
{
return a
}
int dp[2010][2010];
int main()
{
char str[2010];
char p[2];
int cost[30];
int i,j,n,len;
while(~scanf("%d%d",&n,&len))
{
scanf("%s",str);
memset(cost,0,sizeof(cost));
for(i=1;i<=n;i )
{
int a,b;
scanf("%s%d%d",p,&a,&b);
cost[p[0]-'a']=min(a,b);
}
memset(dp,0,sizeof(dp));
for(i=len-1;i>=0;i--)
{
for(j=i 1;j
{
dp[i][j]=min(dp[i 1][j] cost[str[i]-'a'],dp[i][j-1] cost[str[j]-'a']);
if(str[i]==str[j]) dp[i][j]=min(dp[i][j],dp[i 1][j-1]);
}
}
printf("%dn",dp[0][len-1]);
}
return 0;
}

3280 区间dp 题意是给你二个串 令你扩展字母和去掉字母把它形成回文串 扩大或删除室友开支的 问最小耗费; 能够看到增删性质是...

输入输出样例

输入样例#1:

5 2
2
3
5
1
4

出口样例#1:15

一开始自己写了个DP,居然能过样例
特别兴奋,
但是交上去只有70分
发现时间复杂度有点高
思路比较简单:
我们可以很容易的看出这道题具有无后效性,
用dp[i][j]表示前i棵树,第i棵树高度为j的最小代价
先预处理一下dp[1][j],然后对于每一棵树,我们枚举它的前一棵树的高度和这棵树的高度,
计算一下就好
时间复杂度n*h*h

图片 1 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<cstring> 6 #include<algorithm> 7 #include<queue> 8 #include<cstdlib> 9 using namespace std; 10 const int MAXN=100001; 11 const int INF =0x7f7f7f7f; 12 inline void read(int &n) 13 { 14 char c=' ';bool flag=0;n=0; 15 while(c<'0'||c>'9'){c=getchar();if(c=='-')flag=1;} 16 while(c>='0'&&c<='9')n=n*10 c-48,c=getchar(); 17 } 18 int dp[MAXN][201];// 第i棵树,中度为j的纤维成本 19 int n,C; 20 int a[MAXN]; 21 int maxheight; 22 int main() 23 { 24 read(n);read(C); 25 memset(dp,INF,sizeof(dp)); 26 for(int i=1;i<=n;i ) 27 read(a[i]),maxheight=max(maxheight,a[i]); 28 for(int i=a[1];i<=maxheight;i ) 29 dp[1][i]=(i-a[1])*(i-a[1]); 30 31 for(int i=2;i<=n;i )//枚举全体树 32 for(int j=a[i];j<=maxheight;j )//枚举这棵树的万丈 33 for(int k=a[i-1];k<=maxheight;k )//枚举前大器晚成棵树的惊人 34 dp[i][j]=min(dp[i][j], 35 ((j-a[i])*(j-a[i]) (dp[i-1][k]) abs(j-k)*C)); 36 37 38 int ans=0x7fffff; 39 for(int i=a[n];i<=maxheight;i ) 40 ans=min(ans,dp[n][i]); 41 printf("%d",ans); 42 return 0; 43 } View Code

然后化简一下式子

 

图片 2 1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring> 5 using namespace std; 6 const int MAXN=300005; 7 const int INF =0x7fffff; 8 const int maxheight=100; 9 int dp[301];// 第i棵树,高度为j的小小开销 10 int f[301]; 11 int n,C; 12 int a[MAXN]; 13 int bgsum[MAXN]; 14 int edsum[MAXN]; 15 int main() { 16 scanf("%d%d",&n,&C); 17 for(int i=0; i<n; i ) 18 scanf("%d",&a[i]); 19 memset(dp, 0x3f, sizeof(dp)); 20 for(int i=a[0]; i<=maxheight; i ) 21 dp[i]=(i-a[0])*(i-a[0]); 22 23 for(int i=1; i<n; i ) { //枚举全部树 24 memcpy(f,dp,sizeof(dp)); 25 for(int j=0; j<=maxheight; j ) dp[j]=bgsum[j]=edsum[j]=INF; 26 27 bgsum[0]=f[0]; 28 for(int j=1; j<=maxheight; j ) 29 bgsum[j]=min(bgsum[j-1],f[j]-C*j); 30 31 edsum[maxheight]=f[maxheight] maxheight*C; 32 for(int j=maxheight-1; j>=0; j--) 33 edsum[j]=min(edsum[j 1],f[j] C*j); 34 35 for(int j=a[i]; j<=maxheight; j ) //枚举那棵树的冲天 36 dp[j]=min(edsum[j]-C*j,bgsum[j] C*j) (j-a[i])*(j-a[i]); 37 } 38 int ans=0x7fffff; 39 for(int i=a[n-1]; i<=maxheight; i ) 40 ans=min(ans,dp[i]); 41 printf("%d",ans); 42 return 0; 43 } View Code

 

 

[USACO07NOV]电话线Telephone Wire,p2885usaco07nov 标题描述 Farmer John's cows are getting restless about their poor telephone service; they want FJ to replace the old...

    {  

输入输出格式

输入格式:

 

  • 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.

 

            dp[i][j]=max(dp[i][j-1],dp[i-1][j]);  

P2885 [USACO07NOV]电话线Telephone Wire,p2885usaco07nov

给你一串字符串,让您求最少投入多少个字符,能力使得那个字符串是个回文串。

难点呈报

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。

求两种花费加和纤维值。

    int b[5001];  

2,运用动态数组。

short int dp[5001][5001];  

dp[i][j]=max(dp[i][j],dp[i-1][j-1] 1);

题意:

[html]  

1,开short int型数组

            dp[i%2][j]=max(dp[i%2][j-1],dp[(i-1)%2][j]);  

    char str;  

 

假使直接求的话,势须要开多少个5001*5001的数组,铁定MLE。

[html] 

    char str;  

    printf("%dn",n-len);  

                dp[i][j]=max(dp[i][j],dp[i-1][j-1] 1);  

#include<stdio.h>  

        for(j=1;j<=n;j )  

        }  

        scanf("%c",&str);  

    {  

    for(i=0;i<=n;i )  

    int i,j;  

    {  

        }  

    return 0;  

依附dp滚动的历程咱们可见,dp【i】【j】的值不会与dp[i-2][0.....n]的值有关系。

2,滚动数组

#include<string.h>  

}  

            }  

        for(j=1;j<=n;j )  

            if(a[i]==b[j])  

    {  

设a[i]是以此字符串,b[i]是其一字符串的逆序串。

本文由澳门威利斯人发布于澳门威利斯人,转载请注明出处:学习滚动数组

关键词: 澳门威利斯人