結果
問題 | No.703 ゴミ拾い Easy |
ユーザー | はまやんはまやん |
提出日時 | 2018-06-16 01:10:22 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 122 ms / 1,500 ms |
コード長 | 6,736 bytes |
コンパイル時間 | 1,503 ms |
コンパイル使用メモリ | 175,184 KB |
実行使用メモリ | 12,928 KB |
最終ジャッジ日時 | 2024-06-10 18:30:44 |
合計ジャッジ時間 | 5,499 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 1 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 2 ms
5,376 KB |
testcase_05 | AC | 2 ms
5,376 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 1 ms
5,376 KB |
testcase_08 | AC | 2 ms
5,376 KB |
testcase_09 | AC | 1 ms
5,376 KB |
testcase_10 | AC | 2 ms
5,376 KB |
testcase_11 | AC | 1 ms
5,376 KB |
testcase_12 | AC | 2 ms
5,376 KB |
testcase_13 | AC | 2 ms
5,376 KB |
testcase_14 | AC | 2 ms
5,376 KB |
testcase_15 | AC | 2 ms
5,376 KB |
testcase_16 | AC | 2 ms
5,376 KB |
testcase_17 | AC | 2 ms
5,376 KB |
testcase_18 | AC | 2 ms
5,376 KB |
testcase_19 | AC | 2 ms
5,376 KB |
testcase_20 | AC | 1 ms
5,376 KB |
testcase_21 | AC | 2 ms
5,376 KB |
testcase_22 | AC | 2 ms
5,376 KB |
testcase_23 | AC | 2 ms
5,376 KB |
testcase_24 | AC | 119 ms
12,800 KB |
testcase_25 | AC | 119 ms
12,916 KB |
testcase_26 | AC | 119 ms
12,792 KB |
testcase_27 | AC | 120 ms
12,832 KB |
testcase_28 | AC | 122 ms
12,800 KB |
testcase_29 | AC | 119 ms
12,928 KB |
testcase_30 | AC | 121 ms
12,768 KB |
testcase_31 | AC | 119 ms
12,784 KB |
testcase_32 | AC | 119 ms
12,904 KB |
testcase_33 | AC | 119 ms
12,896 KB |
testcase_34 | AC | 67 ms
12,800 KB |
testcase_35 | AC | 67 ms
12,720 KB |
testcase_36 | AC | 68 ms
12,800 KB |
testcase_37 | AC | 69 ms
12,800 KB |
testcase_38 | AC | 66 ms
12,788 KB |
testcase_39 | AC | 69 ms
12,720 KB |
testcase_40 | AC | 67 ms
12,800 KB |
testcase_41 | AC | 67 ms
12,768 KB |
testcase_42 | AC | 67 ms
12,800 KB |
testcase_43 | AC | 66 ms
12,672 KB |
testcase_44 | AC | 1 ms
5,376 KB |
testcase_45 | AC | 1 ms
5,376 KB |
testcase_46 | AC | 79 ms
12,788 KB |
testcase_47 | AC | 78 ms
12,864 KB |
testcase_48 | AC | 2 ms
5,376 KB |
testcase_49 | AC | 3 ms
5,376 KB |
コンパイルメッセージ
main.cpp: In member function 'bool ConvexHullDynamic::Line::operator<(const ConvexHullDynamic::Line&) const': main.cpp:50:9: warning: control reaches end of non-void function [-Wreturn-type] 50 | } | ^
ソースコード
#include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<b;i++) #define rrep(i,a,b) for(int i=a;i>=b;i--) #define fore(i,a) for(auto &i:a) #define all(x) (x).begin(),(x).end() #pragma GCC optimize ("-O3") using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); } typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60; template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; } //--------------------------------------------------------------------------------------------------- class ConvexHullDynamic { typedef long long coef_t; typedef long long coord_t; typedef long long val_t; /* * Line 'y=a*x+b' represented by 2 coefficients 'a' and 'b' * and 'xLeft' which is intersection with previous line in hull(first line has -INF) */ private: struct Line { coef_t a, b; double xLeft; enum Type { line, maxQuery, minQuery } type; coord_t val; explicit Line(coef_t aa = 0, coef_t bb = 0) : a(aa), b(bb), xLeft(-INFINITY), type(Type::line), val(0) {} val_t valueAt(coord_t x) const { return a * x + b; } friend bool areParallel(const Line &l1, const Line &l2) { return l1.a == l2.a; } friend double intersectX(const Line &l1, const Line &l2) { return areParallel(l1, l2) ? INFINITY : 1.0 * (l2.b - l1.b) / (l1.a - l2.a); } bool operator<(const Line &l2) const { if (l2.type == line) return this->a < l2.a; if (l2.type == maxQuery) return this->xLeft < l2.val; if (l2.type == minQuery) return this->xLeft > l2.val; } }; bool isMax; //whether or not saved envelope is top(search of max value) public: std::set< Line > hull; //envelope itself private: /* * INFO: Check position in hull by iterator * COMPLEXITY: O(1) */ bool hasPrev(std::set< Line >::iterator it) { return it != hull.begin(); } bool hasNext(std::set< Line >::iterator it) { return it != hull.end() && std::next(it) != hull.end(); } /* * INFO: Check whether line l2 is irrelevant * NOTE: Following positioning in hull must be true * l1 is next left to l2 * l2 is right between l1 and l3 * l3 is next right to l2 * COMPLEXITY: O(1) */ bool irrelevant(const Line &l1, const Line &l2, const Line &l3) { return intersectX(l1, l3) <= intersectX(l1, l2); } bool irrelevant(std::set< Line >::iterator it) { return hasPrev(it) && hasNext(it) && (isMax && irrelevant(*std::prev(it), *it, *std::next(it)) || !isMax && irrelevant(*std::next(it), *it, *std::prev(it))); } /* * INFO: Updates 'xValue' of line pointed by iterator 'it' * COMPLEXITY: O(1) */ std::set< Line >::iterator updateLeftBorder(std::set< Line >::iterator it) { if (isMax && !hasPrev(it) || !isMax && !hasNext(it)) return it; double val = intersectX(*it, isMax ? *std::prev(it) : *std::next(it)); Line buf(*it); it = hull.erase(it); buf.xLeft = val; it = hull.insert(it, buf); return it; } public: explicit ConvexHullDynamic(bool isMax = false) : isMax(isMax) {} /* * INFO: Adding line to the envelope * Line is of type 'y=a*x+b' represented by 2 coefficients 'a' and 'b' * COMPLEXITY: Adding N lines(N calls of function) takes O(N*log N) time */ void addLine(coef_t a, coef_t b) { //find the place where line will be inserted in set Line l3 = Line(a, b); auto it = hull.lower_bound(l3); //if parallel line is already in set, one of them becomes irrelevant if (it != hull.end() && areParallel(*it, l3)) { if (isMax && it->b < b || !isMax && it->b > b) it = hull.erase(it); else return; } //try to insert it = hull.insert(it, l3); if (irrelevant(it)) { hull.erase(it); return; } //remove lines which became irrelevant after inserting line while (hasPrev(it) && irrelevant(std::prev(it))) hull.erase(std::prev(it)); while (hasNext(it) && irrelevant(std::next(it))) hull.erase(std::next(it)); //refresh 'xLine' it = updateLeftBorder(it); if (hasPrev(it)) updateLeftBorder(std::prev(it)); if (hasNext(it)) updateLeftBorder(std::next(it)); } val_t getBest(coord_t x) const { Line q; q.val = x; q.type = isMax ? Line::Type::maxQuery : Line::Type::minQuery; auto bestLine = hull.lower_bound(q); if (isMax) --bestLine; return bestLine->valueAt(x); } }; /*--------------------------------------------------------------------------------------------------- ∧_∧ ∧_∧ (´<_` ) Welcome to My Coding Space! ( ´_ゝ`) / ⌒i / \ | | / / ̄ ̄ ̄ ̄/ | __(__ニつ/ _/ .| .|____ \/____/ (u ⊃ ---------------------------------------------------------------------------------------------------*/ int N; ll A[301010], X[301010], Y[301010]; ll dp[301010]; ConvexHullDynamic ch; //--------------------------------------------------------------------------------------------------- void _main() { cin >> N; rep(i, 1, N + 1) cin >> A[i]; rep(i, 1, N + 1) cin >> X[i]; rep(i, 1, N + 1) cin >> Y[i]; rep(i, 0, N + 1) dp[i] = infl; dp[0] = 0; /*rep(i, 1, N + 1) { ll mi = infl; rep(j, 0, i) { ll dx = A[i] - X[j + 1]; ll dy = Y[j + 1]; ll cst = dp[j] + dx * dx + dy * dy; chmin(mi, cst); } dp[i] = mi; }*/ rep(i, 1, N + 1) { ch.addLine(-2LL * X[i], X[i] * X[i] + Y[i] * Y[i] + dp[i-1]); ll mi = ch.getBest(A[i]); /*ll mi = infl; rep(j, 0, i) { // = dp[j] + (A[i] - X[j+1])^2 + Y[j+1]^2 // = dp[j] + A[i]^2 - 2*A[i]*X[j+1] + X[j+1]^2 + Y[j+1]^2 ll cst = dp[j] - 2 * X[j + 1] * A[i] + X[j + 1] * X[j + 1] + Y[j + 1] * Y[j + 1]; chmin(mi, cst); }*/ dp[i] = mi + A[i] * A[i]; } cout << dp[N] << endl; }