题目传送门

题目大意:给一个 \(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的更多相关文章

  1. 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 ...

  2. Codeforces Round #FF/#255 D DZY Loves Modification --贪心+优先队列

    题意:给你一个矩阵,每次选某一行或者某一列,得到的价值为那一行或列的和,然后该行每个元素减去p.问连续取k次能得到的最大总价值为多少. 解法: 如果p=0,即永远不减数,那么最优肯定是取每行或每列那个 ...

  3. Codeforces Round #FF (Div. 2) D. DZY Loves Modification 贪心+优先队列

    链接:http://codeforces.com/problemset/problem/447/D 题意:一个n*m的矩阵.能够进行k次操作,每次操作室对某一行或某一列的的数都减p,获得的得分是这一行 ...

  4. 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 ...

  5. 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 ...

  6. D. DZY Loves Modification

    D. DZY Loves Modification time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  7. [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 ...

  8. Codeforces 447D - DZY Loves Modification

    447D - DZY Loves Modification 思路:将行和列分开考虑.用优先队列,计算出行操作i次的幸福值r[i],再计算出列操作i次的幸福值c[i].然后将行取i次操作和列取k-i次操 ...

  9. B. DZY Loves Modification

    B. DZY Loves Modification time limit per test 2 seconds memory limit per test 256 megabytes input st ...

随机推荐

  1. rule-based optimizer cost-based optimizer

    SQL processing uses the following main components to execute a SQL query: The Parser checks both syn ...

  2. java之折半查找

    //功能:二分查找import java.util.*; public class Demo1 { public static void main(String[] args) { int arr[] ...

  3. android开发基础知识了解

    JDK下载:www.oracle.com; SDK下载:www.android.developer.com; eclipse下载:www.eclipse.org;

  4. Sql Server 2016 创建内存数据库

    官方教程:https://docs.microsoft.com/en-us/sql/relational-databases/in-memory-oltp/survey-of-initial-area ...

  5. Multi-lingual Support

    Multi-lingual Support One problem with dealing with non-Latin characters programmatically is that, f ...

  6. DNS常见攻击与防范

    DNS常见攻击与防范 转自:http://www.williamlong.info/archives/3813.html 日期:2015-7-10 随着网络的逐步普及,网络安全已成为INTERNET路 ...

  7. Docker的远程访问

    $docker : info (10.211.55是另一台服务器的地址) 频繁访问远程的docker服务器使用-H选项很麻烦,使用环境变量DOCKER_HOST, $export DOCKER_HOS ...

  8. Vue数据双向绑定探究

    前面的啰嗦话,写一点吧,或许就有点用呢 使用过vue的小伙伴都会感觉,哇,这个框架对开发者这么友好,简直都要笑出声了. 确实,使用过vue的框架做开发的人都会感觉到,以前写一大堆操作dom,bom的东 ...

  9. ffmpeg 的一些学习网站

    http://blog.csdn.net/leixiaohua1020/article/category/1360795 雷霄骅(leixiaohua1020)的专栏 http://dranger.c ...

  10. C# GDI生成清晰【高质量】图片

    对于GDI+,在正常的操作,Bitmap,Graphcis,DrawImage或者DrawString ,生成图片的话,会产生很多杂点,或者是图片质量不稳定.尤其是在读取图片后,生成缩略图之后,文件会 ...