結果

問題 No.2012 Largest Triangle
ユーザー SSRSSSRS
提出日時 2022-07-15 21:46:14
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 681 ms / 2,500 ms
コード長 2,524 bytes
コンパイル時間 4,320 ms
コンパイル使用メモリ 105,152 KB
実行使用メモリ 5,220 KB
最終ジャッジ日時 2023-09-10 05:44:29
合計ジャッジ時間 16,173 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,500 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 244 ms
4,376 KB
testcase_17 AC 246 ms
4,380 KB
testcase_18 AC 246 ms
4,380 KB
testcase_19 AC 242 ms
4,376 KB
testcase_20 AC 242 ms
4,380 KB
testcase_21 AC 248 ms
4,376 KB
testcase_22 AC 248 ms
4,376 KB
testcase_23 AC 242 ms
4,380 KB
testcase_24 AC 245 ms
4,376 KB
testcase_25 AC 243 ms
4,376 KB
testcase_26 AC 679 ms
4,572 KB
testcase_27 AC 674 ms
4,660 KB
testcase_28 AC 677 ms
4,380 KB
testcase_29 AC 680 ms
4,380 KB
testcase_30 AC 681 ms
4,376 KB
testcase_31 AC 381 ms
4,380 KB
testcase_32 AC 381 ms
4,376 KB
testcase_33 AC 381 ms
4,376 KB
testcase_34 AC 383 ms
4,376 KB
testcase_35 AC 382 ms
4,380 KB
testcase_36 AC 3 ms
4,380 KB
testcase_37 AC 3 ms
4,380 KB
testcase_38 AC 3 ms
4,376 KB
testcase_39 AC 3 ms
4,380 KB
testcase_40 AC 3 ms
4,376 KB
testcase_41 AC 135 ms
5,220 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//https://atcoder.jp/contests/abc244/submissions/30254721
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <vector>
#include <tuple>
using namespace std;
using ll = long long;
using Point = pair<ll, ll>;
const ll INF = LLONG_MAX / 4;
const double inv_phi = (sqrt(5) - 1) / 2;
void chmax(ll& a, ll b){ if(a < b) a = b; }

ll ccw(Point a, Point b, Point c){
    ll cross = (b.first - a.first) * (c.second - b.second) - (b.second - a.second) * (c.first - b.first);
    if(cross > 0) return 1;
    else if(cross == 0) return 0;
    else return -1;
}
struct ConvexHull{
    vector<Point> lower, upper;
    ConvexHull(Point p): lower{p}, upper{p}{}
    ConvexHull(const ConvexHull& a, const ConvexHull& b){
        vector<Point> v;
        merge(a.lower.begin(), a.lower.end(), b.lower.begin(), b.lower.end(), back_inserter(v));
        for(Point p : v){
            while(lower.size() >= 2 && ccw(lower.rbegin()[1], lower.back(), p) <= 0){
                lower.pop_back();
            }
            lower.push_back(p);
        }
        v.clear();
        merge(a.upper.begin(), a.upper.end(), b.upper.begin(), b.upper.end(), back_inserter(v));
        for(Point p : v){
            while(upper.size() >= 2 && ccw(upper.rbegin()[1], upper.back(), p) >= 0){
                upper.pop_back();
            }
            upper.push_back(p);
        }
    }
    ll get(ll A, ll B) const {
        auto& s = B < 0 ? lower : upper;
        // golden-section search
        auto f = [&](ll i){ return A * s[i].first + B * s[i].second; };
        ll l = 0, r = s.size() - 1, r2 = round(r * inv_phi), f_r2 = f(r2);
        while(abs(l - r) >= 6){
            ll l2 = r + llround((l - r) * inv_phi), f_l2 = f(l2);
            if(f_l2 < f_r2) tie(l, r) = tuple{r, l2};
            else tie(r, r2, f_r2) = tuple{r2, l2, f_l2};
        }
        ll ans = -INF;
        if(l > r) swap(l, r);
        for(ll i = l; i <= r; i++) chmax(ans, f(i));
        return ans;
    }
};
int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    ll Q;
    cin >> Q;
    vector<ConvexHull> S;
    ll ans = -INF;
    for(ll i = 1; i <= Q; i++){
        long long X, Y;
        cin >> X >> Y;
        S.emplace_back(pair{X, Y});
        for(ll j = 1; (i & j) == 0; j <<= 1){
            S.rbegin()[1] = ConvexHull(S.rbegin()[1], S.back());
            S.pop_back();
        }
        for(auto& s : S) chmax(ans, s.get(-Y, X));
        for(auto& s : S) chmax(ans, s.get(Y, -X));
    }
    cout << ans << '\n';
}
0