题目链接
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:
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.
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 a single integer — the maximum possible total pleasure value DZY could get.
2 2 2 2 1 3 2 4
11
2 2 5 2 1 3 2 4
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; }