Submission #731639

#TimeUsernameProblemLanguageResultExecution timeMemory
731639danikoynovAliens (IOI16_aliens)C++14
100 / 100
211 ms7792 KiB
#include "aliens.h" #include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 1e5 + 10; struct interval { ll left, right; interval(ll _left = 0, ll _right = 0) { left = _left; right = _right; } }; bool cmp(const interval &it1, const interval &it2) { if (it1.left == it2.left) return it1.right > it2.right; return it1.left < it2.left; } const ll inf = 1e18; interval h[maxn]; ll area(ll left, ll right) { if (left > right) return 0; return (right - left + 1) * (right - left + 1); } struct line { ll k, m; line(ll _k, ll _m) { k = _k; m = _m; } double do_math(ll x) { return k * x + m; } }; double intersect(const line &l1, const line &l2) /// when does the second line become better { if (l1.k == l2.k) { if (l1.m < l2.m) return -inf; else return inf; } /** l1.k * x + l1.m = l2.k * x + l2.m (l1.k - l2.k) * x = l2.m - l1.m */ return (double)(l2.m - l1.m) / (double)(l1.k - l2.k); } struct convex_hull_trick { vector < line > cht; void add_line(ll k, ll m) { line l(k, m); while(cht.size() > 1 && intersect(cht[cht.size() - 2], cht.back()) > intersect(cht.back(), l)) cht.pop_back(); cht.push_back(l); } ll query(ll x) { int lf = 0, rf = (int)(cht.size()) - 2; while(lf <= rf) { int mf = (lf + rf) / 2; if (intersect(cht[mf], cht[mf + 1]) < (double)(x)) lf = mf + 1; else rf = mf - 1; } return cht[lf].do_math(x); } }; pair < ll, ll > state[maxn]; int n; pair < ll, ll > solve_state(ll cost) { state[0] = {0, 0}; ///convex_hull_trick cht; line cur(- 2 * h[1].left, h[1].left * h[1].left + state[0].first - area(h[1].left, h[0].right)); deque < pair < line, int > > dq; dq.push_back({cur, 0}); for (int i = 1; i <= n; i ++) { while(dq.size() > 1 && intersect(dq[0].first, dq[1].first) < (double)(h[i].right + 1)) dq.pop_front(); state[i].first = dq.front().first.do_math(h[i].right + 1) + (h[i].right + 1) * (h[i].right + 1) + cost; state[i].second = dq.front().second + 1; cur = line(- 2 * h[i + 1].left, h[i + 1].left * h[i + 1].left + state[i].first - area(h[i + 1].left, h[i].right)); while(dq.size() > 1 && intersect(dq[dq.size() - 2].first, dq.back().first) > intersect(dq.back().first, cur)) dq.pop_back(); dq.push_back({cur, state[i].second}); /**ll best_val = 1e18; for (int j = 0; j < i; j ++) { ll val = - 2 * h[j + 1].left * (h[i].right + 1) + (h[i].right + 1) * (h[i].right + 1) + cost; val = val + (h[j + 1].left * h[j + 1].left) + state[j].first - area(h[j + 1].left, h[j].right); best_val = min(best_val, val); if (val < state[i].first) { state[i].first = val; state[i].second = state[j].second + 1; } if (val == state[i].first && state[j].second + 1 < state[i].second) { state[i].second = state[j].second + 1; } }*/ } return state[n]; ///cht.add_line(- 2 * h[1].left, h[1].left * h[i + 1].left + state[0].first, ) ///dp[i][j] = cht.query(h[i].right + 1) + (h[i].right + 1) * (h[i].right + 1); ///cht.add_line(- 2 * h[i + 1].left, h[i + 1].left * h[i + 1].left + dp[i][j - 1] - area(h[i + 1].left, h[i].right)); } ll take_photos(int N, int m, int k, vector<int> r, vector<int> c) { n = N; vector < interval > vec; for (int i = 0; i < n; i ++) { if (r[i] > c[i]) swap(r[i], c[i]); interval cur(r[i], c[i]); vec.push_back(cur); } sort(vec.begin(), vec.end(), cmp); n = 0; int to = -1; for (int i = 0; i < vec.size(); i ++) { if (vec[i].right > to) { to = vec[i].right; h[++ n] = vec[i]; } } ///for (int i = 1; i <= n; i ++) /// cout << h[i].left << " : " << h[i].right << endl; k = min(k, n); h[0] = interval(-1, -1); ll lf = 0, rf = (ll)(m) * (ll)(m); while(lf <= rf) { ll mf = (lf + rf) / 2; pair < ll, ll > cur = solve_state(mf); if (cur.second > k) lf = mf + 1; else rf = mf - 1; } ll prev_cost = lf - 1, cur_cost = lf; pair < ll, ll > fp = solve_state(prev_cost), tp = solve_state(cur_cost); ll val1 = fp.first - fp.second * prev_cost, val2 = tp.first - tp.second * cur_cost; ///cout << val << endl; if (k == n) { return solve_state(0).first; } ll val = val1 + (k - fp.second) * (val2 - val1) / (tp.second - fp.second); return val; /**dp[0][0] = 0; for (int j = 1; j <= k; j ++) { convex_hull_trick cht; cht.add_line(- 2 * h[1].left, h[1].left * h[1].left + dp[0][j - 1] - area(h[1].left, h[0].right)); for (int i = 1; i <= n; i ++) { dp[i][j] = cht.query(h[i].right + 1) + (h[i].right + 1) * (h[i].right + 1); cht.add_line(- 2 * h[i + 1].left, h[i + 1].left * h[i + 1].left + dp[i][j - 1] - area(h[i + 1].left, h[i].right)); } }*/ /**for (int i = 1; i <= n; i ++, cout << endl) for (int j = 1; j <= k; j ++) cout << dp[i][j] << " ";*/ ///return dp[n][k]; }

Compilation message (stderr)

aliens.cpp: In function 'll take_photos(int, int, int, std::vector<int>, std::vector<int>)':
aliens.cpp:162:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<interval>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  162 |     for (int i = 0; i < vec.size(); i ++)
      |                     ~~^~~~~~~~~~~~
#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...