Submission #523645

#TimeUsernameProblemLanguageResultExecution timeMemory
523645maks007Maxcomp (info1cup18_maxcomp)C++14
15 / 100
347 ms296 KiB
#include <bits/stdc++.h> using namespace std; int dis(pair <int,int>a, pair <int,int> b) { return abs(a.first - b.first) + abs(a.second - b.second); } int main(void) { int n, m; cin >> n >> m; if(n == 1) { vector <int> a(m); for(int i = 0; i < m; i ++) cin >> a[i]; int ans = INT_MIN; for(int i = 0; i < m; i ++) { for(int end = i; end < m; end ++) { ans = max(ans, *max_element(a.begin() + i, a.begin() + end) - *min_element(a.begin() + i, a.begin() + end) - (end - i)); // cout << *max_element(a.begin() + i, a.begin() + end) << " " << *min_element(a.begin() + i, a.begin() + end) << endl; } } cout << ans; return false; } vector <vector <int> > g(n+2, vector <int> (m+2, -1)); vector <vector <int> > used(n+2, vector <int> (m+2, 0)); queue <pair <int,int>> q; int it[] = {0, 0, +1, -1}; int jt[] = {+1, -1, 0, 0}; function <int(int, int, pair <int,int>, pair <int,int>)> can=[&](int mn, int mx, pair <int,int> s, pair <int,int> e) { for(int i = 1; i <= n; i ++) { for(int j = 1; j <= m; j ++) used[i][j] = 0; } q.push(s); used[s.first][s.second] = 1; while(!q.empty()) { pair <int,int> v = q.front(); q.pop(); for(int to = 0; to < 4; to ++) { int ni = v.first + it[to]; int nj = v.second + jt[to]; if(!used[ni][nj] and g[ni][nj] != -1 and g[ni][nj] >= mn and g[ni][nj] <= mx) { q.push({ni, nj}); used[ni][nj] = 1; } } } return used[e.first][e.second]; }; for(int i = 1; i <= n; i ++) { for(int j = 1; j <= m; j ++) cin >> g[i][j]; } int ans = INT_MAX; for(int i = 1; i <= n; i ++) { for(int j = 1; j <= m; j ++) { for(int k = i+1; k <= n; k ++) { for(int l = j+1; l <= m; l ++) { if(can(min(g[i][j], g[k][l]), max(g[i][j], g[k][l]), {i, j}, {k, l}) == 1) { if(max(g[i][j], g[k][l]) - min(g[i][j], g[k][l]) - (abs(i - k) + abs(j - l)+1) < 0) continue; ans = min(ans, max(g[i][j], g[k][l]) - min(g[i][j], g[k][l]) - (abs(i - k) + abs(j - l)+1)); } } } } } cout << ans; return false; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...