NC218551 网格(dp)

行列无关,因此对于行列分别求解

这题的题目有个错别字,有打成由了,我做的时候没理解求的啥 ,尴尬了

其他就是状态机dp,表示这个位置向左还是向右

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int N=2e5+10;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
int n,m;
int a[2000][2000];
int f[1010][2];
int cal(int x){
    int sum=x;
    while(x){
        sum+=(x%2);
        x/=2;
    }
    return sum;
}
int main(){
    ios::sync_with_stdio(false);
    cin>>n>>m;
    int i,j;
    for(i=1;i<=n;i++){
        for(j=1;j<=m;j++)
            cin>>a[i][j];
    }
    ll ans=0;
    for(i=1;i<=n;i++){
        f[0][1]=-1e9;
        for(j=1;j<=m;j++){
            f[j][0]=max(f[j-1][0],f[j-1][1]+cal(a[i][j-1]^a[i][j]));
            f[j][1]=max(f[j-1][0],f[j-1][1]);
        }
        ans+=max(f[m][0],f[m][1]);
        memset(f,0,sizeof f);
    }
    for(i=1;i<=m;i++){
        f[0][1]=-1e9;
        for(j=1;j<=n;j++){
            f[j][0]=max(f[j-1][0],f[j-1][1]+cal(a[j-1][i]^a[j][i]));
            f[j][1]=max(f[j-1][0],f[j-1][1]);
        }
        ans+=max(f[n][0],f[n][1]);
        memset(f,0,sizeof f);
    }
    cout<<ans<<endl;
    return 0;
}
View Code
没有人不辛苦,只有人不喊疼
原文地址:https://www.cnblogs.com/ctyakwf/p/14443586.html