Submission #34877

# Submission time Handle Problem Language Result Execution time Memory
34877 2017-11-16T11:20:24 Z szawinis Lightning Conductor (POI11_pio) C++14
27 / 100
1000 ms 6216 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 5e5+1;

const ll is_query = -(1LL<<62);
struct Line {
    ll m, b;
    mutable function<const Line*()> succ;
    bool operator<(const Line& rhs) const {
        if (rhs.b != is_query) return m < rhs.m;
        const Line* s = succ();
        if (!s) return 0;
        ll x = rhs.m;
        return b - s->b < (s->m - m) * x;
    }
};
struct HullDynamic : public multiset<Line> { // will maintain upper hull for maximum
    bool bad(iterator y) {
        auto z = next(y);
        if (y == begin()) {
            if (z == end()) return 0;
            return y->m == z->m && y->b <= z->b;
        }
        auto x = prev(y);
        if (z == end()) return y->m == x->m && y->b <= x->b;
        return (x->b - y->b)*(z->m - y->m) >= (y->b - z->b)*(y->m - x->m);
    }
    void update(ll m, ll b) {
        auto y = insert({ m, b });
        y->succ = [=] { return next(y) == end() ? 0 : &*next(y); };
        if (bad(y)) { erase(y); return; }
        while (next(y) != end() && bad(next(y))) erase(next(y));
        while (y != begin() && bad(prev(y))) erase(prev(y));
    }
    ll query(ll x) {
        auto l = *lower_bound((Line) { x, is_query });
        return l.m * x + l.b;
    }
} dp;

int n, h[N], res[N];

void solve() {
	dp.clear();
	dp.update(2*h[0], -1ll*h[0]*h[0]);
	int mx = h[0];
	for(int i = 1, x = 0; i < n; i++) {
		while(x < mx || 1ll*x*x - i < dp.query(x)) ++x;
		res[i] = max(x, res[i]);
		mx = max(mx, h[i]);
		dp.update(2*h[i], -1ll*h[i]*h[i] - i);
	}
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n;
	for(int i = 0; i < n; i++) cin >> h[i];
	solve();
	reverse(h, h+n);
	reverse(res, res+n);
	solve();
	reverse(h, h+n);
	reverse(res, res+n);
	// dp.update(2*h[n-1], -1ll*h[n-1]*h[n-1]);
	// mx = h[n-1];
	// for(int i = n-2, x = 0; i >= 0; i--) {
	// 	cout << i << ' ' << x << ' ' << mx << ":" << endl;
	// 	for(auto l: dp) cout << l.m*x + l.b << endl;
	// 	while(x < mx || 1ll*x*x + i < dp.query(x)) ++x;
	// 	res[i] = max(x, res[i]);
	// 	mx = max(mx, h[i]);
	// 	dp.update(2*h[i], -1ll*h[i]*h[i] + i);
	// }
	for(int i = 0; i < n; i++) cout << max(res[i] - h[i], 0) << '\n';
}
// h[j] <= x - sqrt(i - j)
// x - h[j] >= sqrt(i - j)
// x^2 - 2xh[j] + h[j]^2 >= i - j
// x^2 - i >= 2xh[j] - h[j]^2 - j
//
// x^2 - 2xh[j] + h[j]^2 >= j - i
// x^2 + i >= 2xh[j] - h[j]^2 + j
# Verdict Execution time Memory Grader output
1 Correct 0 ms 6084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 6084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 6084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 956 ms 6084 KB Execution killed because of forbidden syscall writev (20)
# Verdict Execution time Memory Grader output
1 Runtime error 969 ms 6084 KB Execution killed because of forbidden syscall writev (20)
# Verdict Execution time Memory Grader output
1 Runtime error 306 ms 6216 KB Execution killed because of forbidden syscall writev (20)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 883 ms 6084 KB Execution killed because of forbidden syscall writev (20)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1000 ms 6084 KB Execution timed out
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1000 ms 6084 KB Execution timed out
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1000 ms 6084 KB Execution timed out
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1000 ms 6084 KB Execution timed out
2 Halted 0 ms 0 KB -