27分求调
查看原帖
27分求调
1211899
Bobi2014楼主2024/10/14 17:07
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,m,bf[110][110],dp[110][110],ans = LONG_LONG_MIN,pre[110][110],endx,endy;
stack<int> st; 
int main(){
	cin >> n >> m;
	for(int i = 1;i <= n;i ++){
		for(int j = 1;j <= m;j ++){
			cin >> bf[i][j];
			dp[i][j] = LONG_LONG_MIN / 2;
		}
	}
	memset(dp,-127,sizeof(dp));
	for(int i = 0;i <= m;i ++){
		dp[0][i] = 0;
	}
	for(int i = 1;i <= n;i ++){
		for(int j = 1;j <= m;j ++){
			for(int k = i;k <= j;k ++){
				if(dp[i - 1][k - 1] + bf[i][k] > dp[i][j]){
					dp[i][j] = dp[i - 1][k - 1] + bf[i][k];
					pre[i][j] = k;
				}
			}
			if(dp[i][j] > ans){
				ans = dp[i][j];
				endx = i;
				endy = j;
			}
		} 
	}
	cout << ans << endl;
	while(true){
		if(pre[endx][endy] == 0){
			break;
		}
		st.push(pre[endx][endy]);
		endy = pre[endx][endy] - 1;
		endx --;
	}
	while(st.empty() == false){
		cout << st.top() << " ";
		st.pop();
	}
	return 0;
}

2024/10/14 17:07
加载中...