[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 × mmatrix. To be more precise, he decided to modify the matrix with exactly koperations.
Each modification is one of the following:
- 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.
- 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
2 2 2 21 32 4
11
2 2 5 21 32 4
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的更多相关文章
- Codeforces 447D - DZY Loves Modification
447D - DZY Loves Modification 思路:将行和列分开考虑.用优先队列,计算出行操作i次的幸福值r[i],再计算出列操作i次的幸福值c[i].然后将行取i次操作和列取k-i次操 ...
- 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 ...
- 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 ...
- 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. ...
- D. DZY Loves Modification
D. DZY Loves Modification time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- B. DZY Loves Modification
B. DZY Loves Modification time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- 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 ...
- Codeforces 444 C - DZY Loves Colors
C - DZY Loves Colors 思路: 分块,复杂度有点玄学,和普通分块不同的是在这个块被一次染色的时候暴力染整个块. 代码: #pragma GCC optimize(2) #pragma ...
- [CodeForces - 447E] E - DZY Loves Fibonacci Numbers
E DZY Loves Fibonacci Numbers In mathematical terms, the sequence Fn of Fibonacci numbers is define ...
随机推荐
- 原生js 当前时间 倒计时代码
源:https://www.oschina.net/code/snippet_2318153_54763 <!DOCTYPE html> <html> <head> ...
- 6、lvs使用进阶(02)
把web server服务和443服务绑定在一起之后呢? 假设一种场景,对web服务器来讲需要session保持.一个在线购物网站,在购物时,如果不结账,一般是http协议,当结账时,需要网站跳转,可 ...
- python学习 day06打卡
今天学习的主要内容是: 一,小数据池 代码块的概念 python程序是由代码块构成的,一个代码块的文本作为python程序执行的单元. 代码块:一个模块,一个函数,一个类,甚至每一个command命令 ...
- R语言通过loess去除某个变量对数据的影响--CNV分析
当我们想研究不同sample的某个变量A之间的差异时,往往会因为其它一些变量B对该变量的固有影响,而影响不同sample变量A的比较,这个时候需要对sample变量A进行标准化之后才能进行比较.标准化 ...
- 彻底弄懂JS事件委托的概念和作用
一.写在前头 接到某厂电话问什么是事件代理的时候,一开始说addEventListener,然后他说直接绑定新的元素不会报dom不存在的错误吗?然后我就混乱了,我印象中这个方法是可以绑定新节点的 ...
- java进程占用系统内存高,排查解决
转自:http://blog.51cto.com/chengxiaobai/2052530?cid=695076 故障:最近收到生产服务器的报警短信以及邮件,报警内容为:内存使用率高于70%. 使用t ...
- 手动添加jar包到maven仓库
引言: 虽然配置了maven以后可以通过索引的方式自动下载jar包到本地maven仓库,从而使项目中直接使用本地仓库里面的架包, 但是这一招并不是每一次都灵应,也有遇到了失败的时候,当遇到失败的时候, ...
- 如何将购物车信息存到Redis中?
存到Redis中,好处是速度快.毕竟写到硬盘需要更多的时间.加入购物车的功能,操作很频繁,可以通过Redis快速写入,移除,修改. 用什么方式呢? 传统的KEY,VALUE不太合适,每次增加修改,都要 ...
- isnull和sum的关系
这是我刚刚写存储过程的时候意识到的一个问题!!! 先问大家这样一个问题,print 100+null 等于多少? 在一组数据统计的过程中,只要使用到sum函数,就必须使用isnull函数包含起来,因 ...
- Android之StrictMode
1. StrictMode是什么? StrictMode is a developer tool which detects things you might be doing by accident ...