結果

問題 No.96 圏外です。
ユーザー 0w10w1
提出日時 2016-12-05 13:28:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 272 ms / 5,000 ms
コード長 4,647 bytes
コンパイル時間 2,193 ms
コンパイル使用メモリ 194,760 KB
実行使用メモリ 29,568 KB
最終ジャッジ日時 2024-06-06 16:27:36
合計ジャッジ時間 6,151 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 5 ms
5,376 KB
testcase_05 AC 8 ms
5,376 KB
testcase_06 AC 12 ms
5,376 KB
testcase_07 AC 18 ms
6,016 KB
testcase_08 AC 26 ms
7,040 KB
testcase_09 AC 36 ms
7,936 KB
testcase_10 AC 52 ms
8,576 KB
testcase_11 AC 64 ms
11,008 KB
testcase_12 AC 81 ms
13,440 KB
testcase_13 AC 111 ms
12,032 KB
testcase_14 AC 138 ms
17,024 KB
testcase_15 AC 170 ms
14,592 KB
testcase_16 AC 216 ms
22,400 KB
testcase_17 AC 239 ms
29,568 KB
testcase_18 AC 265 ms
25,856 KB
testcase_19 AC 272 ms
25,600 KB
testcase_20 AC 111 ms
10,228 KB
testcase_21 AC 218 ms
16,680 KB
testcase_22 AC 113 ms
6,484 KB
testcase_23 AC 119 ms
6,480 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 162 ms
22,272 KB
testcase_26 AC 215 ms
27,392 KB
testcase_27 AC 183 ms
24,064 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair< int, int > pii;
typedef vector< int > vi;
typedef vector< vi > vvi;
typedef vector< ll > vl;
typedef vector< vl > vvl;
typedef vector< pii > vp;
typedef vector< vp > vvp;
typedef vector< string > vs;
typedef vector< double > vd;
typedef vector< vd > vvd;

template< class T1, class T2 >
int upmin( T1 &x, T2 v ){
  if( x > v ){
    x = v;
    return 1;
  }
  return 0;
}

template< class T1, class T2 >
int upmax( T1 &x, T2 v ){
  if( x < v ){
    x = v;
    return 1;
  }
  return 0;
}

const int INF = 0x3f3f3f3f;

int N;
vi X, Y;

void init(){
  cin >> N;
  X = Y = vi( N );
  for( int i = 0; i < N; ++i )
    cin >> X[ i ] >> Y[ i ];
}

struct dsu{
  vi fa;
  dsu( int sz ){
    fa = vi( sz );
    for( int i = 0; i < sz; ++i )
      fa[ i ] = i;
  }
  int find( int x ){
    return fa[ x ] == x ? x : fa[ x ] = find( fa[ x ] );
  }
  int merge( int a, int b ){
    int x = find( a );
    int y = find( b );
    if( x == y )
      return 0;
    fa[ x ] = y;
    return 1;
  }
};

map< int, vi > group;

int eu_dis2( int a, int b, int x, int y ){
  int dx = a - x;
  int dy = b - y;
  return dx * dx + dy * dy;
}

double eu_dis( int a, int b, int x, int y ){
  double dx = a - x;
  double dy = b - y;
  return sqrt( dx * dx + dy * dy ); 
}

void preprocess(){
  map< pii, vi > box;
  for( int i = 0; i < N; ++i )
    box[ make_pair( ( X[ i ] + 10000 ) /10, ( Y[ i ] + 10000 ) / 10 ) ].emplace_back( i );
  dsu *uf = new dsu( N );
  for( auto it = box.begin(); it != box.end(); ++it ){
    for( int i = 0; i < ( it->second ).size(); ++i )
      for( int j = i + 1; j < ( it->second ).size(); ++j )
        if( eu_dis2( X[ ( it->second )[ i ] ], Y[ ( it->second )[ i ] ], X[ ( it->second )[ j ] ], Y[ ( it->second )[ j ] ] ) <= 100 )
          uf->merge( ( it->second )[ i ], ( it->second )[ j ] );
    static const int dx[] = { 0, 1, 0, -1, 1, 1, -1, -1 };
    static const int dy[] = { 1, 0, -1, 0, 1, -1, 1, -1 };
    for( int di = 0; di < 8; ++di ){
      pii key = make_pair( ( it->first ).first + dx[ di ], ( it->first ).second + dy[ di ] );
      if( not box.count( key ) ) continue;
      auto &cur = it->second;
      auto &adj = box[ key ];
      for( auto &u : cur )
        for( auto &v : adj )
          if( eu_dis2( X[ u ], Y[ u ], X[ v ], Y[ v ] ) <= 100 )
            uf->merge( u, v );
    }
  }
  for( int i = 0; i < N; ++i )
    group[ uf->find( i ) ].emplace_back( i );
}

void solve(){
  double ans = N == 0 ? 1.0 : 2.0;
  for( auto it = group.begin(); it != group.end(); ++it ){
    sort( ( it->second ).begin(), ( it->second ).end(), 
          [ & ]( int i, int j ){ return X[ i ] != X[ j ] ? X[ i ] < X[ j ] : Y[ i ] < Y[ j ]; } );
    vector< int > stk;
    function< double ( double, double, double, double ) > cross = [ & ]( double x1, double y1, double x2, double y2 ){ return x1 * y2 - y1 * x2; };  
    for( int i = 0; i < ( it->second ).size(); ++i ){
      while( stk.size() >= 2 ){
        int s = stk.size();
        double dx1 = X[ stk[ s - 1 ] ] - X[ stk[ s - 2 ] ];
        double dy1 = Y[ stk[ s - 1 ] ] - Y[ stk[ s - 2 ] ];
        double dx2 = X[ ( it->second )[ i ] ] - X[ stk[ s - 2 ] ];
        double dy2 = Y[ ( it->second )[ i ] ] - Y[ stk[ s - 2 ] ];
        if( cross( dx1, dy1, dx2, dy2 ) <= 0.0 )
          stk.pop_back();
        else
          break;
      }
      stk.emplace_back( ( it->second )[ i ] );
    }
    int h = stk.size();
    for( int i = ( int ) ( it->second ).size() - 2; i >= 0; --i ){
      while( stk.size() > h ){
        int s = stk.size();
        double dx1 = X[ stk[ s - 1 ] ] - X[ stk[ s - 2 ] ];
        double dy1 = Y[ stk[ s - 1 ] ] - Y[ stk[ s - 2 ] ];
        double dx2 = X[ ( it->second )[ i ] ] - X[ stk[ s - 2 ] ];
        double dy2 = Y[ ( it->second )[ i ] ] - Y[ stk[ s - 2 ] ];
        if( cross( dx1, dy1, dx2, dy2 ) <= 0.0 )
          stk.pop_back();
        else
          break;
      }
      stk.emplace_back( ( it->second )[ i ] );
    }
    stk.pop_back(); // start
    h = stk.size();
    for( int i = 0; i < h; ++i )
      stk.emplace_back( stk[ i ] );
    for( int i = 0, j = 1; i < h; ++i ){
      while( eu_dis2( X[ stk[ i ] ], Y[ stk[ i ] ], X[ stk[ j ] ], Y[ stk[ j ] ] ) <= eu_dis2( X[ stk[ i ] ], Y[ stk[ i ] ], X[ stk[ j + 1 ] ], Y[ stk[ j + 1 ] ] ) )
        ++j,
        assert( j < stk.size() );
      upmax( ans, 2.0 + eu_dis( X[ stk[ i ] ], Y[ stk[ i ] ], X[ stk[ j ] ], Y[ stk[ j ] ] ) ); 
    }
  }
  cout << fixed << setprecision( 8 ) << ans << endl;
}

signed main(){
  ios::sync_with_stdio( 0 );
  init();
  preprocess();
  solve();
  return 0;
}
0