1. 程式人生 > >[CodeForces - 447D] D - DZY Loves Modification

[CodeForces - 447D] D - DZY Loves Modification

input 選擇 個數 after example could output max sum

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:

  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.

Example

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的矩陣,每個位置有一個數.你可以進行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 }
View Code

[CodeForces - 447D] D - DZY Loves Modification