Submission #1013652

#TimeUsernameProblemLanguageResultExecution timeMemory
1013652amine_arouaNetrpeljivost (COI23_netrpeljivost)C++17
27 / 100
1528 ms10120 KiB
#include<bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #pragma GCC target("avx2") #include<ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; using namespace std; #define intt long long #define pb push_back vector<vector<int>> mat; int n ; const intt INF = 1e18; vector<vector<vector<intt>>> dp; void preprocess(int node , int l , int r) { vector<vector<intt>> v; vector<intt> nodes; for(int i = l ; i <= r ; i++) nodes.pb(INF); for(int i = l ; i <= r ; i++) { v.pb(nodes); } dp[node] = v; if(l == r) return; int m = (l + r)/2; preprocess(2*node , l , m); preprocess(2*node + 1 , m + 1 , r); } intt solve(int node ,int fr , int bk ,int l , int r) { if(dp[node][fr - l][bk - l] != INF) return dp[node][fr - l][bk - l]; if(l + 1 == r) { return mat[fr][bk]; } int m = (l + r)/2; intt ans = INF; if(l <= fr && fr <= m && m + 1 <= bk && bk <= r) { int L1 = l , R1 = (m + l)/2; int L2 = m + 1 , R2 = (m + 1 + r)/2; if(L1 <= fr&& fr <= R1 ) { L1 = R1 + 1; R1 = m; } if(L2 <= bk && bk <= R2) { L2 = R2 + 1; R2 = r; } for(int v1 = L1 ; v1 <= R1 ;v1++) { for(int v2 = L2; v2 <= R2 ; v2++) { ans = min(ans , solve(2*node , fr , v1 , l , m) + solve(2*node + 1 , v2 , bk , m + 1 , r) + mat[v1][v2]); } } } else if(l <= bk && bk <= m && m + 1 <= fr && fr <= r) { int L1 = l , R1 = (m + l)/2; int L2 = m + 1 , R2 = (m + 1 + r)/2; if(L1 <= bk&& bk <= R1 ) { L1 = R1 + 1; R1 = m; } if(L2 <= fr && fr <= R2) { L2 = R2 + 1; R2 = r; } for(int v1 = L1 ; v1 <= R1 ; v1++) { if(v1 == bk) continue; for(int v2 = L2 ; v2 <= R2 ; v2++) { if(v2 == fr) continue; ans = min(ans , solve(2*node , v1 , bk , l , m) + solve(2*node + 1 , fr , v2 , m + 1 , r) + mat[v1][v2]); } } } return dp[node][fr-l][bk - l] = ans; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n; mat = vector<vector<int>> (n , vector<int>(n)); for(int i = 0 ; i < n ; i++) { for(int j = 0 ; j < n ; j++) { cin>>mat[i][j]; } } if(n == 1) { cout<<0<<'\n'; return 0; } dp.assign(2*n , vector<vector<intt>>()); preprocess(1 , 0 , n - 1); intt ans = INF; int l = 0 , r = n - 1; int m = (l + r)/2; for(int fr = l ; fr <= m ; fr++) { for(int bk = m + 1 ; bk <= r ; bk++) { ans = min({ans , solve(1 , fr , bk , l , r) , solve(1 , bk , fr , l , r)}); } } cout<<ans<<'\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...