【悬关】wa 81 求调
查看原帖
【悬关】wa 81 求调
374769
Epi4any楼主2024/7/16 11:45
#include<bits/stdc++.h>
using namespace std;
const int INF=1e9+7;
const int maxn=105;
int n,m,mp[maxn][maxn],f[maxn][maxn];
pair<int,int> from[maxn][maxn],limit={1,1};
vector<int> ans;
int main() {
    cin>>n>>m;
    for(int i=1;i<=n;i++) {
        for(int j=1;j<=m;j++) {
            cin>>mp[i][j];
            f[i][j]=-INF;
        }
    }
    for(int i=1;i<=n;i++) {
        for(int j=i+1;j<=m;j++) {
            f[i][j]=max(f[i][j],max(f[i-1][j-1]+mp[i][j],f[i][j-1]));
            if(f[i-1][j-1]+mp[i][j]>f[i][j-1]) {
                from[i][j]={i-1,j-1};
            } else {
                from[i][j]={i,j-1};
            }
        }
    }
    pair<int,int> last={n,m};
    for(pair<int, int> i={n,m};i>=limit;i=from[i.first][i.second]) {
 //       cout<<i.first<<" "<<i.second<<endl;
        if(i.first!=last.first) {
            ans.push_back(last.second);
        }
        last=i;
    }
    ans.push_back(last.second);
    cout<<f[n][m]<<endl;
    for(int i=ans.size()-1;i>=0;i--) {
        cout<<ans[i]<<" ";
    }
    cout<<endl;
    return 0;
}
2024/7/16 11:45
加载中...