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

1

#include  
#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  
#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  
#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  
#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  
#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;
}

你可能感兴趣的:(算法,算法,c++,数据结构,c语言,人工智能,机器学习,maven)