[codeforces1219G]Harvester

time limit per test : 0.5 seconds
memory limit per test : 256 megabytes

It is Bubble Cup finals season and farmer Johnny Bubbles must harvest his bubbles. The bubbles are in a rectangular bubblefield formed of N N Nx M M M square parcels divided into N N N rows and M M M columns. The parcel in ith row and jth column yields A i , j A_{i,j} Ai,j bubbles.

Johnny Bubbles has available a very special self-driving bubble harvester that, once manually positioned at the beginning of a row or column, automatically harvests all the bubbles in that row or column. Once the harvester reaches the end of the row or column it stops and must be repositioned. The harvester can pass through any parcel any number of times, but it can collect bubbles from the parcel only once.

Johnny is very busy farmer, so he is available to manually position the harvester at most four times per day. Johnny is also impatient, so he wants to harvest as many bubbles as possible on the first day.
Please help Johnny to calculate what is the maximum number of bubbles he can collect on the first day.

Input

The first line contains two integers N N N and M ( 1 ≤ N , M ≤ N ∗ M ≤ 1 0 5 ) M (1 ≤ N, M ≤ N * M ≤ 10^5) M(1N,MNM105) - the bubblefield size.

Each of the next N N N lines contains M integers. The jth element in the ith line is A i , j ( 0 ≤ A i , j ≤ 1 0 9 ) A_{i,j} (0 ≤ A_{i,j} ≤ 10^9) Ai,j(0Ai,j109) — the yield of the parcel located in the ith row and the j j j-th column.

Output

Output contains one integer number - maximum number of the bubbles Johnny can harvest on the first day.
Examples
Input

2 2
1 2
3 4

Output

10

Input

5 5
0 9 2 7 0
9 0 3 0 5
0 8 0 3 1
6 7 4 3 9
3 6 4 1 0

Output

80

Note

In the first example, farmer Johnny can harvest all the bubbles by positioning the harvester on the first and the second row.

In the second example, one way Johnny can harvest maximum number of bubbles is to position the harvester in the second row, the fourth row, the second column and the fourth column.

题意:
给定一个 N ∗ M < = 1 0 5 N*M<=10^5 NM<=105的矩阵,询问从矩阵上最多取出行列一共4个,问取出元素的最大值是多少。

题解:
分5种情况:
1:全选行,直接做
2:全选列,直接做
3:一行三列,枚举行,维护去掉行的最大的三个行的值 O ( N ∗ M ) O(N*M) O(NM)
4:一列三行,枚举列,维护去掉列的最大的三个行的值 O ( N ∗ M ) O(N*M) O(NM)
5:两行两列,枚举行列中短的那个,然后扫一遍维护另一个的最大次大值。 O ( m i n ( N , M ) 2 ∗ m a x ( N , M ) ) O(min(N,M)^2*max(N,M)) O(min(N,M)2max(N,M))

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n,m;
inline int pos(int x,int y){return (x-1)*m+y;}
int a[100004],a2[100004];
ll scol[100004],srow[100004];
priority_queue<ll,vector<ll>,greater<ll> >q;
ll getcol(){
    while(!q.empty())q.pop();
    for(int i=1;i<=m;i++){
        ll now=0;
        if(q.size()<4){
            q.push(scol[i]);
        }
        else if(q.top()<scol[i]){
            q.pop();
            q.push(scol[i]);
        }
    }
    ll ret=0;
    while(!q.empty()){
        ret+=q.top();q.pop();
    }
    return ret;
}
ll getrow(){
    while(!q.empty())q.pop();
    for(int i=1;i<=n;i++){
        ll now=0;
        if(q.size()<4){
            q.push(srow[i]);
        }
        else if(q.top()<srow[i]){
            q.pop();
            q.push(srow[i]);
        }
    }
    ll ret=0;
    while(!q.empty()){
        ret+=q.top();q.pop();
    }
    return ret;
}
ll sekcol(){

    ll ret=0;
    for(int i=1;i<=m;i++){
        ll now=scol[i];
        while(!q.empty())q.pop();
        for(int j=1;j<=n;j++){
            if(q.size()<3){
                q.push(srow[j]-a[pos(j,i)]);
            }
            else if(q.top()<srow[j]-a[pos(j,i)]){
                q.pop();
                q.push(srow[j]-a[pos(j,i)]);
            }
        }
        while(!q.empty()){
            now+=q.top();q.pop();
        }
        ret=max(now,ret);
    }
    return ret;
}
ll sekrow(){

    ll ret=0;
    for(int i=1;i<=n;i++){
        ll now=srow[i];
        while(!q.empty())q.pop();
        for(int j=1;j<=n;j++){
            if(q.size()<3){
                q.push(scol[j]-a[pos(i,j)]);
            }
            else if(q.top()<scol[j]-a[pos(i,j)]){
                q.pop();
                q.push(scol[j]-a[pos(i,j)]);
            }
        }
        while(!q.empty()){
            now+=q.top();q.pop();
        }
        ret=max(now,ret);
    }
    return ret;
}
ll work(){
    ll ret=0;
    for(int i=1;i<=n;i++){
        for(int j=i+1;j<=n;j++){
            ll now=srow[i]+srow[j];
            while(!q.empty())q.pop();
            for(int k=1;k<=m;k++){
                if(q.size()<2){
                    q.push(scol[k]-a[pos(i,k)]-a[pos(j,k)]);
                }
                else if(q.top()<scol[k]-a[pos(i,k)]-a[pos(j,k)]){
                    q.pop();
                    q.push(scol[k]-a[pos(i,k)]-a[pos(j,k)]);
                }
            }
            while(!q.empty()){
                now+=q.top();q.pop();
            }
            ret=max(now,ret);
        }
    }
    return ret;
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            scanf("%d",&a2[pos(i,j)]);
    if(n>m){
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                a[(j-1)*n+i]=a2[pos(i,j)];
            }
        }
        swap(n,m);
    }
    else{
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                a[pos(i,j)]=a2[pos(i,j)];
            }
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            int v=a[pos(i,j)];
            srow[i]+=v;
            scol[j]+=v;
        }
    }
    ll ans=0;
    ans=max(ans,getcol());
    ans=max(ans,getrow());
    ans=max(ans,sekcol());
    ans=max(ans,sekrow());
    ans=max(ans,work());
    printf("%lld\n",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值