제출 #44379

#제출 시각아이디문제언어결과실행 시간메모리
44379szawinisAliens (IOI16_aliens)C++17
0 / 100
2 ms784 KiB
#include "aliens.h" #include <bits/stdc++.h> using ll = long long; using namespace std; inline ll square(ll x) { return x*x; } void tidy_up(int &n, vector<int> &l, vector<int> &r) { for(int i = 0; i < n; i++) if(l[i] > r[i]) swap(l[i], r[i]); vector<int> id(n); iota(id.begin(), id.end(), 0); sort(id.begin(), id.end(), [l, r] (int i, int j) { return make_pair(r[i], -l[i]) < make_pair(r[j], -l[j]); }); vector<int> tmp; tmp.push_back(-1); tmp.push_back(id[0]); for(int i = 1; i < n; i++) { while(tmp.size() > 1 && l[tmp.back()] >= l[id[i]]) tmp.pop_back(); tmp.push_back(id[i]); } n = tmp.size() - 1; id = tmp; for(int i = 1; i <= n; i++) tmp[i] = r[id[i]]; r = tmp; for(int i = 1; i <= n; i++) tmp[i] = l[id[i]]; l = tmp; } const ll is_query = -(1ll << 62); struct line { ll m, c; mutable function<const line*()> succ; bool operator<(const line& rhs) const { if(rhs.c != is_query) return m > rhs.m; const line* s = succ(); if(!s) return false; return (s->m - m < 0) ^ (c - s->c < (double) (s->m - m)*rhs.m); } }; class MinHull : public multiset<line> { bool bad(iterator it) { auto nxt = next(it); if(it == begin()) { if(nxt != end()) return nxt->m == it->m && nxt->c <= it->c; else return false; } auto pre = prev(it); if(nxt == end()) return pre->m == it->m && pre->c <= it->c; line l1 = *pre, l2 = *it, l3 = *nxt; assert(l1.m >= l2.m && l2.m >= l3.m && l1.m >= l3.m); return (double) (l3.c-l1.c)*(l1.m-l2.m) <= (double) (l2.c-l1.c)*(l1.m-l3.m); } public: void update(ll m, ll c) { auto it = insert((line) {m, c}); it->succ = [=] { return next(it) == end() ? 0 : &*next(it); }; if(bad(it)) { erase(it); return; } while(it != begin() && bad(prev(it))) erase(prev(it)); while(next(it) != end() && bad(next(it))) erase(next(it)); } ll query(ll x) { auto it = lower_bound((line) {x, is_query}); assert(it != end()); return it->m*x + it->c; } } hull; // struct cht { // struct line { // ll m, c; // line(ll m, ll c): m(m), c(c) {}; // ll getVal(ll x) { return m*x + c; } // }; // vector<line> f; // bool bad(line l1, line l2, line l3) { // m1 > m2 > m3 // return (double) (l1.c-l3.c)*(l2.m-l1.m) <= (double) (l1.c-l2.c)*(l3.m-l1.m); // } // void insert(ll m, ll c) { // line l = line(m, c); // while(f.size() >= 2 && bad(f[f.size()-2], f[f.size()-1], l)) f.pop_back(); // f.push_back(l); // } // ll query(ll x) { // static int idx = 0; // while(idx+1 < f.size() && f[idx+1].getVal(x) < f[idx].getVal(x)) ++idx; // return f[idx].getVal(x); // } // void clear() { f.clear(); } // } hull; vector<vector<ll> > dp; ll take_photos(int n, int m, int k, vector<int> l, vector<int> r) { tidy_up(n, l, r); dp = vector<vector<ll> >(k+1, vector<ll>(n+1, -1)); hull.update(-2*(l[1]-1), square(l[1]-1)); for(int j = 1; j <= k; j++) { dp[j][0] = 0; for(int i = 1; i <= n; i++) { dp[j][i] = hull.query(r[i]) + square(r[i]); } hull.clear(); for(int i = 1; i < n; i++) { hull.update(-2*(l[i+1]-1), square(l[i+1]-1) + dp[j][i] - square(max(0, r[i] - l[i+1] + 1))); } } return dp[k][n]; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...