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

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 = 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

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;
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

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 = 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

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 = 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

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 = 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;
}
相关推荐
bachelores10 分钟前
数据结构-栈、队列和数组
数据结构·算法
xianwu54312 分钟前
反向代理模块开发,
linux·开发语言·网络·c++·git
大数据在线13 分钟前
进击的开放固件,突破AI时代多元算力管理难题
人工智能·浪潮信息·openbmc·ai算力·开放固件
AQin101228 分钟前
【Leetcode·中等·数组】59. 螺旋矩阵 II(spiral matrix ii)
算法·leetcode·矩阵·数组
好记性+烂笔头30 分钟前
hot100_73. 矩阵置零
数据结构·算法·矩阵
M_Lin_32 分钟前
文件IO
算法
sz66cm34 分钟前
数学基础 -- 拉普拉斯算子的原理与应用
深度学习·机器学习·计算机视觉·数学基础
Jeo_dmy35 分钟前
ChatGPT入门之文本情绪识别:先了解LSTM如何处理文字序列
人工智能·chatgpt·lstm
JINGWHALE141 分钟前
设计模式 行为型 状态模式(State Pattern)与 常见技术框架应用 解析
前端·人工智能·后端·设计模式·性能优化·系统架构·状态模式
robin_suli1 小时前
穷举vs暴搜vs深搜vs回溯vs剪枝系列一>字母大小写全排列
算法·剪枝·深度优先遍历·回溯