您的位置:首页 > 其它

codeforces 446B DZY Loves Modification(枚举)

2015-01-09 17:13 531 查看
题目链接

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:

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 test(s)

Input
2 2 2 2
1 3
2 4


Output
11


Input
2 2 5 2
1 3
2 4


Output
11


题意:n*m的数字矩阵,要刚好操作k次,以及系数p。每次操作可以获得一行或一列的数的和的价值,然后把该行或该列的每个数减p。求获得的价值最大为多少?

题解:先把行和列分开考虑,预处理出单独操作行(1到k次)的最大价值,预处理出单独操作列(1到k次)的最大价值。然后枚举行操作多少次,列操作多少次。若行操作x次的最大价值H[x],列操作x的最大价值为L[x]。若行操作了i次,列操作k-i次,则答案为H[i]+L[k-i]-p*i*(k-i)。

代码如下:

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<string>
#include<stack>
#include<math.h>
#include<vector>
#include<set>
#include<map>
#define nn 1100
#define inff 0x3fffffff
#define eps 1e-8
#define mod 1000000007
typedef long long LL;
const LL inf64=LL(inff)*inff;
using namespace std;
int n,m,k;
LL p;
int a[nn][nn];
multiset<int>se;
multiset<int>::iterator it;
LL f1[nn*nn],f2[nn*nn];
int main()
{
int i,j;
while(scanf("%d%d%d%I64d",&n,&m,&k,&p)!=EOF)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
scanf("%d",&a[i][j]);
}
}
int ix;
se.clear();
for(i=1;i<=n;i++)
{
ix=0;
for(j=1;j<=m;j++)
{
ix+=a[i][j];
}
se.insert(ix);
}
f1[0]=0;
for(i=1;i<=k;i++)
{
it=se.end();
it--;
f1[i]=f1[i-1]+(*it);
ix=(*it);
se.erase(it);
se.insert(ix-(int)p*m);
}
se.clear();
for(j=1;j<=m;j++)
{
ix=0;
for(i=1;i<=n;i++)
{
ix+=a[i][j];
}
se.insert(ix);
}
f2[i]=0;
for(i=1;i<=k;i++)
{
it=se.end();
it--;
f2[i]=f2[i-1]+(*it);
ix=*it;
se.erase(it);
se.insert(ix-(int)p*n);
}
LL ans=-inf64;
for(i=0;i<=k;i++)
{
ans=max(ans,f1[i]+f2[k-i]-p*i*(k-i));
}
printf("%I64d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: