Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- using namespace __gnu_pbds;
- using namespace std;
- //#define int long long
- #define mp make_pair
- #define pb push_back
- //#pragma GCC optimize("Ofast")
- //#pragma GCC optimize("unroll-loops")
- //#pragma GCC target("avx2")
- constexpr long long INF = (long long)2e9;
- constexpr int N = (int)1e6 + 111;
- constexpr int md = (int)1e9 + 7;
- constexpr int mdPower = (int)1e9+7 - 1;
- constexpr int P = (int)998244343;
- constexpr double PI = acos(-1);
- //typedef __int128 _uint;
- typedef long long ll;
- mt19937_64 rnd(time(nullptr));
- void add(int& a,int b){
- a += b; if(a >= md) a -= md;
- }
- void sub(int& a,int b){
- a -= b; while(a < 0) a += md;
- }
- int bpow(int a,int b){
- if(b == 0)
- return 1;
- if(b % 2 == 0){
- int t = bpow(a,b>>1);
- return 1ll*t*t%md;
- }
- return 1ll * a*bpow(a,b-1) % md;
- }
- int inv(int a){
- return bpow(a,md-2);
- }
- //int fac[N],invfac[N];
- //
- //void init(){
- // fac[0] = 1;
- // for(int i = 1; i < N; i++){
- // fac[i] = (fac[i-1] * i) % md;
- // }
- // invfac[N-1] = inv(fac[N-1]);
- // for(int i = N-2; i >= 0; i--){
- // invfac[i] = (invfac[i+1] * (i+1))%md;
- // }
- // return;
- //}
- //
- //int C(int n,int k){
- // if(k > n)
- // return 0;
- // return fac[n] * invfac[k] % md * invfac[n-k] % md;
- //}
- //
- //int A(int n,int k){
- // if(k > n)
- // return 0;
- // return fac[n] * invfac[n-k] % md;
- //}
- struct pt{
- int x,y;
- pt(){}
- pt(int x,int y):x(x),y(y){}
- };
- int sg(int x){
- if(x == 0)
- return 0;
- if(x < 0)
- return -1;
- if(x > 0)
- return 1;
- return 0;
- }
- int src(pt a,pt b,pt c){
- pt v1 = pt(b.x-a.x,b.y-a.y);
- pt v2 = pt(c.x-a.x,c.y-a.y);
- return sg(v1.x * v2.y - v1.y * v2.x);
- }
- bool intersect1(int a,int b,int c,int d){
- if(a > b) swap(a,b);
- if(c > d) swap(c,d);
- return max(a,c) <= min(b,d);
- }
- bool intersect(pt a1,pt a2,pt b1,pt b2){
- return intersect1(a1.x,a2.x,b1.x,b2.x)
- && intersect1(a1.y,a2.y,b1.y,b2.y)
- && sg(src(a1,a2,b1) * src(a1,a2,b2)) == -1
- && sg(src(b1,b2,a1) * src(b1,b2,a2)) == -1;
- }
- long double dist(pt a,pt b){
- int d1 = a.x - b.x;
- int d2 = a.y - b.y;
- return sqrt(d1 * d1 + d2 * d2);
- }
- constexpr int K = 12 * 12;
- pt InnerPolygon[2*K];
- pt OuterPolygon[2*K];
- pt Polygon[2][2*K];
- long double dp[2][2*K];
- int n,m;
- bool LiesInInnerPolygon(pt a){
- set<int> st;
- for(int i = 0; i < n; i++){
- st.insert(src(Polygon[0][i],Polygon[0][i+1],a));
- }
- st.erase(0);
- return st.size() == 1;
- }
- void solve(){
- cin >> n;
- for(int i = 0; i < n; i++){
- cin >> Polygon[0][i].x >> Polygon[0][i].y;
- }
- cin >> m;
- for(int i = 0; i < m; i++){
- cin >> Polygon[1][i].x >> Polygon[1][i].y;
- }
- for(int i = n; i < 2*K; i++){
- Polygon[0][i] = Polygon[0][i-n];
- }
- for(int i = m; i < 2*K; i++){
- Polygon[1][i] = Polygon[1][i-m];
- }
- vector<pair<pt,pt>> Segments;
- for(int i = 0; i < n; i++){
- Segments.pb(mp(Polygon[0][i],Polygon[0][i+1]));
- }
- for(int i = 0; i < m; i++){
- Segments.pb(mp(Polygon[1][i],Polygon[1][i+1]));
- }
- int sz[2] = {n,m};
- long double ans = INF;
- for(int l = 0; l < 2; l++){
- for(int i = 0; i < sz[l]; i++){
- for(int j = 0; j < 2*K; j++){
- dp[0][j] = dp[1][j] = INF;
- }
- dp[l][i] = 0;
- for(int j = i; j < i + K; j++){
- for(int t1 = 0; t1 < 2; t1++){
- if(dp[t1][j] == INF)
- continue;
- pt p1 = Polygon[t1][j];
- cerr << "curPt = " << p1.x << " " << p1.y << "\n";
- for(int k = j; k < i+K; k++){
- for(int t2 = 0; t2 < 2; t2++){
- if(t1 == t2 && (k >= j + 3 || k >= i + sz[t1] - 1))
- break;
- if(t1 == t2 && k%sz[t1] == j%sz[t1])
- continue;
- pt p2 = Polygon[t2][k];
- bool ok = true;
- for(auto&[s1,s2] : Segments){
- if(intersect(p1,p2,s1,s2)){
- ok = false;
- // cerr << p1.x << " " << p1.y << " " << p2.x << " " << p2.y;
- // cerr << " intersects with ";
- // cerr << s1.x << " " << s1.y << " " << s2.x << " " << s2.y;
- // cerr << "\n";
- break;
- }
- }
- if(ok){
- cerr << Polygon[t1][j].x << " " << Polygon[t1][j].y << " -> ";
- cerr << Polygon[t2][k].x << " " << Polygon[t2][k].y << "\n\n";
- dp[t2][k] = min(dp[t2][k],dp[t1][j]+dist(p1,p2));
- }
- }
- }
- }
- }
- cerr << "!!!!!!!!!!!!!!!!!" << l << " " << i << "\n";
- for(int j = i+sz[l]; j < 2*K; j += sz[l]){
- ans = min(ans,dp[l][j]);
- }
- cerr << "ans = " << ans << "\n";
- }
- }
- cout << fixed << setprecision(15) << ans << "\n";
- return;
- }
- signed main(){
- ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
- int tests = 1;
- // cin >> tests;
- for(int test = 1; test <= tests; test++){
- // cerr << "test = " << test << "\n";
- solve();
- }
- return 0;
- }
- /**
- 6 8
- 1 2
- 2 4
- 4 5
- 5 6
- 6 4
- 4 2
- 2 3
- 3 1
- ))((()
- **/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement