郑州大学算法设计与分析实验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;
}
相关推荐
dundunmm6 分钟前
机器学习之scikit-learn(简称 sklearn)
python·算法·机器学习·scikit-learn·sklearn·分类算法
古希腊掌管学习的神7 分钟前
[机器学习]sklearn入门指南(1)
人工智能·python·算法·机器学习·sklearn
波音彬要多做8 分钟前
41 stack类与queue类
开发语言·数据结构·c++·学习·算法
smile-yan42 分钟前
Provides transitive vulnerable dependency maven 提示依赖存在漏洞问题的解决方法
java·maven
果冻的猿宇宙1 小时前
Maven 中央仓库访问过慢的解决方案--设置国内镜像
maven·镜像·仓库·aliyun·国内镜像·mirror
哆啦 AI 梦1 小时前
【Maven】如何解决Maven循环依赖?
maven·循环依赖
Earnest~1 小时前
Maven极简安装&配置-241223
java·maven
皮蛋很白1 小时前
Maven 环境变量 MAVEN_HOME 和 M2_HOME 区别以及 IDEA 修改 Maven repository 路径全局
java·maven·intellij-idea
程序员老冯头2 小时前
第十五章 C++ 数组
开发语言·c++·算法
乘风御浪云帆之上6 小时前
数据库操作【JDBC & HIbernate & Mybatis】
数据库·mybatis·jdbc·hibernate