思想:
巧妙的利用二进制数的特性,时间复杂度o(2^m(n*m));
代码:
#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
int n,m,k,ans;
int a[20][20],sum[20];
bool cmp(int x,int y){
return x>y;
}
int main(){
cin>>n>>m>>k;
k = min(k,min(n,m));
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
scanf("%d",&a[i][j]);
}
}
for(int i=0;i<1<<m;i++){
int temp = i,cnt = 0;
while(temp){
cnt++;
temp &=(temp-1);
}
if(cnt>k) continue;
for(int kk=1;kk<=n;kk++)
sum[kk] = 0;
int num = i,res = 0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(num&(1<<(j-1)))
res +=a[i][j];
else
sum[i]+=a[i][j];
}
}
sort(sum+1,sum+1+n,cmp);
for(int i=1;i<=k-cnt;i++)
res+=sum[i];
ans = max(ans,res);
}
cout<<ans<<endl;
return 0;
}