郑州大学算法设计与分析实验5

1

cpp 复制代码
#include <bits/stdc++.h> 
#define rep(i,a,b) for(register int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(register int i = (a); i >= (b); --i)
#define ls p<<1
#define rs p<<1|1
#define PII pair<int, int>
#define ll long long
#define ull unsigned long long
#define db double
#define endl '\n'
#define debug(a) cout<<#a<<"="<<a<<endl;
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define INF 0x3f3f3f3f 
#define x first
#define y second

using namespace std;
const int N = 1010;
int t, n, m;
int f[N], a[N];

void solve()
{
	int n = 0;
	while(cin >> a[++n]);
	--n;
	rep(i,1,n)
	{
		f[i] = 1;
		rep(j,1,i-1)	if(a[j] >= a[i])	f[i] = max(f[i], f[j] + 1);
	}
	int ans = 0;
	rep(i,1,n)	ans = max(ans, f[i]); 
	cout << ans << endl; 
}

int main()
{
	IOS	
//	freopen("1.in", "r", stdin);
//	cin >> t;
//	while(t --)	
	solve();
	return 0;
}

2

cpp 复制代码
#include <bits/stdc++.h> 
#define rep(i,a,b) for(register int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(register int i = (a); i >= (b); --i)
#define ls p<<1
#define rs p<<1|1
#define PII pair<int, int>
#define ll long long
#define ull unsigned long long
#define db double
#define endl '\n'
#define debug(a) cout<<#a<<"="<<a<<endl;
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define INF 0x3f3f3f3f 

using namespace std;
const int N = 2e5+10;
int t; 
int f[30][30];
int kkk(int m, int pan)
{
	if(pan==1 || m==0)
	{
		f[m][pan] = 1;
		return 1;
	}
	if(pan>m)
	{
		if(!f[m][pan])	f[m][pan] =	kkk(m,m);
		return f[m][pan];
	}	
	else
	{
		if(!f[m][pan-1])	f[m][pan-1]=kkk(m,pan-1);
		if(!f[m-pan][pan])	f[m-pan][pan]=kkk(m-pan,pan);
		return 	f[m][pan-1] + f[m-pan][pan];
	}	
}

void solve()
{
	int m,pan; cin >> m >> pan;
	cout << kkk(m,pan) << endl; 
}

int main()
{
	IOS	
//  	freopen("1.in", "r", stdin);
	rep(i,0,29)	rep(j,0,29)	f[i][j]=0;
	cin >> t;
	while(t --)	
	solve();
	return 0;
}

3

cpp 复制代码
#include <bits/stdc++.h> 
#define rep(i,a,b) for(register int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(register int i = (a); i >= (b); --i)
#define ls p<<1
#define rs p<<1|1
#define PII pair<int, int>
#define ll long long
#define ull unsigned long long
#define db double
#define endl '\n'
#define debug(a) cout<<#a<<"="<<a<<endl;
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define INF 0x3f3f3f3f

using namespace std;
const int N = 10100;
int t, n, m, cnt;
int f[N], v[N], w[N];

void solve()
{
	++ cnt;
	cin >> m >> n;
	rep(i,1,n)	cin >> v[i];
	rep(i,1,n)	cin >> w[i];
	rep(i,1,n)	fep(j,m,v[i])
		f[j] = max(f[j], f[j-v[i]] + w[i]);
	cout << "Case #" << cnt << ": " << f[m] << endl;
	rep(i,0,m)	f[i]=0;
}

int main()
{
	IOS	
//	freopen("1.in", "r", stdin);
	cin >> t;
	while(t --)	
	solve();
	return 0;
}

4

cpp 复制代码
#include <bits/stdc++.h> 
#define rep(i,a,b) for(register int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(register int i = (a); i >= (b); --i)
#define ls p<<1
#define rs p<<1|1
#define PII pair<int, int>
#define ll long long
#define ull unsigned long long
#define db double
#define endl '\n'
#define debug(a) cout<<#a<<"="<<a<<endl;
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define INF 0x3f3f3f3f

using namespace std;
const int N = 1010;
int t, n, m, cnt;
int f1[N][N], f2[N][N], s[N], a[N];

void solve()
{
	cin >> n;
	rep(i,1,n)	cin >> a[i], s[i] = s[i-1] + a[i];
	rep(len,2,n)
		rep(l,1,n+1-len)
		{
			int r = l + len - 1;
			f1[l][r] = INF;
			f2[l][r] = -INF;
			rep(k,l,r-1)
			{
				f1[l][r] = min(f1[l][r], f1[l][k] + f1[k+1][r] + s[r] - s[l-1]);
				f2[l][r] = max(f2[l][r], f2[l][k] + f2[k+1][r] + s[r] - s[l-1]);	
			}	
		}	
	cout << f1[1][n] << endl << f2[1][n] << endl;
}

int main()
{
	IOS	
//	freopen("1.in", "r", stdin);
//	cin >> t;
//	while(t --)	
	solve();
	return 0;
}

5

cpp 复制代码
#include <bits/stdc++.h> 
#define rep(i,a,b) for(register int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(register int i = (a); i >= (b); --i)
#define ls p<<1
#define rs p<<1|1
#define PII pair<int, int>
#define ll long long
#define ull unsigned long long
#define db double
#define endl '\n'
#define debug(a) cout<<#a<<"="<<a<<endl;
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define INF 0x3f3f3f3f

using namespace std;
const int N = 1010;
int t, n, m;
int f[N][N], a[N][N];

void solve()
{
	cin >> n;
	rep(i,1,n)	rep(j,1,n)	cin >> a[i][j];
	rep(i,1,n)	rep(j,1,n)
	{
		f[i][j] = a[i][j];;
		int x = 0;
		if(i-1>=1)	x = max(x, f[i-1][j]);
		if(j-1>=1)	x = max(x, f[i][j-1]);
		f[i][j] += x;
	}
	cout << f[n][n];
}

int main()
{
	IOS	
//	freopen("1.in", "r", stdin);
//	cin >> t;
//	while(t --)	
	solve();
	return 0;
}
相关推荐
羊小猪~~1 分钟前
神经网络基础--什么是正向传播??什么是方向传播??
人工智能·pytorch·python·深度学习·神经网络·算法·机器学习
软工菜鸡27 分钟前
预训练语言模型BERT——PaddleNLP中的预训练模型
大数据·人工智能·深度学习·算法·语言模型·自然语言处理·bert
南宫生29 分钟前
贪心算法习题其三【力扣】【算法学习day.20】
java·数据结构·学习·算法·leetcode·贪心算法
AI视觉网奇1 小时前
sklearn 安装使用笔记
人工智能·算法·sklearn
沐雪架构师1 小时前
mybatis连接PGSQL中对于json和jsonb的处理
json·mybatis
JingHongB1 小时前
代码随想录算法训练营Day55 | 图论理论基础、深度优先搜索理论基础、卡玛网 98.所有可达路径、797. 所有可能的路径、广度优先搜索理论基础
算法·深度优先·图论
weixin_432702261 小时前
代码随想录算法训练营第五十五天|图论理论基础
数据结构·python·算法·深度优先·图论
小冉在学习1 小时前
day52 图论章节刷题Part04(110.字符串接龙、105.有向图的完全可达性、106.岛屿的周长 )
算法·深度优先·图论
Repeat7151 小时前
图论基础--孤岛系列
算法·深度优先·广度优先·图论基础
小冉在学习1 小时前
day53 图论章节刷题Part05(并查集理论基础、寻找存在的路径)
java·算法·图论