B. DZY Loves Modification
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:
- 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.
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 a single integer — the maximum possible total pleasure value DZY could get.
2 2 2 2
1 3
2 4
11
2 2 5 2
1 3
2 4
11
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
题解:
首先上一个错误的想法,分别统计出每一行和每一列的和,然后用优先队列维护,每次取出最大值。
将所对应的行或列的每一个值-P,再更新一下答案。
这种做法为什么是错的呢,因为假设如果有一行和一列的和是相等的,那么我们并不知道要先走行还是先走列。
再来看正确的做法:
首先我们设最终选了 行 i 次,则列选了 k-i 次
那么假设我们先全部选行,然后选列,则每次选列时,要-= i*p
这样最后是 -= i*(k-i)*p
也就是所有行对列的影响
那我们先把这个 i*(k-i)*p 提出来,那么选行和选列就互不影响
就可以分别考虑行和列
对于只取行的情况:
预处理出选0次 1次······k次行的最大值 H[i]
即优先队列跑一次即可
同理对列处理, 得到 L[i] 表示取i次列, 0次行的最大值
然后 ans = max( H[i]+L[k-i] - i*(k-i)*p )
注意结果可能是很小的负数,ans = -inf ,inf要足够大
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+;
long long c[maxn],r[maxn],ll[maxn],rr[maxn];
long long a[][];
int n,m,k,p;
int main()
{
scanf("%d%d%d%d",&n,&m,&k,&p);
for(int i=; i<=n; i++)
{
for(int j=; j<=m; j++)
{
scanf("%I64d",&a[i][j]);
ll[i]+=a[i][j];
rr[j]+=a[i][j];
}
}
priority_queue<long long>Q;
for(int i=; i<=n; i++)Q.push(ll[i]);
for(int i=; i<=k; i++)
{
int now = Q.top();
Q.pop();
c[i]=c[i-]+now;
now-=m*p;
Q.push(now);
}
while(!Q.empty())Q.pop();
for(int i=; i<=m; i++)Q.push(rr[i]);
for(int i=; i<=k; i++)
{
int now=Q.top();
Q.pop();
r[i]=r[i-]+now;
now-=n*p;
Q.push(now);
}
long long ans = -1LL<<;
for(int i=; i<=k; i++)
ans=max(ans,c[i]+r[k-i]-1ll*i*(k-i)*p);
cout<<ans<<endl;
}
B. DZY Loves Modification的更多相关文章
- 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. 2) D. DZY Loves Modification 优先队列
D. DZY Loves Modification time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- [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 ...
- 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. 1) B. DZY Loves Modification
枚举行取了多少次,如行取了i次,列就取了k-i次,假设行列单独贪心考虑然后相加,那么有i*(k-i)个交点是多出来的:dpr[i]+dpc[k-i]-i*(k-i)*p 枚举i取最大值.... B. ...
- 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 Round #FF/#255 D DZY Loves Modification --贪心+优先队列
题意:给你一个矩阵,每次选某一行或者某一列,得到的价值为那一行或列的和,然后该行每个元素减去p.问连续取k次能得到的最大总价值为多少. 解法: 如果p=0,即永远不减数,那么最优肯定是取每行或每列那个 ...
- CodeForces 446B DZY Loves Modification
题意: k次操作 每次选择一行或一列 得到所选数字的和 并将所选数字同一时候减去p 问最多得到多少 思路: 重点在消除行列间的相互影响 因为每选一行全部列所相应的和都会-p 那么假设选了i次 ...
随机推荐
- html制作细线表格
关于这个细线表格的制作方法,百度一下可能就会有答案告诉你设置这几个值:给table设置border="0" cellspacing="1" bgcolor=&q ...
- BZOJ_2802_[Poi2012]Warehouse Store_堆+贪心
BZOJ_2802_[Poi2012]Warehouse Store_堆+贪心 Description 有一家专卖一种商品的店,考虑连续的n天. 第i天上午会进货Ai件商品,中午的时候会有顾客需要购买 ...
- 【C++ Primer 5th】Chapter 15
摘要: 1. 面向对象程序设计的核心思想是数据抽象.继承和动态绑定.数据抽象将类的接口和实现分离:继承定义相似的类型并对齐相似关系建模:动态绑定,在一定程度上忽略相似类型的区别,而以统一的方式使用它们 ...
- Java远程调用
一. 概念: RMI全称是Remote Method Invocation-远程方法调用,其威力就体现在它强大的开发分布式网络应用的能力上,是纯Java的网络分布式应用系统的核心解决方案之一.它支持 ...
- 将hive搭建到spark上
1. 首先搭建好spark和hive,参见相关文档 2. 在spark/conf下创建hive-site.xml <configuration> <property> < ...
- Jenkins安装配置简单使用
安装启动是十分简单的,直接去https://jenkins.io/download/下载对应的rpm包就好了,需要注意的是我们的机器上要提前有java环境,相对应要选择你java环境可以支持的jenk ...
- QT画矩形
第一次发QT的博文,本人对QT接触没多久,还在入门水平,大牛勿喷哈,之前因为C# (.net framework)做出来的绘制矩形的程序闪的太厉害了,现在用QT重做一个 先上效果图 代码贴全了 #i ...
- 利用JS函数制作时钟运行程序
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 二十五种网页加速方法和seo优化技巧
一.使用良好的结构 可扩展 HTML (XHTML) 具有许多优势,但是其缺点也很明显.XHTML 可能使您的页面更加符合标准,但是它大量使用标记(强制性的 <start> 和 <e ...
- nodejs安装使用express
1.用Express 应用程序生成器 express-generator 快速搭建express框架 1.1安装: npm install express-generator -g 1.2 生成目录: ...