D. DZY Loves Modification
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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:

  1. 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.
  2. 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 test(s)
input
2 2 2 2
1 3
2 4
output
11
input
2 2 5 2
1 3
2 4
output
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次操作.

操作1:把一行的每一个元素都减去p,你能够获得该行全部元素和(操作之前)的pleasure

操作2:把一列的每一个元素都减去p,你能够获得该列全部元素和(操作之前)的pleasure

问你最大能够获得的pleasure是多少

思路:枚举操作1所进行的次数,每次取能获得最大的行,用优先队列维护,要注意预处理,否则会超时

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
typedef long long LL;
using namespace std;
int A[1005][1005];
LL R[1005],C[1005];
LL s1[1000005],s2[1000005];
void get_C(int n,int i,int (*A) [1005])
{
LL s=0;
for(int j=1;j<=n;j++)
s+=A[j][i];
C[i]=s;
}
void get_R(int m,int i,int (*A)[1005])
{
LL s=0;
for(int j=1;j<=m;j++)
s+=A[i][j];
R[i]=s;
}
int main()
{
int n,m,k,p;
while(scanf("%d%d%d%d",&n,&m,&k,&p)==4)
{
LL s=0;
s1[0]=s2[0]=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
scanf("%d",&A[i][j]);
LL sum=-1000000000000009;
priority_queue<LL>q1;
priority_queue<LL>q2;
for(int i=1;i<=n;i++){get_R(m,i,A);q1.push(R[i]);}
for(int i=1;i<=m;i++){get_C(n,i,A);q2.push(C[i]);}
for(int j=1;j<=k;j++)
{
LL temp=q1.top();
q1.pop();
s1[j]=s1[j-1]+temp;
temp-=p*m;
q1.push(temp);
}
for(int j=1;j<=k;j++)
{
LL temp=q2.top();
q2.pop();
s2[j]=s2[j-1]+temp;
temp-=p*n;
q2.push(temp);
}
for(int i=0;i<=k;i++)
{
s=s1[i]+s2[k-i];
sum=max(sum,s-LL(i)*p*(k-i));
}
printf("%I64d\n",sum);
}
return 0;
}

D. DZY Loves Modification的更多相关文章

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

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

  3. Codeforces 447D - DZY Loves Modification

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

  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. 1) B. DZY Loves Modification

    枚举行取了多少次,如行取了i次,列就取了k-i次,假设行列单独贪心考虑然后相加,那么有i*(k-i)个交点是多出来的:dpr[i]+dpc[k-i]-i*(k-i)*p 枚举i取最大值.... B. ...

  6. B. DZY Loves Modification

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

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

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

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

  9. CodeForces 446B DZY Loves Modification

    题意: k次操作  每次选择一行或一列  得到所选数字的和  并将所选数字同一时候减去p  问最多得到多少 思路: 重点在消除行列间的相互影响 因为每选一行全部列所相应的和都会-p  那么假设选了i次 ...

随机推荐

  1. gem update --system 302 错误 解决方案(转)

    具体过程如下: 1.InstantRails-2.0安装后,在配置环境变量path中配置ruby/bin目录(如果系统中有多个RUBY,执行命令行的时候系统认的就是path中的) 2.进入DOS命令行 ...

  2. JIRA官方:JIRA报表与分析

    访问重要的问题 JIRA系统内置的过滤器可以使你快速访问最重要的问题.通过保存和收藏自定义的过滤器,你可以随时了解项目和团队的优先级. 保持团队同步 创建一个过滤器,可以保存你的任何搜索条件.通过分享 ...

  3. 网易云课堂_C语言程序设计进阶_期末考试编程题部分

    1 字符串循环右移(5分) 题目内容: 输入一个字符串和一个非负整数N,要求将字符串循环右移N次. 输入格式: 输入在第1行中给出一个字符串,以'#'表示结束,‘#’不是字符串的一部分,字符串的长度未 ...

  4. iOS伪实现打地鼠游戏

    打地鼠是一款可以用iOS知识来实现的一种游戏.其核心技术就是通过imageView来播放动画,点击button时来停止当前播放的动画开始击打地鼠的动画.话不多说直接上代码. 这是添加当前的背景图片,然 ...

  5. Gartner公布了集成系统的魔力象限 - Nutanix的关键技术是什么?

    读报告,分析报告,写报告.这活儿我不专业.专业的是西瓜哥的这个:http://www.dostor.com/article/2014-06-25/9776476.shtml 再列出个几篇文章供參考: ...

  6. 黑科技——编写一个无法卸载的App

    之前经常听到朋友或者新闻媒体上报道说,有的朋友的android手机中病毒了,出现了软件无法卸载的情况,对于我这样一个从事android开发程序员来说,我还不是太相信(毕竟自己还是有点菜,哈哈).今天在 ...

  7. 字符串聚合技术(String Aggregation Techniques)

    from: http://www.oracle-base.com/articles/misc/string-aggregation-techniques.php String Aggregation ...

  8. VS2010 添加资源文件后,出现 “LNK1123: 转换到 COFF 期间失败: 文件无效或损坏”错误

    1>LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏 解决方法: 一.1.点击“项目”-->“属性”-->“清单工具” 2.‘输入 ...

  9. SPL的基本使用

    SPL是Standard PHP Library(PHP标准库)的缩写. 根据官方定义,它是"a collection of interfaces and classes that are ...

  10. java创建对象的几种常用方法

    java几种常见的创建对象的方法: 1.使用new关键字创建对象 2.利用java的反射机制,通过java.lang.Class或者java.lang.reflect.Constructor创建对象 ...