I - DZY Loves Modification--行列单独操作+优先队列

   

  行列单独操作+优先队列


  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:

  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.



这道题的大意就是给你一个nXm矩阵,k 次操作,每次可以单独操作一行或者一列,每次操作后的得到所操作行或者列的值

,然后行或者列上的每一个值减去p;在k次操作之后的得到的最大值:

 

思路:

我们先可以对矩阵的行,列分开考虑,求单独操作i次行,i次列所得到的做大值:这里我们可以用优先队列来优化;

然后我们用贪心的思想,枚举操作i次行(k-i)次列的值

row[i]+culm{k-i}-i*(k-i)*p;

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<iostream>
#include<math.h>
using namespace std;
#define mx 1003
#define mm 1000007
#define LL long long    //数据会超范围 

LL a[mx][mx];
LL n,m,p,k;
LL row[mm],cul[mm];

int main()
{
    LL i,j;
    LL n;
    while(scanf("%I64d %I64d %I64d %I64d",&n,&m,&k,&p)!=EOF)
    {
        memset(row,0,sizeof(row));
        memset(cul,0,sizeof(cul));
        priority_queue<int> r,c;

        LL sum=0;
        for(i=0;i<n;i++)
            for(j=0;j<m;j++)
                 scanf("%I64d",&a[i][j]);
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
               sum+=a[i][j];
               r.push(sum);
               sum=0;
        }
        sum=0;
        for(i=0;i<m;i++)
        {
            for(j=0;j<n;j++)
              sum+=a[j][i];
              c.push(sum);
              sum=0;
        }
        LL tt;
         for(i=1;i<=k;i++)
         {
              tt=r.top();
              r.pop();
              row[i]=tt+row[i-1];
              tt-=m*p;
              r.push(tt);
         }
          for(i=1;i<=k;i++)
          {
             tt=c.top();
             c.pop();
              cul[i]=tt+cul[i-1];
              tt-=n*p;
              c.push(tt);
          }
          LL maxx=row[k];///这里要注意,我就死在这里n次,主要是数据范围很大,用-0x3fffffff的话会wrong,
                              也可以用-0x3fffffffffffffff; 
          for(i=0;i<=k;i++)
          {

              sum=row[i]+cul[k-i]-i*p*(k-i);
              maxx=max(sum,maxx);
          }
           printf("%I64d\n",maxx);
    }

}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值