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. GPT-3:被捧上天的流量巨星,却有重大缺陷,很危险...
  2. php 给图片增加背景平铺水印代码
  3. 赚钱的这些年(上)苦逼
  4. 用FDISK进行硬盘分区
  5. js 中的break continue return
  6. 10月碎碎念-谈谈『自我放弃』
  7. Java 9:对Process API的增强
  8. 多布局怎么搭建_关键词SEO优化怎么做?具体包括哪些方面?
  9. 华为在中国建立其全球最大的网络安全透明中心
  10. Softmax和softmax loss的理解
  11. 【云速建站】页面产品维护简述
  12. 计算机与艺术就业怎样,就业报告:这些艺术类好就业,这些难就业!
  13. 利用jquery 控制select 实例代码
  14. linux删除链接和连接对象,linux 链接的使用 创建和删除符号连接(软、硬链接)...
  15. 网络不通时自动重启网卡的脚本
  16. Android10照片地理位置,华为手机怎么让拍摄照片显示地理位置
  17. 最新小程序反编译的获取流程
  18. 机器人学笔记之——操作臂运动学:驱动器空间、关节空间和笛卡尔空间
  19. 打开Xmind提示The contiolrator Userslwangappication DatalXMindlconfiquration-cathy win32-R3.79.2019120523
  20. 数学运算符“异或”的妙用

热门文章

  1. 阿里分布式开放消息服务(ONS)原理与实践——笔记整理
  2. 如何用企微SCRM管理系统发掘老客户的新增长点?
  3. 用户名和计算机名命名规范
  4. 【C生万物】 指针篇 (进级) 上
  5. 了解网络攻击:类型、策略和技术
  6. 将大写字母转换为小写字母(将该字符串中的大写字母转换成小写字母,之后返回新的字符串。)
  7. 计算机专业简历文案,文案个人简历
  8. Flutter开发:遇到库冲突问题的解决方法
  9. 解决Error: Could not detect Mac OS X Version from sw_vers output: '10.14.3'
  10. 跨考生的噩梦!多所985/211大学计算机考研禁止跨考!