题目
题目描述
As we know, DZY loves playing games. One day DZY decided to play with a n×m n×m matrix. To be more precise, he decided to modify the matrix with exactly k k operations.
Each modification is one of the following:
Pick some row of the matrix and decrease each element of the row by p 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 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 k modifications? Please, help him to calculate this value.
输入输出格式
输入格式:
The first line contains four space-separated integers n,m,k n,m,k and p p $ (1<=n,m<=10^{3}; 1<=k<=10^{6}; 1<=p<=100) $ .
Then n n lines follow. Each of them contains m m integers representing $ a_{ij} (1<=a_{ij}<=10^{3}) $ — the elements of the current row of the matrix.
输出格式:
Output a single integer — the maximum possible total pleasure value DZY could get.
输入输出样例
输入样例#1: 复制
2 2 2 2
1 3
2 4
输出样例#1: 复制
11
输入样例#2: 复制
2 2 5 2
1 3
2 4
输出样例#2: 复制
11
说明
For the first sample test, we can modify: column 2, row 2. After that the matrix becomes:
1 1
0 0
For the second sample test, we can modify: column 2, row 2, row 1, column 1, column 2. After that the matrix becomes:
-3 -3
-2 -2
思路
这道题的主要困难在于,行和列之间是相互影响的
但一旦我们知道行操作的次数,列操作的次数也就知道了, 它们相互的影响也可以在常数时间内求得。
我们怎么知道呢?我们不知道,可以枚举
但直接枚举显然是不行的
因此,我们可以做个预处理,求出两个数组,再扫一遍,取个MAX
但这次一定要注意,long long! long long!
代码
#include <bits/stdc++.h>
#define N 1005
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef long long ll;
ll n,m,k,p,a[N][N],res=-INF,sum_r[N],sum_c[N],r[N*N],c[N*N];
priority_queue <ll> rq,cq;
void Init()
{
scanf("%I64d%I64d%I64d%I64d",&n,&m,&k,&p);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%I64d",&a[i][j]);
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
sum_r[i]+=a[i][j];
}
rq.push(sum_r[i]);
}
for(int j=1;j<=m;j++)
{
for(int i=1;i<=n;i++)
{
sum_c[j]+=a[i][j];
}
cq.push(sum_c[j]);
}
}
void Solve()
{
for(int i=1;i<=k;i++)
{
ll cc=cq.top();
cq.pop();
c[i]=c[i-1]+cc;
cc-=n*p;
cq.push(cc);
}
for(int i=1;i<=k;i++)
{
ll rr=rq.top();
rq.pop();
r[i]=r[i-1]+rr;
rr-=m*p;
rq.push(rr);
}
for(int i=0;i<=k;i++)
{
res=max(res,r[i]+c[k-i]-p*i*(k-i));
}
}
void Print()
{
printf("%I64d\n",res);
}
int main()
{
Init();
Solve();
Print();
return 0;
}