D. 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.

Sample test(s)
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

文章大意是给你一个n * m的矩阵,你可以进行k次操作.

操作1:把一行的每个元素都减去p,你可以获得该行所有元素和(操作之前)的pleasure

操作2:把一列的每个元素都减去p,你可以获得该列所有元素和(操作之前)的pleasure

问你最大可以获得的pleasure是多少

思路:枚举操作1所进行的次数,每次取能获得最大的行,用优先队列维护,要注意预处理,否则会超时

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
typedef long long LL;
using namespace std;
int A[1005][1005];
LL R[1005],C[1005];
LL s1[1000005],s2[1000005];
void get_C(int n,int i,int (*A) [1005])
{LL s=0;for(int j=1;j<=n;j++)s+=A[j][i];C[i]=s;
}
void get_R(int m,int i,int (*A)[1005])
{LL s=0;for(int j=1;j<=m;j++)s+=A[i][j];R[i]=s;
}
int main()
{int n,m,k,p;while(scanf("%d%d%d%d",&n,&m,&k,&p)==4){LL s=0;s1[0]=s2[0]=0;for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)scanf("%d",&A[i][j]);LL sum=-1000000000000009;priority_queue<LL>q1;priority_queue<LL>q2;for(int i=1;i<=n;i++){get_R(m,i,A);q1.push(R[i]);}for(int i=1;i<=m;i++){get_C(n,i,A);q2.push(C[i]);}for(int j=1;j<=k;j++){LL temp=q1.top();q1.pop();s1[j]=s1[j-1]+temp;temp-=p*m;q1.push(temp);}for(int j=1;j<=k;j++){LL temp=q2.top();q2.pop();s2[j]=s2[j-1]+temp;temp-=p*n;q2.push(temp);}for(int i=0;i<=k;i++){s=s1[i]+s2[k-i];sum=max(sum,s-LL(i)*p*(k-i));}printf("%I64d\n",sum);}return 0;
}

D. 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. DZY Loves Modification

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

  3. 【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 ...

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

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

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

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

  6. Codeforces 447C - DZY Loves Sequences

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

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

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

  8. CF A. DZY Loves Hash

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

  9. 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. AI工程的实践者:普元积极将场景落地,为企业提供智能化解决方案
  2. 深度学习时间序列预测:卷积神经网络(CNN)算法构建单变量时间序列预测模型预测空气质量(PM2.5)+代码实战
  3. SQL Server 移动数据库
  4. Pixhawk之姿态控制篇
  5. 安卓--L2T虚拟连接
  6. Git学习笔记01--初始化设置
  7. linux 内核学习线索初步
  8. 【Qt串口调试助手】1.1 - Qt5编写串口调试助手,Qt串口编程
  9. 读Zepto源码之Ajax模块
  10. 科学计算机弧度怎么读,角度怎么换算(角度换算弧度计算器)
  11. Python之txt数据导入
  12. php css下划线,css怎么去掉下划线
  13. walking机器人仿真教程-应用-多点导航结合睡眠功能实现智能取药
  14. 通过ESP8266手机或电脑浏览器网页能控制远程任意组任意路继电器开关并收发单片机指令 测试OK
  15. 什么是中台,为什么要中台?一篇文章带你了解中台的概念!
  16. easyexcel 简介、中文文档、中英对照文档 下载
  17. node.js学习总结:node.js的内置模块,模块化,npm与包 express,前后端身份认证 JWT认证机制
  18. VsCode:设置前进和后退 (返回上一个浏览位置/下一个浏览位置)快捷键
  19. 百度凤巢新版结构导图
  20. php采集百度top,PHP教程之采集百度音乐程序

热门文章

  1. sql服务器图标在哪个文件夹,mysql安装图标在文件夹哪?
  2. 机器学习系列文章——算法的实现(knn,朴素贝叶斯,决策树,模型评估)
  3. 微信支付零花钱刷屏了!5万额度,能花又能借
  4. 将PC网站转化为手机自适应网页或者自己制作手机自适应网页其实很简单,可以利用meta标签声明。
  5. 电脑屏幕监控,员工行为监控,上网行为监控解决方案
  6. 平摊分析(Amortized Analysis)-- Potential Method
  7. 一个关于android旋转屏幕界面的方法
  8. 脑波艺术 · 脑机、数字疗法、人工智能与看不见的“病人”
  9. java maven部署干货
  10. 网龙暑期训练营第二周:Unity介绍、调试以及小游戏demo开发