B. DZY Loves Modification
time limit per test 2 seconds
memory limit per test 256 megabytes
input standard input
output standard output

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:

  1. 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.
  2. 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.

Output

Output a single integer — the maximum possible total pleasure value DZY could get.

Examples
input
2 2 2 2
1 3
2 4
output
11
input
2 2 5 2
1 3
2 4
output
11
Note

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

题解:

首先上一个错误的想法,分别统计出每一行和每一列的和,然后用优先队列维护,每次取出最大值。

将所对应的行或列的每一个值-P,再更新一下答案。

这种做法为什么是错的呢,因为假设如果有一行和一列的和是相等的,那么我们并不知道要先走行还是先走列。

再来看正确的做法:

首先我们设最终选了 行 i 次,则列选了 k-i 次

那么假设我们先全部选行,然后选列,则每次选列时,要-= i*p

这样最后是 -= i*(k-i)*p

也就是所有行对列的影响

那我们先把这个 i*(k-i)*p 提出来,那么选行和选列就互不影响

就可以分别考虑行和列

对于只取行的情况:

预处理出选0次 1次······k次行的最大值 H[i]

即优先队列跑一次即可

同理对列处理, 得到 L[i] 表示取i次列, 0次行的最大值

然后 ans = max( H[i]+L[k-i] - i*(k-i)*p )

注意结果可能是很小的负数,ans = -inf ,inf要足够大

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+;
long long c[maxn],r[maxn],ll[maxn],rr[maxn];
long long a[][];
int n,m,k,p;
int main()
{
scanf("%d%d%d%d",&n,&m,&k,&p);
for(int i=; i<=n; i++)
{
for(int j=; j<=m; j++)
{
scanf("%I64d",&a[i][j]);
ll[i]+=a[i][j];
rr[j]+=a[i][j];
}
}
priority_queue<long long>Q;
for(int i=; i<=n; i++)Q.push(ll[i]);
for(int i=; i<=k; i++)
{
int now = Q.top();
Q.pop();
c[i]=c[i-]+now;
now-=m*p;
Q.push(now);
}
while(!Q.empty())Q.pop();
for(int i=; i<=m; i++)Q.push(rr[i]);
for(int i=; i<=k; i++)
{
int now=Q.top();
Q.pop();
r[i]=r[i-]+now;
now-=n*p;
Q.push(now);
}
long long ans = -1LL<<;
for(int i=; i<=k; i++)
ans=max(ans,c[i]+r[k-i]-1ll*i*(k-i)*p);
cout<<ans<<endl;
}

 

B. DZY Loves Modification的更多相关文章

  1. D. DZY Loves Modification

    D. DZY Loves Modification time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  2. Codeforces Round #FF (Div. 2) D. DZY Loves Modification 优先队列

    D. DZY Loves Modification time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  3. [CodeForces - 447D] D - DZY Loves Modification

    D - DZY Loves Modification As we know, DZY loves playing games. One day DZY decided to play with a n ...

  4. Codeforces 447D - DZY Loves Modification

    447D - DZY Loves Modification 思路:将行和列分开考虑.用优先队列,计算出行操作i次的幸福值r[i],再计算出列操作i次的幸福值c[i].然后将行取i次操作和列取k-i次操 ...

  5. Codeforces Round #FF (Div. 1) B. DZY Loves Modification 优先队列

    B. DZY Loves Modification 题目连接: http://www.codeforces.com/contest/446/problem/B Description As we kn ...

  6. Codeforces Round #FF (Div. 1) B. DZY Loves Modification

    枚举行取了多少次,如行取了i次,列就取了k-i次,假设行列单独贪心考虑然后相加,那么有i*(k-i)个交点是多出来的:dpr[i]+dpc[k-i]-i*(k-i)*p 枚举i取最大值.... B. ...

  7. CF446B DZY Loves Modification 优先队列

    As we know, DZY loves playing games. One day DZY decided to play with a n × m matrix. To be more pre ...

  8. Codeforces Round #FF/#255 D DZY Loves Modification --贪心+优先队列

    题意:给你一个矩阵,每次选某一行或者某一列,得到的价值为那一行或列的和,然后该行每个元素减去p.问连续取k次能得到的最大总价值为多少. 解法: 如果p=0,即永远不减数,那么最优肯定是取每行或每列那个 ...

  9. CodeForces 446B DZY Loves Modification

    题意: k次操作  每次选择一行或一列  得到所选数字的和  并将所选数字同一时候减去p  问最多得到多少 思路: 重点在消除行列间的相互影响 因为每选一行全部列所相应的和都会-p  那么假设选了i次 ...

随机推荐

  1. SpringBoot 版本升级后报错 Cannot instantiate interface org.springframework.context.ApplicationContextInitializer

    本篇博客纯粹讲我遇到这个问题的解决以及思考,如果你想知道解决方法,可以直接看正确解决方案部分.因为是前端写的,所以可能有些明显的内容很容易就看出来了. 首先:升级后更新其他依赖,以及Applicati ...

  2. python 动态添加属性及方法及“__slots__的作用”

    1.动态添加属性 class Person(object): def __init__(self, newName, newAge): self.name = newName self.age = n ...

  3. 1072 Gas Station (30)(30 分)

    A gas station has to be built at such a location that the minimum distance between the station and a ...

  4. poj1821 Fence(单调队列优化dp)

    地址 一排N个木板,M个工匠站在不同位置$S_i$,每个人可以粉刷覆盖他位置的.最长长度为$L_i$木板段,每刷一个有$P_i$报酬.同一木板只刷一次.求最大报酬. 根据每个人的位置dp,设$f[i] ...

  5. mysql数据库---编码格式基本操作

    1.查看数据库编码格式 mysql> show variables like 'character_set_database'; 2.查看数据表的编码格式 mysql> show crea ...

  6. scaleform中ActionScript和UnrealScript的交互

    转自:http://www.cnblogs.com/NEOCSL/p/4174134.html scaleform是制作UI的好工具: 1.他可以解放程序员用代码控制的UI效果,例如平移,旋转和缩放都 ...

  7. JS---猜数字(0-100)

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  8. ng2中的ng-content用法

    用途:1.ng-content用于在组件中嵌入内容 2.ng-content可以在组件中嵌入模板代码,方便定制可复用的组件 select属性支持css选择器,如"#id",&quo ...

  9. 如何使用Git命令将项目从github或者服务器上克隆下来

    在本地新建一个文件夹,作为本地仓库,如“demo”.单击右键git Bush here,打开git,输入命令: cd /c/Users/Administrator/Desktop/demo  然后按回 ...

  10. 你所不知道的html5与html中的那些事(四)——文本标签

    文章简介:       关于html5相信大家早已经耳熟能详,但是他真正的意义在具体的开发中会有什么作用呢?相对于html,他又有怎样的新的定义与新理念在里面呢?为什么一些专家认为html5完全完成后 ...