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次 ...
随机推荐
- noip模拟赛 #2
万年rk2 我写挂大家都挂但是有人比我挂的少 我好不容易卡一波常数然后有人ak ... T1.不想写,等会放链接 T2 给一个方阵,每个地方有一个权值,把它划成两块,不能往回拐弯,求两块极差较大的那个 ...
- Codefroces 762A k-th divisor 数论
Codeforces 762A 题目大意: 给定两个正整数n,k\((n \le 10^{15},k\leq10^9)\),求n的从小到大的第k个约数,无解输出-1 分析: 我们会自然而然地想到找出n ...
- JVM(一)虚拟机内存划分
Java内存区域 线程私有数据区域:虚拟机栈,本地方法栈,程序计数器 线程共享数据区域:方法区,堆 程序计数器:当前线程所执行的字节码的行号指示器,JVM通过这个字节码解释器改变计数器的值,以选择下一 ...
- AtCoder Grand Contest 014 D:Black and White Tree
题目传送门:https://agc014.contest.atcoder.jp/tasks/agc014_d 题目翻译 给你一棵树,每次任选一个点染色,先手染白色,后手染黑色.如果最后存在一个白色的点 ...
- poj3662Telephone Lines——二分+最短路
题目:http://poj.org/problem?id=3662 二分答案找出符合条件的最小长度: 假设了每个长度后,以这个为标准对每条边赋值,0为小于等于,1为大于,然后按这个值来跑最短路,在看看 ...
- hive 面试题
使用 Hive或者自定义 MR 实现如下逻辑 product_no lac_id moment start_time user_id county_id staytime city_id 134291 ...
- ELK安装配置简单使用
ELK是三款软件的总称,包括了elasticsearch.logstash.kibana,其实在生产使用中,我们还需要使用到其他的更多辅助软件来更好更合理的收集展示数据. Elasticsearch: ...
- Docker入门(五):Swarms
这个<Docker入门系列>文档,是根据Docker官网(https://docs.docker.com)的帮助文档大致翻译而成.主要是作为个人学习记录.有错误的地方,Robin欢迎大家指 ...
- 数据结构&算法 索引
最近在看牛客网的算法课,准备开个坑,根据自己的理解,把课程讲到的内容列一个大纲,明年找工作的时候用. 再加上看算法第四版的内容,整理一套明年找工作能够直接拿出来用的代码. 此篇博文作为索引.
- [hdu4311]Meeting point-1
题意:在整数坐标轴上找一个距离所有给定点距离最小的点. 解题关键:对x和y分别处理,前缀和预处理所有点到最小点的距离,每点的$sum$等于左边的贡献+右边的贡献,最后取$min$即可. 复杂度:$O( ...