【组队赛三】-D 优先队列 cf446B

DZY Loves Modification
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit

Status

Practice

CodeForces 446B
Description
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 Input
Input
2 2 2 2
1 3
2 4
Output
11
Input
2 2 5 2
1 3
2 4
Output
11
Hint
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

<span style="color:#3333ff;">/*
_______________________________________________________________________________________

       author    :   Grant yuan
       time      :   2014.7.21
       algorithm :   priority_queue
       explain   :   首先对行和列在1到k的范围内分别求解,然后求出一个满足i和k-i的最大值,
                     注意最终的结果中减去重复计算的,还有会测试大于int类型的数据。 
—————————————————————————————————————————————————————————————————————————————————————————
*/
    
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<functional>
using namespace std;
#define INF 999999999;

priority_queue<__int64>q1;
priority_queue<__int64>q2;
__int64 m,n,k,p,h;
__int64 a[1003][1003];
__int64 s1[1003],s2[1003];
__int64 sum1[1000003],sum2[1000003];
__int64 res,ans,hh;

int main()
{
    cin>>n>>m>>k>>p;
    ans=INF;
    ans=-ans*ans;
    memset(s1,0,sizeof(s1));
    memset(s2,0,sizeof(s2));
    memset(sum1,0,sizeof(sum1));
    memset(sum2,0,sizeof(sum2));
    while(!q1.empty())
      q1.pop();
    while(!q2.empty())
       q2.pop();

    for(int i=0;i<n;i++)
      for(int j=0;j<m;j++){
        scanf("%I64d",&a[i][j]);
        s1[i]+=a[i][j];
        s2[j]+=a[i][j];}

    for(int i=0;i<n;i++)
      q1.push(s1[i]);

     for(int i=0;i<m;i++)
       q2.push(s2[i]);

    for(int i=1;i<=k;i++)
    {
        h=q1.top();q1.pop();
        if(i==1)sum1[i]=h;
        else sum1[i]=sum1[i-1]+h;
        h-=m*p;
        q1.push(h);
    }
     for(int i=1;i<=k;i++)
    {
        h=q2.top();q2.pop();
        if(i==1)sum2[i]=h;
        else sum2[i]=sum2[i-1]+h;
        h-=n*p;
        q2.push(h);
    }

    for(int i=0;i<=k;i++)
        {
            res=sum1[i]+sum2[k-i];
            ans=max(ans,res-i*p*(k-i));
        }

    cout<<ans<<endl;
    return 0;

}
</span>



转载于:https://www.cnblogs.com/codeyuan/p/4254491.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值