51Nod-1625-夹克爷发红包(二进制枚举+贪心)

版权声明:菜鸡博客大佬随便转 https://blog.csdn.net/qq_40642465/article/details/83480391

正解是枚举n行的全部情况,然后针对每种情况对m列进行贪心,求最大值,最后取最大值里的最大值。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fuck(x) cout<<#x<<" "<<x<<endl;
ll mp1[15][205],mp2[15][205];
ll colu[205];
vector<ll>v;
bool cmp(const ll&a,const ll&b)
{
    return a<b;
}
int main()
{
    ll n,m,x,cnm,t,cnt=0;
    ll ans=-1,tmp;
    scanf("%lld %lld %lld %lld",&n,&m,&x,&cnm);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            scanf("%lld",&(mp1[i][j]));
    for(int k=0;k<(1<<n);k++)
    {
        cnt=tmp=0;
        for(int i=1;i<=n;i++)
        {
            if((k>>(i-1))&1)
            {
                cnt++;
                for(int j=1;j<=m;j++)
                    mp2[i][j]=x;
            }
            else
                for(int j=1;j<=m;j++)
                    mp2[i][j]=mp1[i][j];
        }
        if(cnt>cnm)
            continue;
        t=cnm-cnt;
        v.clear();
        for(int j=1;j<=m;j++)
        {
            for(int i=1;i<=n;i++)
            {
                if(i==1)
                    colu[j]=mp2[i][j];
                else
                    colu[j]+=mp2[i][j];
            }
            v.push_back(colu[j]);
        }
        sort(v.begin(),v.end(),cmp);
        for(int i=0;i<v.size();i++)
        {
            if(v[i]<n*x&&t>=1)
            {
                tmp+=n*x;
                t--;
            }
            else
                tmp+=v[i];
        }
        ans=max(ans,tmp);
    }
    printf("%lld\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40642465/article/details/83480391