CF446B DZY Loves Modification 【思维/优先队列】By cellur925
题目大意:给一个 \(n*m\) 的矩阵,并进行 \(k\) 次操作,每次操作将矩阵的一行或一列的所有元素的值减 \(p\) ,得到的分数为这次修改之前这一列/一行的元素和,求分数最大值。
我开始的意识流想法是用一个优先队列维护,先把所有元素插入,然后\(k\)次每次取出堆顶,减去乘\(p\)的什么东西,再塞回队中。但是...行与列是会互相影响的鸭,那么怎么搞呢?然后就不会了hh。
正解是努力打破了行与列之间的相互影响,把行与列分开计算。我们考虑行与列是怎么互相影响的:选择\(i\)个行,\(j\)个列,那么这里\(j=k-i\)。显然会产生\(i\)*\((k-i)\)个交点,我们需要另减去这些交点的贡献。
我们分别处理出行&列后,枚举最终的\(i\)就好了:)
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
int n,m,k,p;
int f[2018][2018];
ll hang[2000],lie[2000],hang_cnt[1000900],lie_cnt[1000900];
ll ans=-1e18;
priority_queue<ll>q;
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("%d",&f[i][j]);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
lie[j]+=f[i][j],hang[i]+=f[i][j];
for(int i=1;i<=m;i++) q.push(lie[i]);
for(int i=1;i<=k;i++)
{
ll tmp=q.top();q.pop();
lie_cnt[i]=lie_cnt[i-1]+tmp;
tmp-=1ll*n*p;
q.push(tmp);
}
while(!q.empty()) q.pop();
for(int i=1;i<=n;i++) q.push(hang[i]);
for(int i=1;i<=k;i++)
{
ll tmp=q.top();q.pop();
hang_cnt[i]=hang_cnt[i-1]+tmp;
tmp-=1ll*m*p;
q.push(tmp);
}
for(int i=0;i<=k;i++)
ans=max(ans,hang_cnt[i]+lie_cnt[k-i]-1ll*i*(k-i)*p);
printf("%lld\n",ans);
return 0;
}
注意开始数组开小了!\(k\)是\(1e5\)级别的。到现在还犯这种智障错误。
CF446B DZY Loves Modification 【思维/优先队列】By cellur925的更多相关文章
- 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 Round #FF (Div. 2) D. DZY Loves Modification 贪心+优先队列
链接:http://codeforces.com/problemset/problem/447/D 题意:一个n*m的矩阵.能够进行k次操作,每次操作室对某一行或某一列的的数都减p,获得的得分是这一行 ...
- 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 ...
- 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次操 ...
- B. DZY Loves Modification
B. DZY Loves Modification time limit per test 2 seconds memory limit per test 256 megabytes input st ...
随机推荐
- Struts表单重复提交
- office web apps 整合到自己项目中(wopi实现在线预览编辑)
借助office web apps实现在线预览和在线编辑 我所有的代码都是用go语言编写,你可以直接编译后使用,不用再有其他的操作. 最近项目实在太忙,这几天才有时间,这次是重头戏,要好好琢磨一下怎么 ...
- 常用git命令和工具
0. ln -s src_dir //一个参数即可在当前目录下生成一个软链接 1.git command --clone/push a branch git clone <url ...
- hibernate入门(-)
1.struts2的支持 在web.xml中配置struts2的支持 <?xml version="1.0" encoding="UTF-8"?> ...
- mongodb压缩——snappy、zlib块压缩,btree索引前缀压缩
MongoDB 3.0 WiredTiger Compression and Performance One of the most exciting developments over the li ...
- HDU3440 House Man (差分约束)
In Fuzhou, there is a crazy super man. He can’t fly, but he could jump from housetop to housetop. To ...
- Array 对象
Array的对象用于在单个的变量中存储多个值. constructor 返回对创建此对象的数组函数的引用. demo: let arr=[]; arr.constructor==Array let ...
- vuex本地存储
vuex与localstorage 区别:vuex数据存储的内存,localstorage的数据存储在本地 应用场景:vuex用于组件之间的传值,localstorage用于不同页面之间的传值 永久性 ...
- Coding 两步认证技术介绍
什么是两步认证 在介绍两步认证之前,首先来看下目前主流的几种认证方式. 上图中的认证方式大体上可以分为三大类 1.You know : 比如密码,这种只有我们知道的 2.You are : 比如指纹, ...
- 2.8-2.10 HBase集成MapReduce
一.HBase集成MapReduce 1.查看HBase集成MapReduce需要的jar包 [root@hadoop-senior hbase-0.98.6-hadoop2]# bin/hbase ...