답안 #298668

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
298668 2020-09-13T18:25:39 Z jainbot27 The Kingdom of JOIOI (JOI17_joioi) C++14
100 / 100
641 ms 16120 KB
#pragma GCC optimize("Ofast")
#pragma GCC target("sse4,avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;


#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ts to_string
#define ub upper_bound
#define lb lower_bound
#define ar array
#define all(x) x.begin(), x.end()
#define siz(x) (int)x.size()
#define FOR(x, y, z) for(int x = (y); x < (z); x++)
#define ROF(x, y, z) for(int x = (y-1); x >= (z); x--)
#define F0R(x, z) FOR(x, 0, z)
#define R0F(x, z) ROF(x, z, 0)
#define trav(x, y) for(auto&x:y)

const string nl = "\n";
using ll = int64_t;
using vi = vector<int>;
using vl = vector<ll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using str = string;
using vpii = vector<pii>;
using vpll = vector<pll>;


template<class T> inline bool ckmin(T&a, T b) {return b < a ? a = b, 1 : 0;}
template<class T> inline bool ckmax(T&a, T b) {return b > a ? a = b, 1 : 0;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void usaco(string x=""){
    ios_base::sync_with_stdio(0); cin.tie(0); x=x;
#ifndef D
    if(siz(x)){
        freopen((x+".in").c_str(), "r", stdin);
        freopen((x+".out").c_str(), "w", stdout);
    }
#endif
}


str ts(char c) { return str(1,c); }
str ts(bool b) { return b ? "true" : "false"; }
str ts(const char* s) { return (str)s; }
str ts(str s) { return s; }
template<class A> str ts(complex<A> c) { 
    stringstream ss; ss << c; return ss.str(); }
str ts(vector<bool> v) { 
    str res = "{"; for(int i = 0;i < (int)v.size(); i++) res += char('0'+v[i]);
    res += "}"; return res; }
template<size_t SZ> str ts(bitset<SZ> b) {
    str res = ""; for(int i = 0; i < b.size(); i++) res += char('0'+b[i]);
    return res; }
template<class A, class B> str ts(pair<A,B> p);
template<class T> str ts(T v) { 
    bool fst = 1; str res = "{";
    for (const auto& x: v) {
        if (!fst) res += ", ";
        fst = 0; res += ts(x);
    }
    res += "}"; return res;
}
template<class A, class B> str ts(pair<A,B> p) {
    return "("+ts(p.f)+", "+ts(p.s)+")"; }
void DBG() { cerr << "]" << endl; }
template<class H, class... T> void DBG(H h, T... t) {
    cerr << ts(h); if (sizeof...(t)) cerr << ", ";
    DBG(t...); }


#ifdef D 
#define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif

int h, w, a[2001][2001], mx, mn = 1e9 + 1;
void fr(){
    F0R(i, h){
        F0R(j, w/2){
            swap(a[i][j], a[i][w-j-1]);
        }
    }
}
void fc(){
    F0R(i, h/2){
        F0R(j, w){
            swap(a[i][j], a[h-i-1][j]);
        }
    }
}
bool works(int dif){
    int u = w;
    F0R(i, h){
        int j=0;
        for(; j < u; j++){
            if(mn + dif < a[i][j])
                break;
        }
        u = j;
        for(j = u; j < w; j++)
            if(mx - dif > a[i][j])
                return 0;
    }
    return 1;
}
signed main(){
    usaco("");
    cin >> h >> w;
    F0R(i, h) F0R(j, w) cin >> a[i][j], ckmax(mx, a[i][j]), ckmin(mn, a[i][j]);
    int l = 0, r = mx, res = 2e9;
    while(l <= r){
        int m = (l + r)/2;
        if(works(m)){
            r = m - 1;
        }
        else{
            l = m + 1;
        }
    }
    ckmin(res, l);
    fc();
    l = 0,r =mx;
    while(l <= r){
        int m = (l + r)/2;
        if(works(m)){
            r = m - 1;
        }
        else{
            l = m + 1;
        }
    }
    ckmin(res, l);
    fc(); 
    fr();
    l = 0,r =mx;
    while(l <= r){
        int m = (l + r)/2;
        if(works(m)){
            r = m - 1;
        }
        else{
            l = m + 1;
        }
    }
    ckmin(res, l);
    l = 0,r =mx;
    fc();
    while(l <= r){
        int m = (l + r)/2;
        if(works(m)){
            r = m - 1;
        }
        else{
            l = m + 1;
        }
    }
    ckmin(res, l);
    cout << res << nl;

}
/*
2 2
2 3
4 5
*/
/*
 * Stuff I should try:
 * Is it Monotonic? -> Binary Search
 * Is N small? -> Bitsets or other exponential
 * Edge Cases? Look Carefully 
 * Can't figure anything out? Brute Force and see patterns
 * Try to prove greedies
 * Prefix Sums, Amortization, DP, Data Structures
*/

Compilation message

joioi.cpp: In function 'void usaco(std::string)':
joioi.cpp:41:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   41 |         freopen((x+".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
joioi.cpp:42:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   42 |         freopen((x+".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 4 ms 1408 KB Output is correct
17 Correct 7 ms 1536 KB Output is correct
18 Correct 7 ms 1536 KB Output is correct
19 Correct 7 ms 1536 KB Output is correct
20 Correct 6 ms 1408 KB Output is correct
21 Correct 8 ms 1664 KB Output is correct
22 Correct 8 ms 1664 KB Output is correct
23 Correct 9 ms 1664 KB Output is correct
24 Correct 7 ms 1536 KB Output is correct
25 Correct 9 ms 1664 KB Output is correct
26 Correct 8 ms 1696 KB Output is correct
27 Correct 8 ms 1664 KB Output is correct
28 Correct 8 ms 1664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 4 ms 1408 KB Output is correct
17 Correct 7 ms 1536 KB Output is correct
18 Correct 7 ms 1536 KB Output is correct
19 Correct 7 ms 1536 KB Output is correct
20 Correct 6 ms 1408 KB Output is correct
21 Correct 8 ms 1664 KB Output is correct
22 Correct 8 ms 1664 KB Output is correct
23 Correct 9 ms 1664 KB Output is correct
24 Correct 7 ms 1536 KB Output is correct
25 Correct 9 ms 1664 KB Output is correct
26 Correct 8 ms 1696 KB Output is correct
27 Correct 8 ms 1664 KB Output is correct
28 Correct 8 ms 1664 KB Output is correct
29 Correct 438 ms 15460 KB Output is correct
30 Correct 421 ms 15992 KB Output is correct
31 Correct 449 ms 15952 KB Output is correct
32 Correct 444 ms 16020 KB Output is correct
33 Correct 397 ms 14200 KB Output is correct
34 Correct 450 ms 16120 KB Output is correct
35 Correct 633 ms 16084 KB Output is correct
36 Correct 571 ms 16120 KB Output is correct
37 Correct 641 ms 15964 KB Output is correct