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. 【Mysql】key 、primary key 、unique key 与index区别

    参考:https://blog.csdn.net/nanamasuda/article/details/52543177 总的来说,primary key .unique key 这些key建立的同时 ...

  2. React Native原生模块向JS传递数据的几种方式(Android)

    一般情况可以分为三种方式: 1. 通过回调函数Callbacks的方式 2. 通过Promises的异步的方式 3. 通过发送事件的事件监听的方式. 参考文档:传送门

  3. SPA中,Node路由优先级高于React路由

    一.问题描述 在一场面试中,面试官问到了React和Node路由之间的关系. 现在SPA(单页面应用)的使用越来越广. Node(后台)和React(前端)都有自己的路由,当我页面访问一个URL的时候 ...

  4. 前端调用后端接口下载excel文件的几种方式

    今天有一个导出相应数据为excel表的需求.后端的接口返回一个数据流,一开始我用axios(ajax类库)调用接口,返回成功状态200,但是!但是浏览器没有自动下载excel表,当时觉得可能是ajax ...

  5. 能改变this各种情况下的总结,还有没有总结到的,请留言!!

    1.. 在函数参数中的,回调函数的this ,指向window 如: promise中的回调函数, 可以在方法外,转存this 2..构造函数中,this指向,实例对象  , 在全局中this是win ...

  6. leecode第一百二十二题(买卖股票的最佳时机II)

    class Solution { public: int maxProfit(vector<int>& prices) { int len=prices.size(); ) ; , ...

  7. 学习笔记23—window10 64位 python2.7 安装liblinear

    最近在使用pythin,因为要使用libsvm,所以到官网去下载libsvm.官网地址为libsvm(https://www.csie.ntu.edu.tw/~cjlin/libsvm/)结果下载下来 ...

  8. python Django 创建应用

    如图输入如下命令 python manage.py startapp apitest 添加应用到 autotest项目项目下 在settings.pyo 中加入“apitest”,如下图 创建视图 在 ...

  9. spring集成JMS访问ActiveMQ

    首先我们搭建一个spring-mvc项目,项目可以参考:spring-mvc 学习笔记 步骤: 在pom.xml中加上需要的包 修改web.xml,增加IOC容器 spring配置文件applicat ...

  10. (转)C# 单例模式

    文章1:  一.多线程不安全方式实现 public sealed class SingleInstance    {        private static SingleInstance inst ...