http://www.codeforces.com/contest/446/problem/B
As we know, DZY loves playing games. One day DZY decided to play with a n × m matrix. To be more precise, he decided to modify the matrix with exactly k operations.
Each modification is one of the following:
Pick some row of the matrix and decrease each element of the row by p. This operation brings to DZY the value of pleasure equal to the sum of elements of the row before the decreasing.
Pick some column of the matrix and decrease each element of the column by p. This operation brings to DZY the value of pleasure equal to the sum of elements of the column before the decreasing.
DZY wants to know: what is the largest total value of pleasure he could get after performing exactly k modifications? Please, help him to calculate this value.
The first line contains four space-separated integers n, m, k and p (1 ≤ n, m ≤ 103; 1 ≤ k ≤ 106; 1 ≤ p ≤ 100).
Then n lines follow. Each of them contains m integers representing aij (1 ≤ aij ≤ 103) — the elements of the current row of the matrix.
Output a single integer — the maximum possible total pleasure value DZY could get.
2 2 2 2
1 3
2 4
11
有一个n*m的矩阵,你需要操作k次,每次操作是选择一行或者一列,使得ans+=这一行或者这一列的和
然后再使得这一行或者这一列的数全部减去p
现在问你他操作k次之后,最多获得多少分数
假设你最后操作了k次,那么对于整体的答案,你需要减去i*(k-i)*p这么多
这样我们就把p给处理出来了
现在我们再把行和列都分开,然后用一个优先队列去处理就好了
然后这道题就结束了……
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
long long c[maxn],r[maxn],ll[maxn],rr[maxn];
long long a[1005][1005];
int n,m,k,p;
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("%lld",&a[i][j]);
ll[i]+=a[i][j];
rr[j]+=a[i][j];
}
}
priority_queue<long long>Q;
for(int i=1;i<=n;i++)Q.push(ll[i]);
for(int i=1;i<=k;i++)
{
int now = Q.top();Q.pop();
c[i]=c[i-1]+now;
now-=m*p;
Q.push(now);
}
while(!Q.empty())Q.pop();
for(int i=1;i<=m;i++)Q.push(rr[i]);
for(int i=1;i<=k;i++)
{
int now=Q.top();Q.pop();
r[i]=r[i-1]+now;
now-=n*p;
Q.push(now);
}
long long ans = -1LL<<60;
for(int i=0;i<=k;i++)
ans=max(ans,c[i]+r[k-i]-1ll*i*(k-i)*p);
cout<<ans<<endl;
}