其他分享
首页 > 其他分享> > CF446B DZY Loves Modification

CF446B DZY Loves Modification

作者:互联网

https://www.luogu.com.cn/problem/CF446B

行列分开考虑(用优先队列)搞出最大的前 n n n个
然后枚举选 k k k行,那么就是选 n − k n-k n−k列,把重复计算的剪掉再取max即可
代码不难实现

code:

#include<bits/stdc++.h>
#define N 1050
#define ll long long
using namespace std;
int n, m, k, p, a[N][N];
ll f[N * N], g[N * N];
priority_queue<ll> q;
int main() {
    scanf("%d%d%d%d", &n, &m, &k, &p);
    for(int i = 1; i <= n; i ++)
        for(int j = 1; j <= m; j ++) scanf("%d", &a[i][j]);
    for(int i = 1; i <= n; i ++) {
        int s = 0;
        for(int j = 1; j <= m; j ++) s += a[i][j];
        q.push(s);
    }
    for(int i = 1; i <= k; i ++) {
        int val = q.top();
        f[i] = f[i - 1] + val;
        q.pop(); q.push(val - m * p);
    }
    while(q.size()) q.pop();
    for(int i = 1; i <= m; i ++) {
        int s = 0;
        for(int j = 1; j <= n; j ++) s += a[j][i];
        q.push(s);
    }
    for(int i = 1; i <= k; i ++) {
        int val = q.top();
        g[i] = g[i - 1] + val;
        q.pop(); q.push(val - n * p);
    }
    ll ans = f[k];
    for(int i = 0; i <= k; i ++) ans = max(ans, f[i] + g[k - i] - 1ll * i * (k - i) * p);
    printf("%lld", ans);
    return 0;
}

标签:CF446B,val,int,ll,d%,pop,Modification,long,DZY
来源: https://blog.csdn.net/qq_38944163/article/details/120693249