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. BZOJ_3672_ [Noi2014]购票_CDQ分治+斜率优化

    BZOJ_3672_ [Noi2014]购票_CDQ分治+斜率优化 Description  今年夏天,NOI在SZ市迎来了她30周岁的生日.来自全国 n 个城市的OIer们都会从各地出发,到SZ市参 ...

  2. poj1456Supermarket——并查集压缩查找

    题目:http://poj.org/problem?id=1456 排序+贪心,每次选利润最大的,放在可能的最靠后的日期卖出,利用并查集快速找到下一个符合的日期. 代码如下: #include< ...

  3. 开源LTE代码分析

    跟踪了一个在将开源组织-OpenLTE(将4G通信网络LTE开源),现将自己梳理整理的一些文档Post出来,请有相同兴趣的朋友指点: 一,系统介绍 OpenLTE是一位Mot的工程师在12年发起的一个 ...

  4. maven学习九 关于maven一些參數

    一 maven profile:      不同的运行环境,比如开发环境.测试环境.生产环境,而我们的软件在不同的环境中,有的配置可能会不一样,比如数据源配置.日志文件配置.以及一些软件运行过程中的基 ...

  5. tomcat 开机自启方法

    1.把下面的代码保存为tomcat文件,并让它成为可执行文件 chmod 755 tomcat. #!/bin/bash # # /etc/rc.d/init.d/tomcat # init scri ...

  6. 塞尔达:旷野之息个人对比上古卷轴V:天际

    上古卷轴5是我之前玩过最优秀的作品.玩塞尔达的时候就有跟上古卷轴5比对,真的都是神作.两个游戏的自由度都是真的高. 主线剧情上,老滚5印象不深了,当时就知道战斗,只记住了开头砍头现场,还有奥杜因这个龙 ...

  7. WPF访问UserControl的自定义属性和事件

    要实现外部窗体能直接访问UserControl的属性必须把UserControl的属性定义为依赖属性: 一,在UserControl.cs中为TextBox控件的Text建立依赖属性,输入" ...

  8. C# 中的迭代器 yield关键字 提高性能和可读性

    展示一个例子 IList<string> FindBobs(IEnumerable<string> names) { var bobs = new List<string ...

  9. ACM-ICPC2018北京网络赛 Tomb Raider(暴力)

    题目2 : Tomb Raider 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Lara Croft, the fiercely independent daughte ...

  10. React Native环境搭建(iOS、Mac)

    http://reactnative.cn/docs/0.42/getting-started.html#content 1.安装Homebrew Homebrew, Mac系统的包管理器,用于安装N ...