답안 #62330

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
62330 2018-07-28T05:54:26 Z Benq Fences (JOI18_fences) C++11
100 / 100
438 ms 17388 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
 
using namespace std;
using namespace __gnu_pbds;
 
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
 
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
 
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
 
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
 
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
 
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
 
const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 100001;
 
ld EPS = 1e-14;
 
template<class T> istream& operator>> (istream& is, complex<T>& p) {
    T value;
    is >> value; p.real(value);
    is >> value; p.imag(value);
    return is;
}
 
int N,S;
vector<pair<cd,cd>> p;
vector<vector<pair<int,pair<ld,int>>>> adj;
ld ans = INF;
 
cd reflect(cd p, cd a, cd b) { return a+conj((p-a)/(b-a))*(b-a); }
cd proj(cd p, cd a, cd b) { return (p+reflect(p,a,b))/(ld)2; }
 
bool bet(cd a, cd b, cd c) { return ((b-a)/(c-b)).real() > 0; }
 
cd closest(cd a, pair<cd,cd> b) {
    if (b.f == b.s) return b.f;
    auto x = proj(a,b.f,b.s);
    if (bet(b.f,x,b.s)) return x;
    return abs(a-b.f) < abs(a-b.s) ? b.f : b.s;
}
 
ld cross(cd a, cd b) { return (conj(a)*b).imag(); }
ld area(cd a, cd b, cd c) { return cross(b-a,c-a); }
 
cd line(cd a, cd b, cd c, cd d) {
    ld x = area(a,b,c), y = area(a,b,d);
    return (x*d-y*c)/(x-y);
}
 
bool equiv(cd a, cd b) {
    return abs(1-abs(a/b)) <= EPS;
}
 
int inter(pair<cd,cd> bes) {
    F0R(i,4) {
        auto x = line({0,0},p[i].f,bes.f,bes.s);
        if (bet(bes.f,x,bes.s) && abs(x) < abs(p[i].f) && !equiv(p[i].f,x)) return MOD;
    }
    bool swa = 0;
    if (bes.f.imag() > bes.s.imag()) swa = 1, swap(bes.f,bes.s);
    if (bes.f.imag() < 0 && bes.s.imag() >= 0) {
        auto x = line({0,0},{1,0},bes.f,bes.s);
        if (x.real() < 0) return 0;
        if (swa == 0) return 1;
        return -1;
    }
    return 0;
}
 
pair<cd,cd> bet(pair<cd,cd> a, pair<cd,cd> b) {
     return (abs(a.f-a.s) < abs(b.f-b.s) ? a : b);
}
 
pair<ld,int> dis(pair<cd,cd> a, pair<cd,cd> b) {
    pair<cd,cd> bes = {{0,0},{INF,INF}};
    bes = bet(bes,{a.f,closest(a.f,b)});
    bes = bet(bes,{a.s,closest(a.s,b)});
    bes = bet(bes,{closest(b.f,a),b.f});
    bes = bet(bes,{closest(b.s,a),b.s});
    
    int x = inter(bes);
    
    if (x == MOD) return {INF,0};
    return {abs(bes.s-bes.f),x};
}
 
void input() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> N >> S;
    p.pb({{S,S},{S,S}});
    p.pb({{S,-S},{S,-S}});
    p.pb({{-S,S},{-S,S}});
    p.pb({{-S,-S},{-S,-S}});
    F0R(i,N) {
        cd a,b; cin >> a >> b; a += cd(1e-9,1e-9); b += cd(1e-9,1e-9);
        if (a.imag() > b.imag()) swap(a,b);
        if (a.imag() < 0 && b.imag() >= 0) {
            auto x = line(a,b,{0,0},{1,0});
            if (x.real() > 0) {
                p.pb({a,x+EPS*(a-x)});
                p.pb({x,b});
                continue;
            }
        } 
        p.pb({a,b});
    }
}
 
ld dist[300][101];

void genDist(int x) {
    F0R(i,sz(p)) F0R(j,101) dist[i][j] = INF;
    priority_queue<pair<ld,pi>,vector<pair<ld,pi>>,greater<pair<ld,pi>>> p;
    p.push({dist[x][50] = 0, {x,0}});
    while (sz(p)) {
        auto a = p.top(); p.pop();
        if (a.f > dist[a.s.f][a.s.s+50]) continue;
        if (a.s.f == x && a.s.s != 0) {
            ans = min(ans,a.f);
            return;
        }
        for (auto x: adj[a.s.f]) {
            pi t = {x.f,a.s.s+x.s.s};
            if (abs(t.s) > 50) continue;
            if (x.s.f+a.f < dist[t.f][t.s+50]) {
                p.push({dist[t.f][t.s+50] = x.s.f+a.f,t});
            }
        }
    }
}
 
void genEdge() {
    // for (auto a: p) cout << a.f << " " << a.s << "\n";
    adj.resize(sz(p));
    F0R(i,sz(p)) {
        F0R(j,sz(p)) if (i != j) {
            auto a = dis(p[i],p[j]);
            if (a.f == INF) continue;
            // cout << i << " " << j << " " << a.f << " " << a.s << "\n";
            adj[i].pb({j,a});
        }
    }
}
 
int main() {
    input();
    genEdge();
    F0R(i,sz(p)) genDist(i);
    cout << fixed << setprecision(9) << ans;
}
 
/* Look for:
* the exact constraints (multiple sets are too slow for n=10^6 :( ) 
* special cases (n=1?)
* overflow (ll vs int?)
* array bounds
*/

Compilation message

fences.cpp: In function 'void input()':
fences.cpp:117:23: warning: narrowing conversion of 'S' from 'int' to 'long double' inside { } [-Wnarrowing]
     p.pb({{S,S},{S,S}});
                       ^
fences.cpp:117:23: warning: narrowing conversion of 'S' from 'int' to 'long double' inside { } [-Wnarrowing]
fences.cpp:117:23: warning: narrowing conversion of 'S' from 'int' to 'long double' inside { } [-Wnarrowing]
fences.cpp:117:23: warning: narrowing conversion of 'S' from 'int' to 'long double' inside { } [-Wnarrowing]
fences.cpp:118:25: warning: narrowing conversion of 'S' from 'int' to 'long double' inside { } [-Wnarrowing]
     p.pb({{S,-S},{S,-S}});
                         ^
fences.cpp:118:14: warning: narrowing conversion of '- S' from 'int' to 'long double' inside { } [-Wnarrowing]
     p.pb({{S,-S},{S,-S}});
              ^~
fences.cpp:118:25: warning: narrowing conversion of 'S' from 'int' to 'long double' inside { } [-Wnarrowing]
     p.pb({{S,-S},{S,-S}});
                         ^
fences.cpp:118:21: warning: narrowing conversion of '- S' from 'int' to 'long double' inside { } [-Wnarrowing]
     p.pb({{S,-S},{S,-S}});
                     ^~
fences.cpp:119:12: warning: narrowing conversion of '- S' from 'int' to 'long double' inside { } [-Wnarrowing]
     p.pb({{-S,S},{-S,S}});
            ^~
fences.cpp:119:25: warning: narrowing conversion of 'S' from 'int' to 'long double' inside { } [-Wnarrowing]
     p.pb({{-S,S},{-S,S}});
                         ^
fences.cpp:119:19: warning: narrowing conversion of '- S' from 'int' to 'long double' inside { } [-Wnarrowing]
     p.pb({{-S,S},{-S,S}});
                   ^~
fences.cpp:119:25: warning: narrowing conversion of 'S' from 'int' to 'long double' inside { } [-Wnarrowing]
     p.pb({{-S,S},{-S,S}});
                         ^
fences.cpp:120:12: warning: narrowing conversion of '- S' from 'int' to 'long double' inside { } [-Wnarrowing]
     p.pb({{-S,-S},{-S,-S}});
            ^~
fences.cpp:120:15: warning: narrowing conversion of '- S' from 'int' to 'long double' inside { } [-Wnarrowing]
     p.pb({{-S,-S},{-S,-S}});
               ^~
fences.cpp:120:20: warning: narrowing conversion of '- S' from 'int' to 'long double' inside { } [-Wnarrowing]
     p.pb({{-S,-S},{-S,-S}});
                    ^~
fences.cpp:120:23: warning: narrowing conversion of '- S' from 'int' to 'long double' inside { } [-Wnarrowing]
     p.pb({{-S,-S},{-S,-S}});
                       ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 4 ms 556 KB Output is correct
4 Correct 3 ms 556 KB Output is correct
5 Correct 3 ms 556 KB Output is correct
6 Correct 2 ms 556 KB Output is correct
7 Correct 3 ms 556 KB Output is correct
8 Correct 3 ms 556 KB Output is correct
9 Correct 3 ms 588 KB Output is correct
10 Correct 3 ms 588 KB Output is correct
11 Correct 3 ms 588 KB Output is correct
12 Correct 2 ms 588 KB Output is correct
13 Correct 3 ms 588 KB Output is correct
14 Correct 3 ms 588 KB Output is correct
15 Correct 3 ms 592 KB Output is correct
16 Correct 3 ms 592 KB Output is correct
17 Correct 3 ms 596 KB Output is correct
18 Correct 3 ms 600 KB Output is correct
19 Correct 3 ms 600 KB Output is correct
20 Correct 2 ms 620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 4 ms 556 KB Output is correct
4 Correct 3 ms 556 KB Output is correct
5 Correct 3 ms 556 KB Output is correct
6 Correct 2 ms 556 KB Output is correct
7 Correct 3 ms 556 KB Output is correct
8 Correct 3 ms 556 KB Output is correct
9 Correct 3 ms 588 KB Output is correct
10 Correct 3 ms 588 KB Output is correct
11 Correct 3 ms 588 KB Output is correct
12 Correct 2 ms 588 KB Output is correct
13 Correct 3 ms 588 KB Output is correct
14 Correct 3 ms 588 KB Output is correct
15 Correct 3 ms 592 KB Output is correct
16 Correct 3 ms 592 KB Output is correct
17 Correct 3 ms 596 KB Output is correct
18 Correct 3 ms 600 KB Output is correct
19 Correct 3 ms 600 KB Output is correct
20 Correct 2 ms 620 KB Output is correct
21 Correct 2 ms 620 KB Output is correct
22 Correct 4 ms 620 KB Output is correct
23 Correct 2 ms 620 KB Output is correct
24 Correct 3 ms 620 KB Output is correct
25 Correct 4 ms 620 KB Output is correct
26 Correct 3 ms 620 KB Output is correct
27 Correct 2 ms 620 KB Output is correct
28 Correct 3 ms 624 KB Output is correct
29 Correct 3 ms 624 KB Output is correct
30 Correct 3 ms 624 KB Output is correct
31 Correct 3 ms 624 KB Output is correct
32 Correct 3 ms 624 KB Output is correct
33 Correct 3 ms 624 KB Output is correct
34 Correct 4 ms 624 KB Output is correct
35 Correct 3 ms 624 KB Output is correct
36 Correct 3 ms 624 KB Output is correct
37 Correct 3 ms 624 KB Output is correct
38 Correct 3 ms 624 KB Output is correct
39 Correct 3 ms 624 KB Output is correct
40 Correct 3 ms 624 KB Output is correct
41 Correct 3 ms 624 KB Output is correct
42 Correct 3 ms 624 KB Output is correct
43 Correct 4 ms 764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 4 ms 556 KB Output is correct
4 Correct 3 ms 556 KB Output is correct
5 Correct 3 ms 556 KB Output is correct
6 Correct 2 ms 556 KB Output is correct
7 Correct 3 ms 556 KB Output is correct
8 Correct 3 ms 556 KB Output is correct
9 Correct 3 ms 588 KB Output is correct
10 Correct 3 ms 588 KB Output is correct
11 Correct 3 ms 588 KB Output is correct
12 Correct 2 ms 588 KB Output is correct
13 Correct 3 ms 588 KB Output is correct
14 Correct 3 ms 588 KB Output is correct
15 Correct 3 ms 592 KB Output is correct
16 Correct 3 ms 592 KB Output is correct
17 Correct 3 ms 596 KB Output is correct
18 Correct 3 ms 600 KB Output is correct
19 Correct 3 ms 600 KB Output is correct
20 Correct 2 ms 620 KB Output is correct
21 Correct 2 ms 620 KB Output is correct
22 Correct 4 ms 620 KB Output is correct
23 Correct 2 ms 620 KB Output is correct
24 Correct 3 ms 620 KB Output is correct
25 Correct 4 ms 620 KB Output is correct
26 Correct 3 ms 620 KB Output is correct
27 Correct 2 ms 620 KB Output is correct
28 Correct 3 ms 624 KB Output is correct
29 Correct 3 ms 624 KB Output is correct
30 Correct 3 ms 624 KB Output is correct
31 Correct 3 ms 624 KB Output is correct
32 Correct 3 ms 624 KB Output is correct
33 Correct 3 ms 624 KB Output is correct
34 Correct 4 ms 624 KB Output is correct
35 Correct 3 ms 624 KB Output is correct
36 Correct 3 ms 624 KB Output is correct
37 Correct 3 ms 624 KB Output is correct
38 Correct 3 ms 624 KB Output is correct
39 Correct 3 ms 624 KB Output is correct
40 Correct 3 ms 624 KB Output is correct
41 Correct 3 ms 624 KB Output is correct
42 Correct 3 ms 624 KB Output is correct
43 Correct 4 ms 764 KB Output is correct
44 Correct 111 ms 1744 KB Output is correct
45 Correct 86 ms 1744 KB Output is correct
46 Correct 93 ms 1744 KB Output is correct
47 Correct 69 ms 1744 KB Output is correct
48 Correct 86 ms 1860 KB Output is correct
49 Correct 117 ms 1860 KB Output is correct
50 Correct 79 ms 1860 KB Output is correct
51 Correct 93 ms 1860 KB Output is correct
52 Correct 95 ms 1860 KB Output is correct
53 Correct 75 ms 1860 KB Output is correct
54 Correct 92 ms 1860 KB Output is correct
55 Correct 102 ms 1860 KB Output is correct
56 Correct 120 ms 1860 KB Output is correct
57 Correct 80 ms 1860 KB Output is correct
58 Correct 89 ms 1860 KB Output is correct
59 Correct 89 ms 1860 KB Output is correct
60 Correct 102 ms 1860 KB Output is correct
61 Correct 111 ms 1860 KB Output is correct
62 Correct 4 ms 1860 KB Output is correct
63 Correct 3 ms 1860 KB Output is correct
64 Correct 72 ms 1860 KB Output is correct
65 Correct 104 ms 1860 KB Output is correct
66 Correct 96 ms 1860 KB Output is correct
67 Correct 438 ms 17388 KB Output is correct
68 Correct 219 ms 17388 KB Output is correct
69 Correct 78 ms 17388 KB Output is correct
70 Correct 73 ms 17388 KB Output is correct
71 Correct 87 ms 17388 KB Output is correct
72 Correct 83 ms 17388 KB Output is correct