D - DZY Loves Modification

As we know, DZY loves playing games. One day DZY decided to play with a n × mmatrix. To be more precise, he decided to modify the matrix with exactly koperations.

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.

Example

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

题目的意思是,给你一个n*m的矩阵,每个位置有一个数.你可以进行k次操作,每次操作可以选择一行(列),然后把ans累加上这一行(列)的数的和,然后把这一行(列)的每一个数都减去一个固定的值.

要求最大化ans.

我们设R[i]为选择i个(次)行的最优解,C[i]为选择i个(次)列的最优解,由于选择的总次数为K,所以

ans=max(ans,R[i]+C[K-i]).然而这是不对的,因为在考虑R和C的时候是相对独立的,不受另一个因素的影响,而实际上,选择i个行,j个列,会产生i*(K-i)个交点,由于交点的存在,所以还要减去i*(K-i)*p(那个固定值).

所以ans=max(ans,R[i]+C[K-1]-i*(K-i)*p)(要注意数据类型)

那么我们现在来关注如何构造出R和C.由于贪心的想法,我们肯定挑目前和最大的行(列),那么我们可以用两个优先队列来维护一下不就好了.

 #include<cstdio>
 #include<cstring>
 #include<algorithm>
 #include<vector>
 #include<queue>
 using namespace std;
 ,maxK=;
 int sum_R[maxn],sum_C[maxn];
 long long R[maxK],C[maxK];
 int n,m,K,p,a[maxn][maxn];
 long long ans;
 struct node{
     int x,index;
     bool operator < (const node u) const {return x<u.x;}
 };
 priority_queue <node> Q_R,Q_C;
 int read(){
     ,f=; char ch=getchar();
     '){if (ch=='-') f=-f; ch=getchar();}
     +ch-',ch=getchar();
     return x*f;
 }
 int main(){
     n=read(),m=read(),K=read(),p=read(),ans=;
     ; i<=n; i++)
         ; j<=m; j++) a[i][j]=read();
     ; i<=n; i++){
         sum_R[i]=;
         ; j<=m; j++) sum_R[i]+=a[i][j];
     }
     ; i<=m; i++){
         sum_C[i]=;
         ; j<=n; j++) sum_C[i]+=a[j][i];
     }
     R[]=C[]=;
     ; i<=n; i++){node P; P.x=sum_R[i],P.index=i; Q_R.push(P);}
     ; i<=K; i++){
         node P=Q_R.top(); Q_R.pop();
         R[i]=R[i-]+P.x,P.x-=m*p,Q_R.push(P);
     }
     ; i<=m; i++){node P; P.x=sum_C[i],P.index=i; Q_C.push(P);}
     ; i<=K; i++){
         node P=Q_C.top(); Q_C.pop();
         C[i]=C[i-]+P.x,P.x-=n*p,Q_C.push(P);
     }
     ans=-;
     ; i<=K; i++) ans=max(ans,R[i]+C[K-i]-(long long)i*(K-i)*p);
     printf("%lld",ans);
     ;
 }

[CodeForces - 447D] D - DZY Loves Modification的更多相关文章

  1. Codeforces 447D - DZY Loves Modification

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

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

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

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

  5. D. DZY Loves Modification

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

  6. B. DZY Loves Modification

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

  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 444 C - DZY Loves Colors

    C - DZY Loves Colors 思路: 分块,复杂度有点玄学,和普通分块不同的是在这个块被一次染色的时候暴力染整个块. 代码: #pragma GCC optimize(2) #pragma ...

  9. [CodeForces - 447E] E - DZY Loves Fibonacci Numbers

    E  DZY Loves Fibonacci Numbers In mathematical terms, the sequence Fn of Fibonacci numbers is define ...

随机推荐

  1. 不要再混淆js的substring和substr了!(附js所有字符串方法)

    一.字符串操作方法 js中字符串方法操作有很多:concat.indexOf.... 这里只要介绍两种经常混淆的字符串截取方法:substring.substr 二.从例子入手 let str = ' ...

  2. 阿里云CentOS Linux服务器上搭建邮件服务器遇到的问题

    参考文章: 阿里云CentOS Linux服务器上用postfix搭建邮件服务器 Linux系统下邮件服务器的搭建(Postfix+Dovecot) 本来想自己搭建邮件服务器,但是看到一篇资料表示阿里 ...

  3. 《剑指offer》第六十题(n个骰子的点数)

    // 面试题60:n个骰子的点数 // 题目:把n个骰子扔在地上,所有骰子朝上一面的点数之和为s.输入n,打印出s // 的所有可能的值出现的概率. #include <iostream> ...

  4. 虹软人脸识别SDK的接入方法

    背景: 虹软的人脸识别还是不错的,在官方注册一个账号,成为开发者,下载SDK的jar包,在开发者中心,找一个demo就可以开始做了,安装里边的逻辑,先看理解代码,然后就可以控制代码,完成自己想要的功能 ...

  5. ACM-ICPC 2018 沈阳赛区网络预赛 J Ka Chang

    Ka Chang 思路: dfs序+树状数组+分块 先dfs处理好每个节点的时间戳 对于每一层,如果这一层的节点数小于sqrt(n),那么直接按照时间戳在树状数组上更新 如果这一层节点个数大于sqrt ...

  6. Python splinter 环境搭建

    今天无意间看到了splinter. Splinter是一个使用Python开发的开源Web应用测试工具.它可以帮你实现自动浏览站点和与其进行交互. Splinter对已有的自动化工具(如:Seleni ...

  7. Nginx自学笔记

    Nginx相关 标签(空格分隔): nginx 享学 安装部署 通过源代码的方式安装 使用 ./sbin/nginx #启动 ./sbin/nginx -t #检查是否有错 ./sbin/nginx ...

  8. Spring Boot入门第五天:使用JSP

    原文链接 1.在pom.xml文件中添加依赖: <!-- Servlet依赖 --> <dependency> <groupId>javax.servlet< ...

  9. 【文献04】无人驾驶高速AWID-AWIS车辆运动控制研究

    参考:阮久宏, 李贻斌, 荣学文, et al. 无人驾驶高速AWID-AWIS车辆运动控制研究[J]. 农业机械学报, 2009, 40(12):37-42. https://drive.wps.c ...

  10. 雷林鹏分享:XML - E4X

    XML - E4X E4X 向 JavaScript 添加了对 XML 的直接支持. E4X 实例 var employees= Tove 32 Jani 26 ; document.write(em ...