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

发布时间: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 = 110;
int t, n;
int f[N][N], a[N][N];

void solve()
{
	cin >> n;
	rep(i,1,n)	rep(j,1,i)	cin >> a[i][j];
	rep(i,1,n)	rep(j,1,i)
	{
		f[i][j] = a[i][j];
		int t = 0;
		if(i-1>=1)	t = max(t, f[i-1][j]);
		if(j-1>=1 && i-1>=1)	t = max(t, f[i-1][j-1]);
		f[i][j] += t;
	}
	int ans = 0;
	rep(j,1,n)	ans = max(ans, f[n][j]);
	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 
#define x first
#define y second
using namespace std;


const int N = 1010;
int t, n, m;
char a[N], b[N];
int f[N][N];

void solve()
{
	cin >> (a+1);
	cin >> (b+1);
	n = strlen(a+1);
	m = strlen(b+1);
	rep(i,1,n)	rep(j,1,m)
	{
		f[i][j] = max(f[i-1][j], f[i][j-1]);
		if(a[i] == b[j])	f[i][j] = max(f[i][j], f[i-1][j-1] + 1);
	} 
	cout << f[n][m] << endl;
}

int main()
{
	IOS	
//	freopen("1.in", "r", stdin);
//	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 
#define x first
#define y second
using namespace std;

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

void solve()
{
	cin >> n;
	rep(i,1,n)	cin >> a[i];
	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;
}

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 = 110;
int t, n;
int a[N], s[N], f[N];

void solve()
{
	cin >> n;
	rep(i,1,n)	 cin >> a[i], s[i] = s[i-1] + a[i];
	int minn = INF;
	rep(i,1,n) 
	{
		f[i] = min(s[i], minn);
		minn = min(minn, f[i]);
	}
	int ans = 0;
	rep(i,1,n)	ans = max(ans, s[i] - f[i]);
	cout << max(0, ans) << 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 = 110;
int t, n, m;
int a[N][N], s[N][N];

void solve()
{
	cin >> n >> m;
	rep(i,1,n)	rep(j,1,m) 	cin >> a[i][j];
	rep(i,1,n)	rep(j,1,m)	s[i][j] = s[i-1][j] + s[i][j-1] - s[i-1][j-1] + a[i][j];
	int ans = 0, x1, y1, x2, y2;
	rep(i,1,n)	rep(j,1,m)	rep(p,1,i)	rep(q,1,j)
	{
		int t = s[i][j] - s[i][q-1] - s[p-1][j] + s[p-1][q-1];
		if(t > ans)
		{
			ans = t;
			x1 = p; y1 = q;
			x2 = i; y2 = j;
		}
	}
	cout << ans << endl;
	cout << x1 << ' ' << x2 << ' ' << y1 << ' ' << y2 << endl;
}

int main()
{
	IOS	
//	freopen("1.in", "r", stdin);
//	cin >> t;
//	while(t --)	
	solve();
	return 0;
}
文章来源:https://blog.csdn.net/weixin_61426225/article/details/135397896
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。