玄关求条
查看原帖
玄关求条
1128671
lyc_qwq楼主2025/1/18 10:00
#include<bits/stdc++.h>

using namespace std;

const int N = 20;
int n , m;
int a[12][N];
int f[12][N];
int maxn;
int q;
int write(int i , int j)
{
	if(i == 0) return 0;
	for(int s = 0;s <= j; ++ s)
	{
		if(maxn == f[i - 1][s] + a[i][i - s])
		{
			maxn = f[i - 1][s];
			write(i - 1 , s);
			q ++;
			if(q == n) cout << i << " " << j - s;
			else cout << i << " " << j - s << '\n';
			break;
		}
	}
}

int main()
{
	cin >> n >> m;
	for(int i = 1;i <= n; ++ i)
		for(int j = 1;j <= m; ++ j)
			cin >> 	a[i][j];
	for(int i = 1;i <= n; ++ i)
	{
		for(int j = 1;j <= m; ++ j)
		{
			maxn = 0;
			for(int k = 0;k <= j; ++ k)
				if(f[i - 1][k] + a[i][j - k] > maxn)
					maxn = f[i - 1][k] + a[i][j - k];
			f[i][j] = maxn;
		}
	}
	cout << f[n][m] << '\n';
	write(n , m);
	return 0;
}

开O2全RE,不开O2全WA,but阳历过了

求大佬们帮助

2025/1/18 10:00
加载中...