답안 #864325

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
864325 2023-10-22T12:28:27 Z Dec0Dedd The Kingdom of JOIOI (JOI17_joioi) C++14
60 / 100
4000 ms 86028 KB
#include <bits/stdc++.h>
 
using namespace std;

#pragma GCC optimize("Ofast")
 
#define sh short
#define pp pair<sh, sh>
 
const int N = 2e3+110;
const int INF = 1e9;
 
int a[N][N], cnt[N], h, w;
vector<int> v;
 
void fs(int &number) {
    register int c;
    number = 0;
    c = getchar();
    for (; (c>47 && c<58); c=getchar()) number = number *10 + c - 48;
}
 
bool cmp(sh x, sh y) {
    return a[cnt[x]+1][x] > a[cnt[y]+1][y];
}
 
deque<pp> tmp;
 
int solve() {
    memset(cnt, 0, sizeof(cnt));
 
    int res=INF, lis=INF, uis=-1;
    deque<pp> q=tmp;
 
    priority_queue<sh, vector<sh>, function<bool(sh, sh)>> pq(cmp);
    pq.push(w);
 
    for (int k=0; k<(int)v.size(); ++k) {
        while (!pq.empty()) {
            if (a[cnt[pq.top()]+1][pq.top()] <= v[k]) {
                sh x=pq.top(); pq.pop();
                int val=a[cnt[x]+1][x];
                lis=min(lis, val), uis=max(uis, val);
 
                ++cnt[x];
                if (cnt[x] == h) continue;
                if (x == w || cnt[x+1] >= cnt[x]+1) pq.push(x);
 
                if (x == 1) continue;
                if (cnt[x] == cnt[x-1]+1) pq.push(x-1);
            } else break;
        }
 
        while (!q.empty()) {
            int i=q.front().first, j=q.front().second;
            if (i <= cnt[j]) q.pop_front();
            else break;
        }
 
        while (!q.empty()) {
            int i=q.back().first, j=q.back().second;
            if (i <= cnt[j]) q.pop_back();
            else break;
        }
 
        int lf;
        if (q.empty()) lf=INF;
        else {
            int x=a[q.back().first][q.back().second];
            int y=a[q.front().first][q.front().second];
            lf=x-y;
        }
 
        res=min(res, max(lf, uis-lis));
    }
 
    return res;
}
 
void hor() {
    for (int i=1; i<h/2+1; ++i) {
        for (int j=1; j<=w; ++j) swap(a[i][j], a[h-i+1][j]);
    }
}
 
void ver() {
    for (int i=1; i<=h; ++i) {
        for (int j=1; j<w/2+1; ++j) swap(a[i][j], a[i][w-j+1]);
    }
}
 
bool cmpx(pp x, pp y) {
    return a[x.first][x.second] < a[y.first][y.second];
}
 
void filltmp() {
    sort(tmp.begin(), tmp.end(), cmpx);
} 
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    fs(h), fs(w);
 
    for (int i=1; i<=h; ++i) {
        for (int j=1; j<=w; ++j) {
            fs(a[i][j]);
            v.push_back(a[i][j]);
        }
    } sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());

    for (int i=1; i<=h; ++i) {
        for (int j=1; j<=w; ++j) tmp.push_back({i, j});
    }
 
    filltmp();
    int ans=solve();
 
    hor();
    filltmp();
    ans=min(ans, solve()); 
    hor();
 
    ver();
    filltmp();
    ans=min(ans, solve());
    hor(); 
 
    filltmp();
    ans=min(ans, solve());
    
    printf("%d\n", ans);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 600 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 600 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 2 ms 624 KB Output is correct
16 Correct 31 ms 3128 KB Output is correct
17 Correct 36 ms 3296 KB Output is correct
18 Correct 35 ms 3172 KB Output is correct
19 Correct 36 ms 3344 KB Output is correct
20 Correct 32 ms 3296 KB Output is correct
21 Correct 40 ms 3296 KB Output is correct
22 Correct 43 ms 3492 KB Output is correct
23 Correct 37 ms 3496 KB Output is correct
24 Correct 32 ms 3296 KB Output is correct
25 Correct 37 ms 3332 KB Output is correct
26 Correct 37 ms 3384 KB Output is correct
27 Correct 37 ms 3316 KB Output is correct
28 Correct 38 ms 3364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 600 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 2 ms 624 KB Output is correct
16 Correct 31 ms 3128 KB Output is correct
17 Correct 36 ms 3296 KB Output is correct
18 Correct 35 ms 3172 KB Output is correct
19 Correct 36 ms 3344 KB Output is correct
20 Correct 32 ms 3296 KB Output is correct
21 Correct 40 ms 3296 KB Output is correct
22 Correct 43 ms 3492 KB Output is correct
23 Correct 37 ms 3496 KB Output is correct
24 Correct 32 ms 3296 KB Output is correct
25 Correct 37 ms 3332 KB Output is correct
26 Correct 37 ms 3384 KB Output is correct
27 Correct 37 ms 3316 KB Output is correct
28 Correct 38 ms 3364 KB Output is correct
29 Execution timed out 4070 ms 86028 KB Time limit exceeded
30 Halted 0 ms 0 KB -