ACM篇:CF 446B -- DZY Loves Modification

不會。
網上看的題解。

優先隊列+枚舉

#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
#define LL long long
#define max(A, B) ( ((A)>(B)) ? (A) : (B) )
using namespace std;
const int MAX = 1000;
const int K = 1000005;
LL row[MAX+24];
LL col[MAX+24]; 
LL dpr[K];
LL dpc[K];
int matrix[MAX+2][MAX+2];
priority_queue<LL>q;
int n;
int m;
int k;
int p;
void get_sum()
{
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
        {
            scanf("%d", &matrix[i][j]);
            row[i] += matrix[i][j];
            col[j] += matrix[i][j];
        }
}

void work(LL *arr,LL *sum, int sz, int minus)
{
    while(!q.empty()) 
        q.pop();
    for (int i = 1; i <= sz; i++) q.push(sum[i]);
    for (int i = 1; i <= k; i++)
    {
        arr[i] = arr[i-1] + q.top();
        q.push(q.top() - minus);
        q.pop();
    }
}
int main()
{
    LL ans;

    ans = -1LL<<60;

    scanf("%d%d%d%d", &n, &m, &k, &p);
    get_sum();

    work(dpr, row, n, m*p);
    work(dpc, col, m, n*p);

    for (int i = 0; i <= k; i++)
        ans = max(ans, dpr[i] + dpc[k-i] - 1LL*i*(k-i)*p);

    printf("%lld", ans);
    return 0;
}
發佈了58 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章