/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Accepted 10ms 592.0 KiB
#3 Accepted 430ms 2.609 MiB
#4 Accepted 454ms 648.0 KiB
#5 Accepted 376ms 2.945 MiB
#6 Accepted 1827ms 15.062 MiB
#7 Accepted 1527ms 14.426 MiB
#8 Accepted 1396ms 14.422 MiB
#9 Accepted 1449ms 14.219 MiB
#10 Accepted 1430ms 14.27 MiB
#11 Accepted 1994ms 14.371 MiB
#12 Accepted 1938ms 14.953 MiB
#13 Accepted 242ms 644.0 KiB
#14 Accepted 298ms 648.0 KiB

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){
    for(int i=0;i<=n;i++)
        for(int j=0;j<=n;j++)a[i][j]=1e5;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
       int n;
       cin>>n;
      vector<int>st;
       for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            int b;
            cin>>b;
            st.push_back(b);
       }
   }
   sort(st.begin(),st.end());
       int l=-2e4,r=2e4;
       int res=-1e5;
       while(l<=r){
        int mid=l+(r-l)/2;
        int ok=1;
        multiset<int>st1;
        for(auto it:st)st1.insert(it);
        clr(n);
        for(int i=1;i<=n&&ok;i++){
            for(int j=1;j<=n&&ok;j++){
                if(i==1 && j==1){
                    auto it=st1.begin();
                    a[i][j]=*it;
                    st1.erase(st1.find(a[i][j]));
                }
                else{
                    int x=min(a[i-1][j],a[i][j-1]);
                    int lagba=mid-x;
                    auto it=st1.lower_bound(lagba);
                    if(it==st1.end()){
                        ok=0;
                        break;
                    }
                    a[i][j]=*it;
                    st1.erase(st1.find(a[i][j]));
                }
                
            }
        }
        int ok1=1;
        clr(n);
        st1.clear();
        for(auto it:st)st1.insert(it);
         for(int i=1;i<=n&&ok1;i++){
            for(int j=1;j<=n&&ok1;j++){
                if(i==1 && j==1){
                    auto it=st1.rbegin();
                    a[i][j]=*it;
                    st1.erase(st1.find(a[i][j]));
                }
                else{
                    int x=min(a[i-1][j],a[i][j-1]);
                    int lagba=mid-x;
                    auto it=st1.lower_bound(lagba);
                    if(it==st1.end()){
                        ok1=0;
                        break;
                    }
                    a[i][j]=*it;
                    st1.erase(st1.find(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-05-30 17:26:41
Judged At
2024-05-30 17:26:41
Judged By
Score
100
Total Time
1994ms
Peak Memory
15.062 MiB