D - DZY Loves Modification
As we know, DZY loves playing games. One day DZY decided to play with a n × mmatrix. To be more precise, he decided to modify the matrix with exactly koperations.
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.
Example
2 2 2 2
1 3
2 4
11
2 2 5 2
1 3
2 4
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次操作,每次操作可以选择一行(列),然后把ans累加上这一行(列)的数的和,然后把这一行(列)的每一个数都减去一个固定的值.
要求最大化ans.
我们设R[i]为选择i个(次)行的最优解,C[i]为选择i个(次)列的最优解,由于选择的总次数为K,所以
ans=max(ans,R[i]+C[K-i]).然而这是不对的,因为在考虑R和C的时候是相对独立的,不受另一个因素的影响,而实际上,选择i个行,j个列,会产生i*(K-i)个交点,由于交点的存在,所以还要减去i*(K-i)*p(那个固定值).
所以ans=max(ans,R[i]+C[K-1]-i*(K-i)*p)(要注意数据类型)
那么我们现在来关注如何构造出R和C.由于贪心的想法,我们肯定挑目前和最大的行(列),那么我们可以用两个优先队列来维护一下不就好了.

1 #include<cstdio>
2 #include<cstring>
3 #include<algorithm>
4 #include<vector>
5 #include<queue>
6 using namespace std;
7 const int maxn=1005,maxK=1000005;
8 int sum_R[maxn],sum_C[maxn];
9 long long R[maxK],C[maxK];
10 int n,m,K,p,a[maxn][maxn];
11 long long ans;
12 struct node{
13 int x,index;
14 bool operator < (const node u) const {return x<u.x;}
15 };
16 priority_queue <node> Q_R,Q_C;
17 int read(){
18 int x=0,f=1; char ch=getchar();
19 while (ch<'0'||ch>'9'){if (ch=='-') f=-f; ch=getchar();}
20 while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
21 return x*f;
22 }
23 int main(){
24 n=read(),m=read(),K=read(),p=read(),ans=0;
25 for (int i=1; i<=n; i++)
26 for (int j=1; j<=m; j++) a[i][j]=read();
27 for (int i=1; i<=n; i++){
28 sum_R[i]=0;
29 for (int j=1; j<=m; j++) sum_R[i]+=a[i][j];
30 }
31 for (int i=1; i<=m; i++){
32 sum_C[i]=0;
33 for (int j=1; j<=n; j++) sum_C[i]+=a[j][i];
34 }
35 R[0]=C[0]=0;
36 for (int i=1; i<=n; i++){node P; P.x=sum_R[i],P.index=i; Q_R.push(P);}
37 for (int i=1; i<=K; i++){
38 node P=Q_R.top(); Q_R.pop();
39 R[i]=R[i-1]+P.x,P.x-=m*p,Q_R.push(P);
40 }
41 for (int i=1; i<=m; i++){node P; P.x=sum_C[i],P.index=i; Q_C.push(P);}
42 for (int i=1; i<=K; i++){
43 node P=Q_C.top(); Q_C.pop();
44 C[i]=C[i-1]+P.x,P.x-=n*p,Q_C.push(P);
45 }
46 ans=-123456789012345;
47 for (int i=0; i<=K; i++) ans=max(ans,R[i]+C[K-i]-(long long)i*(K-i)*p);
48 printf("%lld",ans);
49 return 0;
50 }
本文探讨了DZY在n*m矩阵中通过k次操作,选择行或列减少元素并获得最大快乐值的问题。通过使用优先队列维护行和列的最大和,实现了选择i个行与j个列时的最优解计算,考虑了交点的影响。
1102

被折叠的 条评论
为什么被折叠?



