1625 夹克爷发红包
- 1 秒
- 131,072 KB
- 20 分
- 3 级题
在公司年会上,做为互联网巨头51nod掌门人的夹克老爷当然不会放过任何发红包的机会。
现场有n排m列观众,夹克老爷会为每一名观众送出普通现金红包,每个红包内金额随机。
接下来,夹克老爷又送出最多k组高级红包,每组高级红包会同时给一排或一列的人派发 ,每个高级红包的金额皆为x。
派发高级红包时,普通红包将会强制收回。同时,每个人只能得到一个高级红包。(好小气!)
现在求一种派发高级红包的策略,使得现场观众获得的红包总金额最大。
收起
输入
第一行为n, m, x, k四个整数。 1 <= n <= 10, 1 <= m <= 200 1 <= x <= 10^9,0 <= k <= n + m 接下来为一个n * m的矩阵,代表每个观众获得的普通红包的金额。普通红包的金额取值范围为1 <= y <= 10^9
输出
输出一个整数,代表现场观众能获得的最大红包总金额
输入样例
3 4 1 5 10 5 7 2 10 5 10 8 3 9 5 4
输出样例
78
题解:先确定枚举那些行(二进制子集),再确定列(此时要用贪心),注:把相互影响的去掉就行。
#include<set>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<bitset>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define eps (1e-8)
#define MAX 0x3f3f3f3f
#define u_max 1844674407370955161
#define l_max 9223372036854775807
#define i_max 2147483647
#define re register
#define pushup() tree[rt]=max(tree[rt<<1],tree[rt<<1|1])
#define nth(k,n) nth_element(a,a+k,a+n); // 将 第K大的放在k位
#define ko() for(int i=2;i<=n;i++) s=(s+k)%i // 约瑟夫
#define ok() v.erase(unique(v.begin(),v.end()),v.end()) // 排序,离散化
using namespace std;
inline int read(){
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' & c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
typedef long long ll;
const double pi = atan(1.)*4.;
const int M=1e3+5;
const int N=5e5+5;
ll a[15][250];
ll x[15],y[250],yy[250],f[250];
bool cmp(ll aa,ll bb){
return aa>bb;
}
int main(){
int n,m,k; ll g;
scanf("%d %d %lld %d",&n,&m,&g,&k);
ll ans=0;
for(int i=1;i<=n;i++){
ll sum=0;
for(int j=1;j<=m;j++){
scanf("%lld",&a[i][j]);
sum+=a[i][j];
ans+=a[i][j];
}
x[i]=sum;
}
for(int i=1;i<=m;i++){
ll sum=0;
for(int j=1;j<=n;j++){
sum+=a[j][i];
}
y[i]=sum;
}
ll cut=-l_max; // 额外的增加
for(int i=0;i<(1<<n);i++){
for(int j=1;j<=m;j++) yy[j]=y[j];
int num=0;
ll d=0;
for(int j=0;j<n;j++){
if(i&(1<<j)){
for(int h=1;h<=m;h++){
yy[h]-=a[j+1][h];
}
num++;
d+=m*g-x[j+1];
}
}
if(num>k) break;
else if(num==k){
cut=max(d,cut);
}
else{
int b=n-num;
for(int j=1;j<=m;j++){
f[j]=b*g-yy[j];
}
sort(f+1,f+m+1,cmp);
for(int j=1;j<=min(m,(k-num));j++){
if(f[j]<=0) break;
d+=f[j];
}
cut=max(cut,d);
}
}
printf("%lld\n",max(ans,ans+cut));
return 0;
}