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

发布时间:2024年01月05日

1

#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

#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

#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

#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

#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;
}
文章来源:https://blog.csdn.net/weixin_61426225/article/details/135397916
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。