Codeforces Round #FF (Div. 2)D. DZY Loves Modification

16 篇文章 0 订阅

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的矩阵,有两种操作方式:统计某行或者某列的和并将每项减去p。问k个操作后,取的最大值是多少。

题解:

我刚开始想法是每次取行列中和最大的,然后减去多少个p(减多少个p根据取的是行或者列可知,取某一列之和则减去行数*p,同理)。再将其值加入优先队列,一共取k次,再看这k次取了多少行r,多少列w,最后将其和减去r*w*p即可。后来测试发现这种做法是错误的,因为有可能存在一种情况,取两个列比取1行1列的值要小。仔细想一下吧。后来我觉得还是用贪心,取前i行之和+取前k-i列之和再减去i*(k-i)*p的最大值即是答案。有个小地方需要注意,max赋值的时候最小值得很小很小,远超过Int最小值0x7fffffff。所以,嗯,就这样。帖代码。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>
#include <limits>
using namespace std;
#define Max(a,b) a>b?a:b
struct node
{
	__int64 v;
};
__int64 r[1000010],w[1000010];
int main()
{
	__int64 n,m,k,p;
	priority_queue <__int64> q;
	scanf("%I64d%I64d%I64d%I64d",&n,&m,&k,&p);
	__int64 i,j,t;
	memset(r,0,sizeof(r));
	memset(w,0,sizeof(w));
	for (i=1;i<=n;i++) //w代表列,r代表行 
	{
		for (j=1;j<=m;j++)
		{
			scanf("%I64d",&t);
			r[i]+=t;
			w[j]+=t;
		}
	}
	while (!q.empty()) q.pop();
	for (i=1;i<=n;i++)
	{
		q.push(r[i]);
		r[i]=0;
	}
	for (i=1;i<=k;i++)
	{
		__int64 h=q.top();
		q.pop();
		r[i]=r[i-1]+h;
		h-=p*m;
		q.push(h);
	}
	while (!q.empty())q.pop();
	for (i=1;i<=m;i++)
	{
		q.push(w[i]);
		w[i]=0;
	}
	for (i=1;i<=k;i++)
	{
		__int64 h=q.top();
		q.pop();
		w[i]=w[i-1]+h;
		h-=p*n;
		q.push(h);
	}
	__int64 ans=-1e18;
	for (i=0;i<=k;i++)
	{
		ans=Max(ans,r[i]+w[k-i]-(__int64)(i*(k-i)*p));
	}
	printf("%I64d\n",ans);
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值