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 优先队列的更多相关文章

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

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

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

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

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

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

  7. Codeforces Round #FF (Div. 1) A. DZY Loves Sequences

    题目链接: http://www.codeforces.com/contest/446/problem/A 题解: dp1[x]表示以x结尾的最大严格升序连续串,dp2[x]表示以x开头的最大严格升序 ...

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

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

随机推荐

  1. HDU 6061 RXD and functions

    题目链接:HDU-6061 题意:给定f(x),求f(x-A)各项系数. 思路:推导公式有如下结论: 然后用NTT解决即可. 代码: #include <set> #include < ...

  2. 2017北京网络赛 Bounce GCD加找规律

    题目链接:http://hihocoder.com/problemset/problem/1584 题意:就是求一个小球从矩阵的左上角,跑到矩形的右下角不能重复经过的格子,小球碰到墙壁就反射. 解法: ...

  3. 十一、springcloud之链路追踪Sleuth

    一.背景 随着微服务的数量增长,一个业务接口涉及到多个微服务的交互,在出错的情况下怎么能够快速的定位错误 二.简介 Spring Cloud Sleuth 主要功能就是在分布式系统中提供追踪解决方案, ...

  4. No.1 selenium学习之路之浏览器操作

    selenium基础,首先就是浏览器的相关操作 下面描述几种浏览器的常用操作 1.打开浏览器 webdriver后面添加想要打开的浏览器 Ie或者Chrome 2.打开指定页面(百度) 3.休眠时间 ...

  5. gtk+学习笔记(六)

    今天用到了滚动窗口和微调按钮,根据网上的信息,简单总结下用法. 滚动窗口只能添加一个控件到其中 scrolled=gtk_scrolled_window_new(NULL,NULL); /*创建滚动窗 ...

  6. Metronic 5.0.5 bootstrap后台管理模板

    演示地址:http://keenthemes.com/preview/metronic/    下载 Dashboard Table

  7. sicily 1051. Biker's Trip Odomete

    DescriptionMost bicycle speedometers work by using a Hall Effect sensor fastened to the front fork o ...

  8. Java学习笔记之:Struts2.0 环境搭建

    一.介绍 Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的数据交互. 二 ...

  9. ifdown eth0或service network restart

    错误提示信息如下: Shutting down interface eth0:  Error: Device 'eth0' (/org/freedesktop/NetworkManager/Devic ...

  10. sort sign numeric

    //JS001   EXEC PGM=SORT                                 //*                                          ...