行列单独操作+优先队列
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.
input
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.
outputOutput a single integer — the maximum possible total pleasure value DZY could get.
这道题的大意就是给你一个nXm矩阵,k 次操作,每次可以单独操作一行或者一列,每次操作后的得到所操作行或者列的值
,然后行或者列上的每一个值减去p;在k次操作之后的得到的最大值:
思路:
我们先可以对矩阵的行,列分开考虑,求单独操作i次行,i次列所得到的做大值:这里我们可以用优先队列来优化;
然后我们用贪心的思想,枚举操作i次行(k-i)次列的值
row[i]+culm{k-i}-i*(k-i)*p;
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<iostream>
#include<math.h>
using namespace std;
#define mx 1003
#define mm 1000007
#define LL long long //数据会超范围
LL a[mx][mx];
LL n,m,p,k;
LL row[mm],cul[mm];
int main()
{
LL i,j;
LL n;
while(scanf("%I64d %I64d %I64d %I64d",&n,&m,&k,&p)!=EOF)
{
memset(row,0,sizeof(row));
memset(cul,0,sizeof(cul));
priority_queue<int> r,c;
LL sum=0;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%I64d",&a[i][j]);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
sum+=a[i][j];
r.push(sum);
sum=0;
}
sum=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
sum+=a[j][i];
c.push(sum);
sum=0;
}
LL tt;
for(i=1;i<=k;i++)
{
tt=r.top();
r.pop();
row[i]=tt+row[i-1];
tt-=m*p;
r.push(tt);
}
for(i=1;i<=k;i++)
{
tt=c.top();
c.pop();
cul[i]=tt+cul[i-1];
tt-=n*p;
c.push(tt);
}
LL maxx=row[k];///这里要注意,我就死在这里n次,主要是数据范围很大,用-0x3fffffff的话会wrong,
也可以用-0x3fffffffffffffff;
for(i=0;i<=k;i++)
{
sum=row[i]+cul[k-i]-i*p*(k-i);
maxx=max(sum,maxx);
}
printf("%I64d\n",maxx);
}
}