結果

問題 No.96 圏外です。
ユーザー t98slidert98slider
提出日時 2022-06-02 02:30:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,318 ms / 5,000 ms
コード長 4,759 bytes
コンパイル時間 2,137 ms
コンパイル使用メモリ 187,452 KB
実行使用メモリ 11,812 KB
最終ジャッジ日時 2023-10-21 01:04:50
合計ジャッジ時間 19,174 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 20 ms
4,348 KB
testcase_05 AC 32 ms
4,348 KB
testcase_06 AC 53 ms
4,348 KB
testcase_07 AC 83 ms
4,348 KB
testcase_08 AC 121 ms
4,588 KB
testcase_09 AC 171 ms
4,948 KB
testcase_10 AC 243 ms
5,212 KB
testcase_11 AC 325 ms
5,832 KB
testcase_12 AC 401 ms
6,576 KB
testcase_13 AC 563 ms
6,560 KB
testcase_14 AC 665 ms
7,852 KB
testcase_15 AC 870 ms
7,852 KB
testcase_16 AC 1,010 ms
9,964 KB
testcase_17 AC 1,190 ms
11,812 KB
testcase_18 AC 1,269 ms
11,020 KB
testcase_19 AC 1,294 ms
11,020 KB
testcase_20 AC 1,315 ms
9,436 KB
testcase_21 AC 933 ms
8,892 KB
testcase_22 AC 1,265 ms
9,520 KB
testcase_23 AC 1,318 ms
9,520 KB
testcase_24 AC 1 ms
4,348 KB
testcase_25 AC 797 ms
9,436 KB
testcase_26 AC 1,068 ms
11,020 KB
testcase_27 AC 894 ms
9,964 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
 
struct dsu {
  public:
    dsu() : _n(0) {}
    dsu(int n) : _n(n), parent_or_size(n, -1) {}

    int merge(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        int x = leader(a), y = leader(b);
        if (x == y) return x;
        if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);
        parent_or_size[x] += parent_or_size[y];
        parent_or_size[y] = x;
        return x;
    }

    bool same(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        return leader(a) == leader(b);
    }

    int leader(int a) {
        assert(0 <= a && a < _n);
        if (parent_or_size[a] < 0) return a;
        return parent_or_size[a] = leader(parent_or_size[a]);
    }

    int size(int a) {
        assert(0 <= a && a < _n);
        return -parent_or_size[leader(a)];
    }

    std::vector<std::vector<int>> groups() {
        std::vector<int> leader_buf(_n), group_size(_n);
        for (int i = 0; i < _n; i++) {
            leader_buf[i] = leader(i);
            group_size[leader_buf[i]]++;
        }
        std::vector<std::vector<int>> result(_n);
        for (int i = 0; i < _n; i++) {
            result[i].reserve(group_size[i]);
        }
        for (int i = 0; i < _n; i++) {
            result[leader_buf[i]].push_back(i);
        }
        result.erase(
            std::remove_if(result.begin(), result.end(),
                           [&](const std::vector<int>& v) { return v.empty(); }),
            result.end());
        return result;
    }

  private:
    int _n;
    // root node: -1 * component size
    // otherwise: parent
    std::vector<int> parent_or_size;
};

vector<pair<int,int>> Totsuho(vector<pair<int,int>> &pos){
    int n=pos.size(),n1,n2;
    sort(pos.begin(),pos.end());
    vector<pair<int,int>> res1={pos[0],pos[1]},res2={pos[0],pos[1]};
    auto cross=[&](pair<int,int> a,pair<int,int> b){
        return (long long)a.first*b.second-(long long)a.second*b.first;
    };
    auto sub=[&](pair<int,int> a,pair<int,int> b){
        return make_pair(a.first-b.first,a.second-b.second);
    };
    for(int i=2;i<n;i++){
        n1=res1.size(),n2=res2.size();
        while(n1>=2&&cross(sub(res1[n1-1],res1[n1-2]),sub(pos[i],res1[n1-1]))<=0){
            res1.pop_back();
            n1--;
        }
        while(n2>=2&&cross(sub(res2[n2-1],res2[n2-2]),sub(pos[i],res2[n2-1]))>=0){
            res2.pop_back();
            n2--;
        }
        res1.push_back(pos[i]);
        res2.push_back(pos[i]);
    }
    for (int i=res2.size()-2;i>=1;i--)res1.push_back(res2[i]);
    return res1;
}

double Calipers(vector<pair<int,int>> &tot){
    int n = tot.size();
    auto dist = [&](int i, int j){
        long long dy = tot[i].first - tot[j].first;
        long long dx = tot[i].second - tot[j].second;
        return dy * dy + dx * dx;
    };
    auto cross=[&](pair<int,int> a,pair<int,int> b){
        return (long long)a.first*b.second-(long long)a.second*b.first;
    };
    auto sub=[&](pair<int,int> a,pair<int,int> b){
        return make_pair(a.first-b.first,a.second-b.second);
    };
    if(n == 1){
        return 0;
    }
    if(n == 2){
        return sqrt(dist(0, 1));
    }
    double ans = 0;
    int i = 0, j = 0;
    for(int k = 0; k < n; k++){
        if(tot[k].first < tot[i].first)i = k;
        if(tot[k].first > tot[j].first)j = k;
    }
    int si = i, sj = j;
    int cnt = 0;
    while(i != sj || j != si){
        cnt ++;
        ans = max(ans, sqrt(dist(i, j)));
        int i2 = i + 1 == n ? 0 : i + 1, j2 = j + 1 == n ? 0 : j + 1;
        if(cross(sub(tot[i2],tot[i]),sub(tot[j2],tot[j])) < 0){
            i = i2;
        }else{
            j = j2;
        }
        if(cnt >= 2*n)break;
    }
    return ans;
}

int main(){
    int n, y, x;
    cin >> n;
    vector<pair<int,int>> a(n);
    for(int i = 0; i < n; i++){
        cin >> a[i].first >> a[i].second;
    }
    sort(a.begin(), a.end());
    dsu uf(n);
    for(int i = 0; i < n; i++){
        tie(y, x) = a[i];
        for(int dy = -10; dy <= 10; dy++){
            for(int dx = -10; dx <= 10; dx++){
                if(dy * dy + dx * dx > 100)continue;
                auto p = make_pair(y + dy, x + dx);
                int j = lower_bound(a.begin(), a.end(), p) - a.begin();
                if(j < n && a[j] == p){
                    uf.merge(i, j);
                }
            }
        }
    }
    double ans = 1 + (n != 0);
    auto G = uf.groups();
    for(auto &&b:G){
        if(b.size() == 1)continue;
        vector<pair<int,int>> c;
        for(auto i:b)c.push_back(a[i]);
        auto tot = Totsuho(c);
        ans = max(ans, 2 + Calipers(tot));
    }
    printf("%.15lf\n",ans);
}
0