51NOD 1625 夹克爷发红包 枚举+贪心

14 篇文章 0 订阅
1625 夹克爷发红包
基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏  关注
在公司年会上,做为互联网巨头51nod掌门人的夹克老爷当然不会放过任何发红包的机会。

现场有n排m列观众,夹克老爷会为每一名观众送出普通现金红包,每个红包内金额随机。

接下来,夹克老爷又送出最多k组高级红包,每组高级红包会同时给一排或一列的人派发 ,每个高级红包的金额皆为x。

派发高级红包时,普通红包将会强制收回。同时,每个人只能得到一个高级红包。(好小气!)

现在求一种派发高级红包的策略,使得现场观众获得的红包总金额最大。
Input
第一行为n, m, x, k四个整数。

1 <= n <= 10, 1 <= m <= 200
1 <= x <= 10^90 <= k <= n + m

接下来为一个n * m的矩阵,代表每个观众获得的普通红包的金额。普通红包的金额取值范围为1 <= y <= 10^9
Output
输出一个整数,代表现场观众能获得的最大红包总金额
Input示例
3 4 1 5
10 5 7 2
10 5 10 8
3 9 5 4
Output示例
78
Wizmann (题目提供者)

不难发现 二维数组 贪心并不能保证正确
题目突破口在n<=10
如果枚举每行是否被发高级红包 最多也只是2^10=1024种情况
然后 就在剩下的一维里用贪心 取换成高级红包能加最多钱的k列
每次贪心需要 O(n*m + m*log(m))
总复杂度O(n*m * 2^n)

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
#include<queue>
#include<sstream>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>

const int N = 12;
const int M = 202;
int pre[N][M];
int now[N][M];
int n,m,x,k;

ll add[M];
ll greed(int k_){
    for(int c=0;c<m;++c){//每列换成高级红包 可以增加多少钱
        add[c]=0;
        for(int r=0;r<n;++r){
            add[c]+=x-now[r][c];
        }
    }
    sort(add,add+m,greater<ll>());
    ll ans=0;
    for(int i=0;i<k_;++i){
        if(add[i]>0){
            ans+=add[i];
        }
        else{
            break;
        }
    }
    return ans;
}

ll slove(){
    ll ans=0;
    for(int i=0,end=1<<n;i<end;++i){//枚举行的状态 i的第flag位为1 表示第flag行被发高级红包
        ll t=0;//发了use行高级红包 一共有多少钱
        int use=0,flag=1;//use 发了多少行

        for(int r=0;r<n;++r){
            bool tmp=i&flag;//这行有没有发高级红包
            use+=tmp;
            if(use>k){//超过k组了
                break;
            }
            for(int c=0;c<m;++c){
                now[r][c]=tmp?x:pre[r][c];
                t+=now[r][c];
            }
            flag<<=1;
        }

        if(use<=k){
            ans = max(ans,t+greed(min(k-use,m)));
        }
    }
    return ans;
}

int main()
{
    //freopen("/home/lu/Documents/r.txt","r",stdin);
    //freopen("/home/lu/Documents/w.txt","w",stdout);
    scanf("%d%d%d%d",&n,&m,&x,&k);
    for(int i=0;i<n;++i){
        for(int j=0;j<m;++j){
            scanf("%d",&pre[i][j]);
        }
    }
    printf("%lld\n",slove());
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值