Codeforces Round #FF (Div. 1) B. DZY Loves Modification 优先队列
B. DZY Loves Modification
题目连接:
http://www.codeforces.com/contest/446/problem/B
Description
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.
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.
Sample Input
2 2 2 2
1 3
2 4
Sample Output
11
Hint
题意
有一个n*m的矩阵,你需要操作k次,每次操作是选择一行或者一列,使得ans+=这一行或者这一列的和
然后再使得这一行或者这一列的数全部减去p
现在问你他操作k次之后,最多获得多少分数
题解:
假设你最后操作了k次,那么对于整体的答案,你需要减去i*(k-i)*p这么多
这样我们就把p给处理出来了
现在我们再把行和列都分开,然后用一个优先队列去处理就好了
然后这道题就结束了……
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
long long c[maxn],r[maxn],ll[maxn],rr[maxn];
long long a[1005][1005];
int n,m,k,p;
int main()
{
scanf("%d%d%d%d",&n,&m,&k,&p);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%lld",&a[i][j]);
ll[i]+=a[i][j];
rr[j]+=a[i][j];
}
}
priority_queue<long long>Q;
for(int i=1;i<=n;i++)Q.push(ll[i]);
for(int i=1;i<=k;i++)
{
int now = Q.top();Q.pop();
c[i]=c[i-1]+now;
now-=m*p;
Q.push(now);
}
while(!Q.empty())Q.pop();
for(int i=1;i<=m;i++)Q.push(rr[i]);
for(int i=1;i<=k;i++)
{
int now=Q.top();Q.pop();
r[i]=r[i-1]+now;
now-=n*p;
Q.push(now);
}
long long ans = -1LL<<60;
for(int i=0;i<=k;i++)
ans=max(ans,c[i]+r[k-i]-1ll*i*(k-i)*p);
cout<<ans<<endl;
}
Codeforces Round #FF (Div. 1) B. DZY Loves Modification 优先队列的更多相关文章
- 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. ...
- Codeforces Round #FF (Div. 2) D. DZY Loves Modification 贪心+优先队列
链接:http://codeforces.com/problemset/problem/447/D 题意:一个n*m的矩阵.能够进行k次操作,每次操作室对某一行或某一列的的数都减p,获得的得分是这一行 ...
- DP Codeforces Round #FF (Div. 1) A. DZY Loves Sequences
题目传送门 /* DP:先用l,r数组记录前缀后缀上升长度,最大值会在三种情况中产生: 1. a[i-1] + 1 < a[i+1],可以改a[i],那么值为l[i-1] + r[i+1] + ...
- Codeforces Round #FF (Div. 1) A. DZY Loves Sequences 动态规划
A. DZY Loves Sequences 题目连接: http://www.codeforces.com/contest/446/problem/A Description DZY has a s ...
- Codeforces Round #FF (Div. 2):B. DZY Loves Strings
B. DZY Loves Strings time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #FF (Div. 1) A. DZY Loves Sequences
题目链接: http://www.codeforces.com/contest/446/problem/A 题解: dp1[x]表示以x结尾的最大严格升序连续串,dp2[x]表示以x开头的最大严格升序 ...
- Codeforces Round #FF (Div. 2)__E. DZY Loves Fibonacci Numbers (CF447) 线段树
http://codeforces.com/contest/447/problem/E 题意: 给定一个数组, m次操作, 1 l r 表示区间修改, 每次 a[i] + Fibonacci[i-l ...
- Codeforces Round #FF (Div. 2) A. DZY Loves Hash
DZY has a hash table with p buckets, numbered from 0 to p - 1. He wants to insert n numbers, in the ...
随机推荐
- 编译器是如何实现32位整型的常量整数除法优化的?[C/C++]
引子 在我之前的一篇文章[ ThoughtWorks代码挑战——FizzBuzzWhizz游戏 通用高速版(C/C++ & C#) ]里曾经提到过编译器在处理除数为常数的除法时,是有优化的,今 ...
- poj1067
题意:有两堆石子,两人轮流取,每次可以取一堆中的任意个,或两堆中取相同多个.谁先取光所有堆谁赢.问先手能否获胜. 分析:威佐夫博弈,如果是奇异态则先手输,否则先手赢.直接套用公式判断是否为奇异态,设第 ...
- 在Mac上搭建ReactNative开发环境
1.安装Homebrew, Mac系统的包管理器,用于安装NodeJS和一些其他必需的工具软件. /usr/bin/ruby -e "$(curl -fsSL https://raw.g ...
- python中mock的使用
什么是mock? mock在翻译过来有模拟的意思.这里要介绍的mock是辅助单元测试的一个模块.它允许您用模拟对象替换您的系统的部分,并对它们已使用的方式进行断言. 在Python2.x 中 mock ...
- Flask SQLAlchemy & model
Flask-SQLAlchemy Flask-SQLAlchemy库让flask更方便的使用SQLALchemy,是一个强大的关系形数据库框架,既可以使用orm方式操作数据库,也可以使用原始的SQL命 ...
- mysql sql语句中用括号处理or和and的运算顺序
需求,我要检索出 a =1 或者 b=1 并且 c = 0 或者 c=1 时候的结果 例子: select * from test where a = 1 or b = 1 and ( c = 0 o ...
- 一步一步学习IdentityServer3 (10)
在某些服务器环境下 identityserver3 会闹情绪, 比如在google浏览器下授权失败(陷入死循环) 查了很多资料好像然并卵 Microsoft.Owin.Security.Notific ...
- Bootstrap Paginator分页插件使用示例
最近做的asp.netMVC项目中需要对数据列表进行分类,这个本来就是基于bootstrap开发的后台,因此也就想着bootstrap是否有分页插件呢,或者说是基于jquery支持的分页功能,这样整体 ...
- 成功实施的APS项目故事分享---我们数据治理的心路历程
一.故事背景 A企业是易普优APS重要客户之一,是某行业的龙头企业:APS项目历时7个月顺利上线,十个月验收!通过易普优APS的顺利实施,建成了集团的精益计划管控运营平台,树立计划的权威与指挥棒作用, ...
- Kibana部署及配置(四)
一.Kibana安装 Kibana 是为 Elasticsearch 设计的开源分析和可视化平台.你可以使用 Kibana 来搜索,查看存储在 Elasticsearch 索引中的数据并与之交互.你可 ...