제출 #562835

#제출 시각아이디문제언어결과실행 시간메모리
562835elazarkorenAliens (IOI16_aliens)C++17
100 / 100
1157 ms25900 KiB
#include "aliens.h" #include <bits/stdc++.h> #define x first #define y second #define all(v) v.begin(), v.end() #define chkmin(a, b) a = min(a, b) #define chkmax(a, b) a = max(a, b) //#define int ll using namespace std; //typedef __int128_t ll; typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<ll, ll> pii; typedef vector<pii> vii; const int MAX_N = 1e5 + 5; const ll infinity = 1e18; struct Rmq { vi loga; vvi rmq_min, rmq_max; Rmq() {} Rmq(vi &v_min, vi &v_max) { int n = v_min.size(); loga.resize(n + 1); for (int i = 2; i <= n; i++) loga[i] = loga[i >> 1] + 1; rmq_min.resize(loga[n] + 1, vi(n)); rmq_max.resize(loga[n] + 1, vi(n)); rmq_min[0] = v_min, rmq_max[0] = v_max; for (int i = 1; i <= loga[n]; i++) { for (int j = 0; j < n; j++) { rmq_min[i][j] = min(rmq_min[i - 1][j], rmq_min[i - 1][min(j + (1 << (i - 1)), n - 1)]); rmq_max[i][j] = max(rmq_max[i - 1][j], rmq_max[i - 1][min(j + (1 << (i - 1)), n - 1)]); } } } inline pii query(int l, int r) { if (r <= l) return {-1, -1}; int x = min(rmq_min[loga[r - l]][l], rmq_min[loga[r - l]][r - (1 << loga[r - l])]); int y = max(rmq_max[loga[r - l]][l], rmq_max[loga[r - l]][r - (1 << loga[r - l])]); return {x, y}; } }; pii dp[MAX_N]; int opt[MAX_N]; int ind[MAX_N]; int r[MAX_N], c[MAX_N]; Rmq rmq; bitset<MAX_N> inside; int n, m; ll Cost(int j, int i) { auto [left, right] = rmq.query(j, i + 1); ll x = ll(right - left) * (right - left); int pre = rmq.query(0, j).y; if (pre > left) { x -= ll(pre - left) * (pre - left); } return x; } pii Solve(ll lambda) { vector<pair<pii, int>> ranges; ranges.push_back({{0, n}, 0}); dp[0] = {lambda + Cost(0, 0), 1}; for (int i = 1; i < n; i++) { while (!ranges.empty()) { if (ranges.back().x.y <= i) { ranges.clear(); break; } int index = max<int>(ranges.back().x.x, i); int op = ranges.back().y; ll x1 = (i ? dp[i - 1].x : 0) + Cost(i, index) + lambda; ll cnt1 = i ? dp[i - 1].y : 0; ll x2 = (op ? dp[op - 1].x : 0) + Cost(op, index) + lambda; ll cnt2 = op ? dp[op - 1].y : 0; if (pii(x1, cnt1) >= pii(x2, cnt2)) break; ranges.pop_back(); } if (ranges.empty()) { ranges.push_back({{i, n}, i}); } else { auto [left, right] = ranges.back().x; chkmax(left, ll(i)); int begin = left, end = right, mid; int op = ranges.back().y; while (begin < end) { mid = (begin + end) >> 1; ll x1 = (i ? dp[i - 1].x : 0) + Cost(i, mid) + lambda; ll cnt1 = (i ? dp[i - 1].y : 0) + 1; ll x2 = (op ? dp[op - 1].x : 0) + Cost(op, mid) + lambda; ll cnt2 = (op ? dp[op - 1].y : 0) + 1; if (pii(x1, cnt1) < pii(x2, cnt2)) end = mid; else begin = mid + 1; } ranges.pop_back(); ranges.push_back({{left, end}, op}); if (end != n) ranges.push_back({{end, n}, i}); } // dp[i] = {infinity, infinity}; // int op = i ? opt[i - 1] : 0; // for (int l = i; l >= op; l--) { // ll x = (l ? dp[l - 1].x : 0) + Cost(l, i) + lambda; // ll cnt = l ? dp[l - 1].y : 0; // pii p = {x, cnt + 1}; // if (p < dp[i]) { // dp[i] = p; // opt[i] = l; // } // } auto it = upper_bound(all(ranges), pair<pii, int>({i, n}, n)); it--; int l = it->y; dp[i].x = (l ? dp[l - 1].x : 0) + Cost(l, i) + lambda; dp[i].y = (l ? dp[l - 1].y : 0) + 1; // if (opt[i] != l) { // cout << ""; // } } return dp[n - 1]; } long long take_photos(int32_t N, int32_t M, int32_t k, std::vector<int32_t> R, std::vector<int32_t> C) { n = N, m = M; for (int i = 0; i < n; i++) { r[i] = R[i], c[i] = C[i]; if (r[i] > c[i]) swap(r[i], c[i]); } iota(ind, ind + n, 0); sort(ind, ind + n, [&] (int i, int j) { return r[i] == r[j] ? c[i] < c[j] : r[i] < r[j]; }); int max_c = 0; for (int i = 0; i < n; i++) { chkmax(max_c, c[ind[i]]); if (max_c > c[ind[i]]) { inside[ind[i]] = true; r[ind[i]] = m + 1, c[ind[i]] = m + 1; } } sort(ind, ind + n, [&] (int i, int j) { return r[i] + c[i] < r[j] + c[j]; }); n -= inside.count(); vi v_min(n), v_max(n); for (int i = 0; i < n; i++) v_min[i] = r[ind[i]], v_max[i] = c[ind[i]] + 1; rmq = Rmq(v_min, v_max); pii p = Solve(0); if (p.y <= k) return p.x; ll begin = 0, end = ll(m) * m + 5, mid; while (begin < end) { mid = (begin + end) >> 1; p = Solve(mid); if (p.y <= k) end = mid; else begin = mid + 1; } return Solve(end).x - k * (end); } //6 7 2 //0 3 //4 4 //4 6 //4 5 //4 6 //0 5 //4 10 2 //0 0 //4 4 //5 5 //9 9
#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...