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 21 32 4

output
11

input
2 2 5 21 32 4

output
11

Note

For the first sample test, we can modify: column 2, row 2. After that the matrix becomes:

1 10 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+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("%I64d",&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;
}

 

转载于:https://www.cnblogs.com/huangdalaofighting/p/7390442.html

B. DZY Loves Modification相关推荐

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

  2. D. DZY Loves Modification

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

  3. DZY Loves Modification

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82662#problem/A 题目:DZY Loves Modification ...

  4. 【CF446B】 DZY Loves Modification

    题目 题目描述 As we know, DZY loves playing games. One day DZY decided to play with a n×m n×m matrix. To b ...

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

    链接:http://codeforces.com/problemset/problem/447/D 题意:一个n*m的矩阵.能够进行k次操作,每次操作室对某一行或某一列的的数都减p,获得的得分是这一行 ...

  6. Codeforces Round #FF 446 C. DZY Loves Fibonacci Numbers

    參考:http://www.cnblogs.com/chanme/p/3843859.html 然后我看到在别人的AC的方法里还有这么一种神方法,他预先设定了一个阈值K,当当前的更新操作数j<K ...

  7. Codeforces 447C - DZY Loves Sequences

    447C - DZY Loves Sequences 思路:dp 代码: #include<bits/stdc++.h> using namespace std; #define ll l ...

  8. Codeforces 446C —— DZY Loves Fibonacci Numbers(线段树)

    题目:DZY Loves Fibonacci Numbers 题意比較简单,不解释了. 尽管官方的题解也是用线段树,但还利用了二次剩余. 可是我没有想到二次剩余,然后写了个感觉非常复杂度的线段树,还是 ...

  9. CF A. DZY Loves Hash

    A. DZY Loves Hash time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  10. BZOJ 3309 DZY Loves Math

    3309: DZY Loves Math Description 对于正整数n,定义f(n)为n所含质因子的最大幂指数.例如f(1960)=f(2^3 * 5^1 * 7^2)=3, f(10007) ...

最新文章

  1. Javascript中函数提升和变量提升
  2. 使用eclipse生成javadoc的方法
  3. LeetCode 01两数之和02两数相加
  4. LINUX下简单制作QCOW2镜像
  5. 计算机课堂教育叙事,《我与电脑交朋友》教学反思
  6. 算法笔记_面试题_14. strStr 长字符串中查找短字符串
  7. 使用nginx的ngx_upstream_jdomain模块实现k8s容器的负载均衡
  8. mysql definer super_技术分享 | 改写 mysqldump 解决 DEFINER 问题
  9. 2008年全国计算机软考程序员考试大纲
  10. win10搜索框没反应或者搜索太慢,看这篇就够了
  11. 摄影基础知识(光圈、快门、感光度等)
  12. 跨域 CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR
  13. 众海世纪影业:五一档19部影片“扎堆”,能否再次掀起观影热潮?
  14. C1——primitives添加gltf并实现旋转、缩放、平移
  15. 雅可比矩阵:“Jacobian“矩阵
  16. c语言 程序竞赛题,C语言程序设计竞赛题目(学生使用).doc
  17. 大数据重新定义‘餐饮行业增长黑客’/怎么用数据驱动餐饮行业到店营销
  18. php ldap目录协议,PHP 通过LDAP协议,操作Windows Active Directory
  19. 易语言常用WINdows API分类查询
  20. 【牛客网】 小白月赛16 J题 小雨坐地铁

热门文章

  1. WPF 微信 MVVM 【续】发送部分QQ表情
  2. [原]解决百度地图多个Marker和InfoWindow时总是打开最后一个InfoWindow的问题
  3. 每日一C(函数指针)
  4. 使用vue搭建项目(创建手脚架)
  5. Linux下在Android模拟器中使用SD 卡的操作步骤
  6. SpringBoot源码篇:Spring5内置tomcat实现code-based的web.xml实现
  7. Webform(分页、组合查询)
  8. WebGIS项目中利用mysql控制点库进行千万条数据坐标转换时的分表分区优化方案...
  9. JMeter设置集合点
  10. event.type 事件属性