https://ac.nowcoder.com/acm/contest/9986/E
題意:
有一個網格,每一個位置可以選上下左右四個方向的數,但是選了上就不能選下,選了左就不能選右。
同時如果兩個相鄰的位置,如果其中一個方向選了右,一個選了左,互相選到了對方,就對答案産生w(a1^a2)的貢獻
其中w(x):x+popcnt(x).
思路:其實可以發現一個位置選上下和左右是分割開的,可以分開統計出來。
于是行列分開處理,對于每一列先轉移出一個最優狀态,累加到答案。
然後再對每一行轉移出一個最優狀态,再累加到答案。
dp[i][j][0/1]:表示第i行第j個數選左邊/右邊的最大貢獻和。
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e3+1000;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar(); while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL a[maxn][maxn];
LL dp1[maxn][maxn][2],dp2[maxn][maxn][2];
LL get(LL x){
LL temp=x;LL sum=0;
while(x>0){
if(x&1) sum++;
x>>=1;
}
return temp+sum;
}
int main(void)
{
cin.tie(0);std::ios::sync_with_stdio(false);
LL n,m;cin>>n>>m;
for(LL i=1;i<=n;i++){
for(LL j=1;j<=m;j++){
cin>>a[i][j];
}
}
LL ans=0;
for(LL i=1;i<=n;i++){
for(LL j=2;j<=m;j++){
dp1[i][j][0]=max(dp1[i][j-1][1]+get(a[i][j-1]^a[i][j]),dp1[i][j-1][0]);
dp1[i][j][1]=max(dp1[i][j-1][1],dp1[i][j-1][0]);
}
ans+=max(dp1[i][m][0],dp1[i][m][1]);
}
for(LL j=1;j<=m;j++){
for(LL i=2;i<=n;i++){
dp2[i][j][0]=max(dp2[i-1][j][1]+get(a[i-1][j]^a[i][j]),dp2[i-1][j][0]);
dp2[i][j][1]=max(dp2[i-1][j][1],dp2[i-1][j][0]);
}
ans+=max(dp2[n][j][0],dp2[n][j][1]);
}
cout<<ans<<"\n";
return 0;
}