/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Accepted 2ms 540.0 KiB
#3 Accepted 19ms 540.0 KiB
#4 Accepted 20ms 608.0 KiB
#5 Accepted 14ms 1.277 MiB
#6 Accepted 58ms 2.402 MiB
#7 Accepted 38ms 2.383 MiB
#8 Accepted 40ms 2.383 MiB
#9 Accepted 38ms 2.461 MiB
#10 Accepted 45ms 2.395 MiB
#11 Accepted 36ms 2.383 MiB
#12 Accepted 35ms 2.391 MiB
#13 Accepted 12ms 584.0 KiB
#14 Accepted 15ms 540.0 KiB
#15 Accepted 2ms 332.0 KiB
#16 Accepted 44ms 1.363 MiB

Code

#include <bits/stdc++.h>
using namespace std;
#define SC               scanf
#define PF               printf
#define ull              unsigned long long
#define ld               long double
#define F                first
#define S                second
#define pb               push_back
#define sort_a(a)        sort(a.begin(),a.end());
#define sort_d(a)        sort(a.rbegin(),a.rend());
#define READ(f)          freopen(f, "r", stdin)
#define WRITE(f)         freopen(f, "w", stdout)
#define rev(s)           reverse(s.begin(),s.end())
#define P(ok)            cout << (ok ? "YES\n": "NO\n")
#define __Heart__              ios_base :: sync_with_stdio(false); cin.tie(NULL);
#define ll long long
typedef pair< ll , ll>                   PII;
const int sz = 505 ;
int Arr[sz][sz] , n , m , val;
int dx[] = {-1 , 0 , 0 , 1} ;
int dy[] = {0 , 1 , -1 , 0} ;
int heart(int x , int y){
   int cur = INT_MAX ;
  for(int i = 0 ; i < 4 ; i++){
    int cur_i = dx[i] + x ;
    int cur_j = dy[i] + y ;
    if(cur_i >= 0 && cur_i < n && cur_j >= 0 && cur_j < m){
        cur = min(cur , Arr[cur_i][cur_j] + Arr[x][y]) ;
    }
  }
  return cur ;
}
void solve()
{
    cin >> n >> m ;
   vector < int > v ;
   vector < PII > V ;
   for(int i = 0 ; i < n ; i++){
    for(int j = 0 ; j < m ; j++){
        cin >> val ;
        Arr[i][j] = INT_MIN ;
        v.pb(val) ;
    }
   }
   sort_a(v) ;
   int cur_st = 0 , cur_en = v.size() - 1 ;
   for(int i = 0 ; i < n ; i++){
    for(int j = i % 2 ; j < m ; j += 2){
        Arr[i][j] = v[cur_st++] ;
        if(j + 1 < m && Arr[i][j + 1] == INT_MIN) Arr[i][j + 1] = v[cur_en--] ;
        if(i + 1 < n && Arr[i + 1][j] == INT_MIN) Arr[i + 1][j] = v[cur_en--] ;
    }
   }
   int Ans = INT_MAX ;
   for(int i = 0 ; i < n ; i++){
    for(int j = 0 ; j < m ; j++){
        Ans = min(Ans , heart(i , j)) ;
        //cout << i << " " << j << " --> " << heart(i , j) << endl ;
    }
   }
   cout << Ans << "\n" ;
}
int main()
{
     __Heart__
     int t ; cin >> t ; while(t--) solve() ;
}

Information

Submit By
Type
Submission
Problem
P1065 Matrix Sum
Language
C++20 (G++ 13.2.0)
Submit At
2024-07-13 23:04:31
Judged At
2024-07-13 23:04:31
Judged By
Score
100
Total Time
58ms
Peak Memory
2.461 MiB