답안 #140793

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
140793 2019-08-05T08:06:26 Z Vlatko Pismo (COCI18_pismo) C++14
30 / 70
713 ms 3832 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

const int inf = 1e9;
const int maxn = 100010;

int n;
int a[maxn];

struct segtree {
    struct node {
        int minval, maxval;
        node() {}
        node(int x, int y) : minval(x), maxval(y) {}    
    };
    node tree[4*maxn];
    inline node merge(node a, node b) {
        return node(min(a.minval, b.minval), max(a.maxval, b.maxval));
    }
    segtree() {}
    void build(int v, int tl, int tr) {
        if (tl == tr) {
            tree[v].minval = tree[v].maxval = a[tl];
        } else {
            int tm = (tl + tr) / 2;
            build(2*v, tl, tm);
            build(2*v+1, tm+1, tr);
            tree[v] = merge(tree[2*v], tree[2*v+1]);
        }
    }
    node query(int v, int tl, int tr, int l, int r) {
        if (l > r) return node(inf, -inf);
        if (l <= tl && tr <= r) return tree[v];
        int tm = (tl + tr) / 2;
        return merge(query(2*v, tl, tm, l, min(r, tm)),
                     query(2*v+1, tm+1, tr, max(l, tm+1), r));
    }
} tree;

int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    tree.build(1, 0, n-1);
    int ans = 2*inf;
    for (int i = 0; i < n; ++i) {
        // assume a[i] is min value
        // find min l <= i so that min(a[l], a[l+1], ..., a[i]) is a[i]
        // find max r >= i so that min(a[i], a[i+1], ..., a[r]) is a[i]
        // ans: max(a[l], a[l+1], ..., a[r-1], a[r]) - a[i]
        int lo, mid, hi, l, r;
        lo = 0, hi = i-1, l = i;
        while (lo <= hi) {
            mid = (lo + hi) / 2;
            auto res = tree.query(1, 0, n-1, mid, i);
            if (res.minval == a[i]) {
                l = mid;
                hi = mid-1;
            } else {
                lo = mid+1;
            }
        }
        lo = i+1, hi = n-1, r = i;
        while (lo <= hi) {
            mid = (lo + hi) / 2;
            auto res = tree.query(1, 0, n-1, i, mid);
            if (res.minval == a[i]) {
                r = mid;
                lo = mid+1;
            } else {
                hi = mid-1;
            }
        }
        if (l < r) {
            ans = min(ans, tree.query(1, 0, n-1, l, r).maxval - a[i]);
        }
    }
    cout << ans << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 376 KB Output isn't correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 10 ms 376 KB Output is correct
4 Correct 10 ms 376 KB Output is correct
5 Incorrect 713 ms 3704 KB Output isn't correct
6 Incorrect 711 ms 3832 KB Output isn't correct
7 Incorrect 711 ms 3576 KB Output isn't correct