Submission #724441

#TimeUsernameProblemLanguageResultExecution timeMemory
724441PetyAliens (IOI16_aliens)C++14
100 / 100
281 ms13388 KiB
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; vector<pair<long long, long long>>p, p2; pair<long long, int>dp[100002]; struct line { long long a, b; int cnt; line (long long _a, long long _b, int _cnt): a(_a), b(_b), cnt(_cnt) {}; long long eval (long long x) { return a * x + b; } }; long long inter (line x, line y) { return (y.b - x.b) / (x.a - y.a); } struct CHT { vector<line>q; vector<long long>point; void add(line L) { while (q.size() >= 2 && inter(L, q.back()) <= inter(q.back(), q.end()[-2])) { q.pop_back(); point.pop_back(); } q.push_back(L); if (q.size() >= 2) point.push_back(inter(q.end()[-1], q.end()[-2])); } pair<long long, int> get (long long x) { int st = 0, dr = point.size() - 1, ans = point.size(); while (st <= dr) { int mid = (st + dr) / 2; if (x <= point[mid]) { dr = mid - 1; ans = mid; } else st = mid + 1; } return {q[ans].eval(x) + x * x, q[ans].cnt + 1}; } }; long long lol (int i) { long long x = max(0ll, p[i].second - p[i + 1].first + 1); return x * x; } void solve (int n, long long cost) { CHT C; dp[0] = {0, 0}; C.add(line(2 - 2 * p[1].first, p[1].first * p[1].first - 2 * p[1].first + cost + 1, 0)); for (int i = 1; i <= n; i++) { dp[i] = C.get(p[i].second); if (i < n) C.add(line(2 - 2 * p[i + 1].first, dp[i].first + p[i + 1].first * p[i + 1].first - 2 * p[i + 1].first + cost + 1 - lol(i), dp[i].second)); } } long long take_photos(int n, int m, int k, vector<int>r, vector<int> c) { for (int i = 0; i < n; i++) { if (r[i] > c[i]) swap(r[i], c[i]); p2.push_back({r[i], -c[i]}); } sort(p2.begin(), p2.end()); p.push_back({-1, -1}); long long mx = -1; for (int i = 0; i < n; i++) { if (-p2[i].second > mx) p.push_back({p2[i].first, -p2[i].second}); mx = max(mx, -p2[i].second); } n = p.size() - 1; k = min(k, n); long long st = 0, dr = INF, answer; while (st <= dr) { long long mid = (st + dr) / 2; solve(n, mid); if (dp[n].second <= k) { answer = dp[n].first - mid * k; dr = mid - 1; } else st = mid + 1; } return answer; } // int main() { // int n, m, k; // assert(3 == scanf("%d %d %d", &n, &m, &k)); // std::vector<int> r(n), c(n); // for (int i = 0; i < n; i++) { // assert(2 == scanf("%d %d", &r[i], &c[i])); // } // long long ans = take_photos(n, m, k, r, c); // printf("%lld\n", ans); // return 0; // } /** 5 7 2 0 3 4 4 4 6 4 5 4 6 */

Compilation message (stderr)

aliens.cpp: In function 'long long int take_photos(int, int, int, std::vector<int>, std::vector<int>)':
aliens.cpp:95:10: warning: 'answer' may be used uninitialized in this function [-Wmaybe-uninitialized]
   95 |   return answer;
      |          ^~~~~~
#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...