CodeForces 446B
DZY Loves Modification
2 seconds
256 megabytes
standard input
standard output
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.
Examples
2 2 2 2
1 3
2 4
11
2 2 5 2
1 3
2 4
11
Note
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
题意:
给定n行m列的矩阵 k次操作,一个常数p,ans = 0;对于每次操作,可以任选一行或一列, 则ans += 这行(列)的数字和,然后这行(列)上的每个数字都-=p
//2016.8.19
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define ll __int64
#define inf 999999999 using namespace std; ll rsum[], csum[], rres[], cres[];
priority_queue<ll> pq1;
priority_queue<ll> pq2; int main()
{
ll n, m, k, p;//被数据恶心到了,这行类型改成ll才过
ll tmp, val;
while(cin>>n>>m>>k>>p)
{
memset(rsum, , sizeof(rsum));
memset(csum, , sizeof(csum));
memset(rres, , sizeof(rres));
memset(cres, , sizeof(cres));
for(int i =; i < n; i++)
{
for(int j = ; j < m; j++)
{
scanf("%I64d", &val);
rsum[i] += val;//rsum[i]表示第i行之和
csum[j] += val;//csum[i]表示第i列之和
}
}
while(!pq1.empty())pq1.pop();
for(int i = ; i < n; i++)
{
pq1.push(rsum[i]);
}
for(int i = ; i <= k; i++)
{
tmp = pq1.top();
pq1.pop();
rres[i] = rres[i-]+tmp;//rres[i]表示选了i行的值
tmp-=p*m;
pq1.push(tmp);
}
while(!pq2.empty())pq2.pop();
for(int i = ; i < m; i++)
{
pq2.push(csum[i]);
}
for(int i = ; i <= k; i++)
{
tmp = pq2.top();
pq2.pop();
cres[i] = cres[i-]+tmp;//cres[i]表示选了i列的值
tmp-=p*n;
pq2.push(tmp);
}
ll happy = inf;
happy = -happy*happy;
for(int i = ; i <= k; i++)
happy = max(happy, rres[i]+cres[k-i]-p*i*(k-i));
cout<<happy<<endl;
} return ;
}
CodeForces 446B的更多相关文章
- codeforces 446B(优先队列)
题目链接:http://codeforces.com/problemset/problem/446/B #include<bits/stdc++.h> using namespace st ...
- CodeForces 446B DZY Loves Modification
题意: k次操作 每次选择一行或一列 得到所选数字的和 并将所选数字同一时候减去p 问最多得到多少 思路: 重点在消除行列间的相互影响 因为每选一行全部列所相应的和都会-p 那么假设选了i次 ...
- 11.1 正睿停课训练 Day14
目录 2018.11.1 正睿停课训练 Day14 A 字符串 B 取数游戏(贪心) C 魔方(模拟) 考试代码 B C 2018.11.1 正睿停课训练 Day14 时间:3.5h 期望得分:100 ...
- 【组队赛三】-D 优先队列 cf446B
DZY Loves Modification Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Sub ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
随机推荐
- HttpClient 教程 (二)
第二章 连接管理 HttpClient有一个对连接初始化和终止,还有在活动连接上I/O操作的完整控制.而连接操作的很多方面可以使用一些参数来控制. 2.1 连接参数 这些参数可以影响连接操作: 'ht ...
- PAT (Advanced Level) 1002. A+B for Polynomials (25)
为0的不要输出. #include<iostream> #include<cstring> #include<cmath> #include<algorith ...
- Java虚拟机中的内存分配
java虚拟机在执行Java程序的过程中会把它所管理的内存划分为若干个不同的数据区域.这些区域都有各自的用途以及创建和销毁的时间. 栈:存放的是局部变量,包括:1.用来保存基本数据类型的值:2.保存类 ...
- 2014专业知识学习---be strong
一 公司工作 完成好自动化营销系统构建,并以此为契机掌握推荐,数据分析,可视化等知识 1 完成统计和可视化.具体参考 cookie mapping项目规划 2 以广告投放为契机,学习数据分析,推荐系统 ...
- linux usb installer
其实很简单,手册上有,cp debian....iso /dev/sdc,但是要把sdc上的分区删掉了先. This will only work if it is a hybrid ISO cont ...
- eclipse java生成exe
eclipse导出jar文件再将它转换成exe可执行文件详解 关键字: 欢迎光临 此文章是为了帮助刚接触j2se或不懂如何打包jar文件的人而着笔,同时也是让自己的知识以文章的形式保存起来. 一.导出 ...
- (总结)Linux的chattr与lsattr命令详解
PS:有时候你发现用root权限都不能修改某个文件,大部分原因是曾经用chattr命令锁定该文件了.chattr命令的作用很大,其中一些功能是由Linux内核版本来支持的,不过现在生产绝大部分跑的li ...
- Arduino线程库ProtoThreads
参考: Arduino线程库ProtoThreads 一个“蝇量级” C 语言协程库
- CodeForces 622D Optimal Number Permutation
是一个简单构造题. 请观察公式: 绝对值里面的就是 |di-(n-i)|,即di与(n-i)的差值的绝对值. 事实上,对于任何n,我们都可以构造出来每一个i的di与(n-i)的差值为0. 换句话说,就 ...
- 22、手把手教你Extjs5(二十二)模块Form的自定义的设计[1]
下面开始设计和完成一个简单的Form的自定义过程.先准备数据,在ModuleModel.js中的data属性下面,加入自定义Form的参数定义,下面的代码中定义了一个新的属性tf_formScheme ...