acwing提高训练DAY1

数字三角形模型

acwing105

#include<cstdio>
#include<algorithm>

using namespace std;

const int N = 105;

int a[N][N];
int dp[N][N];

int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		memset(dp,0,sizeof dp);
		int r,c;
		scanf("%d%d",&r,&c);
		for(int i=1;i=r;i++){
			for(int j=1;j<=c;j++){
				scanf("%d",&a[i][j]);
			}
		}
		for(int i=1;i<=r;i++){
			for(int j=1;j<=c;j++){
				dp[i][j] = max(dp[i][j-1],dp[i-1][j])+a[i][j];
			}
		}
		printf("%d\n",dp[r][c]);
	}
	return 0;
} 

acwing1018

#include<cstdio>
#include<algorithm>

using namespace std;

const int N = 102,INF = 0x3f3f3f3f;
int a[N][N],dp[N][N];

int main(){
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			scanf("%d",&a[i][j]);
			
	for(int i=0;i<=n;i++){
		dp[0][i]=INF;
		dp[i][0]=INF;
	}
	dp[0][1] = 0;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			dp[i][j] = min(dp[i-1][j],dp[i][j-1])+a[i][j];
		}
	}
	printf("%d\n",dp[n][n]);
	return 0;
} 

acwing1027

双路径取最大值

在这里插入图片描述


在这里插入图片描述

#include<cstdio>
#include<algorithm>
#include<iostream>

using namespace std;

const int N = 15;

int n;
int w[N][N];
int f[N*2][N][N];

int main(){
	scanf("%d",&n);
	int a,b,c;
	while(cin>>a>>b>>c,a||b||c) w[a][b]=c;
	for(int k=2;k<=n+n;k++){
		for(int i1 = 1;i1<=n;i1++){
			for(int i2 = 1;i2<=n;i2++){
				int j1 = k-i1,j2 = k-i2;
				if(j1>=1&&j2>=1&&j1<=n&&j2<=n){
					int t = w[i1][j1];
					if(i1!=i2) t+=w[i2][j2];
					int &x = f[k][i1][i2];
					x = max(x,f[k-1][i1-1][i2-1]+t);
					x = max(x,f[k-1][i1-1][i2]+t);
					x = max(x,f[k-1][i1][i2-1]+t);
					x = max(x,f[k-1][i1][i2]+t);
				}
			}
		}
	}
	printf("%d\n",f[n+n][n][n]);
	return 0;
}

acwing275

思路与1027题一模一样
传纸条一个只能向下向右,一个向上向左,只要不重复选取一个数,和同时从(1,1)出发到(m,n)是等价的

#include<cstdio>
#include<algorithm>

using namespace std;

const int N = 55;
int a[N][N],f[N*2][N][N];

int main(){
	int m,n;
	scanf("%d%d",&m,&n);
	for(int i=1;i<=m;i++)
		for(int j=1;j<=n;j++)
			scanf("%d",&a[i][j]);
	
	for(int k=2;k<=n+m;k++){
		for(int i1=1;i1<=m;i1++){
			for(int i2=1;i2<=m;i2++){
				int j1 = k-i1,j2 = k-i2;
				if(j1>=1&&j1<=n&&j2>=1&&j2<=n){
					int t = a[i1][j1];
					if(i1!=i2) t+=a[i2][j2];
					int &x = f[k][i1][i2];
					x = max(f[k-1][i1-1][i2-1]+t,x);
					x = max(x,f[k-1][i1-1][i2]+t);
					x = max(x,f[k-1][i1][i2-1]+t);
					x = max(x,f[k-1][i1][i2]+t);
				}
			}
		}
	}
	printf("%d\n",f[n+m][m][m]);
	return 0;
} 

最长上升子序列模型

acwing1017

#include<cstdio>
#include<algorithm>

using namespace std;

int k,n;
const int N = 105;
int a[N],f[N][2];

int main(){
	scanf("%d",&k);
	while(k--){
		scanf("%d",&n);
		for(int i=1;i<=n;i++)scanf("%d",&a[i]);
		for(int i=1;i<=n;i++){
			f[i][0] = 1;
			for(int j=1;j<i;j++){
				if(a[j]<a[i]){
					f[i][0] = max(f[i][0],f[j][0]+1);
				}
			}
		}
		for(int i=n;i>=1;i--){
			f[i][1] = 1;
			for(int j = i+1;j<=n;j++){
				if(a[i]>a[j]){
					f[i][1] = max(f[i][1],f[j][1]+1);
				}
			}
		}
		int res = -1;
		for(int i=1;i<=n;i++){
			res = max(res,f[i][0]);
			res = max(res,f[i][1]);
		} 
		printf("%d\n",res);
	}
	return 0;
}

acwing1014

#include<cstdio>
#include<algorithm>

using namespace std;

int n;
const int N = 1010;
int a[N],f[N][2];

int main(){
		scanf("%d",&n);
		for(int i=1;i<=n;i++)scanf("%d",&a[i]);
		for(int i=1;i<=n;i++){
			f[i][0] = 1;
			for(int j=1;j<i;j++){
				if(a[j]<a[i]){
					f[i][0] = max(f[i][0],f[j][0]+1);
				}
			}
		}
		for(int i=n;i>=1;i--){
			f[i][1] = 1;
			for(int j = i+1;j<=n;j++){
				if(a[i]>a[j]){
					f[i][1] = max(f[i][1],f[j][1]+1);
				}
			}
		}
		int res = -1;
		for(int i=1;i<=n;i++){
			res = max(res,f[i][0]+f[i][1]-1);
		} 
		printf("%d\n",res);
	return 0;
}

acwing482

#include<cstdio>
#include<algorithm>

using namespace std;

int n;
const int N = 110;
int a[N],f[N][2];

int main(){
		scanf("%d",&n);
		for(int i=1;i<=n;i++)scanf("%d",&a[i]);
		for(int i=1;i<=n;i++){
			f[i][0] = 1;
			for(int j=1;j<i;j++){
				if(a[j]<a[i]){
					f[i][0] = max(f[i][0],f[j][0]+1);
				}
			}
		}
		for(int i=n;i>=1;i--){
			f[i][1] = 1;
			for(int j = i+1;j<=n;j++){
				if(a[i]>a[j]){
					f[i][1] = max(f[i][1],f[j][1]+1);
				}
			}
		}
		int res = -1;
		for(int i=1;i<=n;i++){
			res = max(res,f[i][0]+f[i][1]-1);
		} 
		printf("%d\n",n-res);
	return 0;
}

acwing1012

#include<cstdio>
#include<algorithm>

using namespace std;

const int N = 5005;

typedef pair<int,int>PII;

PII a[N];
int f[N];	

int main(){
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d%d",&a[i].first,&a[i].second);
	sort(a+1,a+1+n);
	for(int i=1;i<=n;i++){
		f[i] = 1;
		for(int j=1;j<i;j++){
			if(a[i].second>a[j].second){
				f[i] = max(f[i],f[j]+1);
			}
		}
	}
	int res = 0;
	for(int i=1;i<=n;i++) res = max(res,f[i]);
	printf("%d\n",res);
	return 0;
} 

acwing1016

#include<cstdio>
#include<algorithm>

using namespace std;

const int N = 1010;
int f[N],a[N];

int main(){
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		f[i] = a[i];
	}
	for(int i=2;i<=n;i++){
		for(int j=1;j<i;j++){
			if(a[i]>a[j]){
				f[i] = max(f[i],f[j]+a[i]);
			}
		}
	}
	int res = 0;
	for(int i=1;i<=n;i++) res = max(res,f[i]);
	printf("%d\n",res);
	return 0; 
} 

acwing1010

#include<cstdio>
#include<algorithm>

using namespace std;

const int N = 10100;

int f[N],a[N],k[N];

int main(){
	int n = 0;
	while(scanf("%d",&a[++n]));
	for(int i=1;i<=n;i++){
		f[i] = 1;
		for(int j=1;j<i;j++){
			if(a[i]<a[j]) f[i] = max(f[i],f[j]+1);
		}
	}
	int res = 0;
	for(int i=1;i<=n;i++) res = max(res,f[i]);
	int cnt = 0,t;
	for(int i=1;i<=n;i++){ 
		t = 1;
		bool flag = false;
		while(t<=cnt){
			if(k[t]>=a[i]){
				flag = true;
				k[t] = a[i];
				break;
			}
			t++;
		}
		if(!flag){
			k[++cnt] = a[i];
		}
	}
	printf("%d\n%d",res,cnt);
	return 0;
} 

acwing187

最长上升子序列加dfs
暴力搜索一个子序列
搜完n个数就结束
然后就如T1010一样

#include<cstdio>
#include<algorithm>
#include<iostream>

using namespace std;

const int N = 55;

int n;
int q[N];
int up[N],down[N],ans;

void dfs(int u,int su,int sd){//当前上升子序列个数,下降子序列个数 
	if(su+sd>=ans) return ;
	if(u==n){
		ans = su+sd;
		return ;
	}
	//情况1:将当前数放到上升子序列中
	int k = 0;
	while(k<su&&up[k]>=q[u]) k++;
	int t = up[k];
	up[k] = q[u];
	if(k<su) dfs(u+1,su,sd);
	else dfs(u+1,su+1,sd);
	up[k] = t;
	//情况2:将当前数放到下降子序列中 
	k = 0;
	while(k<sd&&down[k]<=q[u]) k++;
	t = down[k];
	down[k] = q[u];
	if(k<sd) dfs(u+1,su,sd);
	else dfs(u+1,su,sd+1);
	down[k] = t;	
}

int main(){
	while(cin>>n,n){
		for(int i=0;i<n;i++)cin>>q[i];
		ans = n;
		dfs(0,0,0);
		cout<<ans<<endl;
	}
	return 0;
} 

acwing272

明天补!!!
先求出公共子序列的前提下,每次往最长公共子序列中添加a[i],如果符合就长度加1并更新maxv
将情况分成子序列在中包含a[i]和不包含a[i],这样就不会重叠(a[i]==b[j])

#include<algorithm>
#include<cstdio>

using namespace std;

const int N = 3010;

int n;
int a[N],b[N];
int f[N][N];
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	for(int i=1;i<=n;i++)scanf("%d",&b[i]);
	
	for(int i=1;i<=n;i++){
		int maxv = 1;
		for(int j=1;j<=n;j++)
		{
			f[i][j] = f[i-1][j];
			if(a[i]==b[j])f[i][j] = max(f[i][j],maxv);
			if(b[j]<a[i]) maxv = max(maxv,f[i][j]+1);
		}
	}
	int res = 0;
	for(int i=1;i<=n;i++) res = max(res,f[n][i]);
	printf("%d\n",res);
	return 0;
} 

相关文章

显卡天梯图2024最新版,显卡是电脑进行图形处理的重要设备,...
初始化电脑时出现问题怎么办,可以使用win系统的安装介质,连...
todesk远程开机怎么设置,两台电脑要在同一局域网内,然后需...
油猴谷歌插件怎么安装,可以通过谷歌应用商店进行安装,需要...
虚拟内存这个名词想必很多人都听说过,我们在使用电脑的时候...