/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Accepted 10ms 596.0 KiB
#3 Accepted 398ms 636.0 KiB
#4 Accepted 453ms 648.0 KiB
#5 Accepted 128ms 3.016 MiB
#6 Accepted 215ms 3.672 MiB
#7 Accepted 198ms 3.852 MiB
#8 Accepted 213ms 3.129 MiB
#9 Accepted 220ms 2.926 MiB
#10 Accepted 220ms 3.145 MiB
#11 Accepted 167ms 2.93 MiB
#12 Accepted 237ms 2.992 MiB
#13 Accepted 213ms 636.0 KiB
#14 Accepted 259ms 632.0 KiB
#15 Accepted 1ms 540.0 KiB
#16 Accepted 547ms 1.438 MiB

Code

#include<bits/stdc++.h>
using namespace std;
const long long M=1e7+1,MOD=1000000007;
typedef long long ll;
#define double long double
#define debug(x) cout<<x<<endl
int a[800][800];
void clr(int n,int m){
    for(int i=0;i<=n;i++)
        for(int j=0;j<=m;j++)a[i][j]=1e5;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
       int n,m;
       cin>>n>>m;
      vector<int>st;
       for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            int b;
            cin>>b;
            st.push_back(b);
       }
   }
   sort(st.begin(),st.end());
       int l=-2e3,r=2e3;
       int res=-1e5;
       while(l<=r){
        int mid=l+(r-l)/2;
        int ok=1;
        map<int,int>mp;
        for(auto it:st)mp[it]++;
        clr(n,m);
        for(int i=1;i<=n&&ok;i++){
            for(int j=1;j<=m&&ok;j++){
                if(i==1 && j==1){
                    auto it=mp.begin();
                    a[i][j]=it->first;
                    mp[a[i][j]]--;
                    if(mp[a[i][j]]==0)
                    mp.erase(a[i][j]);
                }
                else{
                    int x=min(a[i-1][j],a[i][j-1]);
                    int lagba=mid-x;
                    auto it=mp.lower_bound(lagba);
                    if(it==mp.end()){
                        ok=0;
                        break;
                    }
                    a[i][j]=it->first;
                    mp[it->first]--;
                    if(mp[it->first]==0)
                    mp.erase(a[i][j]);
                }
                
            }
        }
        int ok1=1;
        clr(n,m);
        mp.clear();
        for(auto i:st)mp[i]++;
        for(int i=1;i<=n&&ok1;i++){
            for(int j=1;j<=m&&ok1;j++){
                if(i==1 && j==1){
                    auto it=mp.rbegin();
                    a[i][j]=it->first;
                    mp[a[i][j]]--;
                    if(mp[a[i][j]]==0)
                    mp.erase(a[i][j]);
                }
                else{
                    int x=min(a[i-1][j],a[i][j-1]);
                    int lagba=mid-x;
                    auto it=mp.lower_bound(lagba);
                    if(it==mp.end()){
                        ok1=0;
                        break;
                    }
                    a[i][j]=it->first;
                    mp[it->first]--;
                    if(mp[it->first]==0)
                    mp.erase(a[i][j]);
                }
                
            }
        }
        if(ok || ok1){
            res=mid;
            l=mid+1;
        }
        else r=mid-1;

       }
       cout<<res<<"\n";
    }


    return 0;
 
}

Information

Submit By
Type
Submission
Problem
P1065 Matrix Sum
Language
C++20 (G++ 13.2.0)
Submit At
2024-06-03 13:11:26
Judged At
2024-06-03 13:11:26
Judged By
Score
100
Total Time
547ms
Peak Memory
3.852 MiB