code forces 994C

C. Two Squares

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given two squares, one with sides parallel to the coordinate axes, and another one with sides at 45 degrees to the coordinate axes. Find whether the two squares intersect.

The interior of the square is considered to be part of the square, i.e. if one square is completely inside another, they intersect. If the two squares only share one common point, they are also considered to intersect.

Input

The input data consists of two lines, one for each square, both containing 4 pairs of integers. Each pair represents coordinates of one vertex of the square. Coordinates within each line are either in clockwise or counterclockwise order.

The first line contains the coordinates of the square with sides parallel to the coordinate axes, the second line contains the coordinates of the square at 45 degrees.

All the values are integer and between ?100?100 and 100100.

Output

Print "Yes" if squares intersect, otherwise print "No".

You can print each letter in any case (upper or lower).

Examples

input

Copy

0 0 6 0 6 6 0 61 3 3 5 5 3 3 1

output

Copy

YES

input

Copy

0 0 6 0 6 6 0 67 3 9 5 11 3 9 1

output

Copy

NO

input

Copy

6 0 6 6 0 6 0 07 4 4 7 7 10 10 7

output

Copy

YES

Note

In the first example the second square lies entirely within the first square, so they do intersect.

In the second sample squares do not have any points in common.

Here are images corresponding to the samples:

题意,两个正方形是否相交

题解:1.用叉积判断边是否相交

   2.判断A的中心是否在B内或者B的中心是否在A内

叉积

判断两条线段是否相交

要判断两条线段是否相交,则需要检查每条线段是否跨越了另一条线段的直线。如果点p1位于某直线的一边,而点p2位于该直线的另一边,则称p1p2跨越了这条直线。两条线段相交,当且仅当下面两个条件至少成立一个:

每条线段都跨越了包含另一条线段的直线

一条线段的一个端点落在另一条线段上

#include<bits/stdc++.h>
using namespace std;
#define x first
#define y second
typedef pair<int, int> PII;
int x[3][4], y[3][4];//两个正方形的坐标
PII p[3][4];

//叉积判断是否相交
int cross (PII p1, PII p2, PII p) {
    return (p2.x - p1.x) * (p.y - p1.y) - (p.x - p1.x) * (p2.y - p1.y);
}

bool ok (int i, int j) {
    int flag = 0;
    for (int k = 0; k < 4; k++) {
            //四个边用叉积判断是否相交或者同侧

        if (cross (p[i][0], p[i][1], p[j][k]) *cross (p[i][2], p[i][3], p[j][k]) >= 0 &&
                cross (p[i][1], p[i][2], p[j][k]) *cross (p[i][3], p[i][0], p[j][k]) >= 0) {
            //判断相交
            //两边包一边的思想
            flag = 1;
        }
    }

    //判断A中心是否在B内或者B中心是否在A内
    int xx=(p[j][0].x + p[j][2].x) / 2;
    int yy=(p[j][1].y + p[j][3].y) / 2;
    PII px = PII ( xx, yy);
    if (cross (p[i][0], p[i][1], px) *cross (p[i][2], p[i][3], px) >= 0 &&
                cross (p[i][1], p[i][2], px) *cross (p[i][3], p[i][0], px) >= 0) {
        //判断相交
        flag = 1;
    }
    return flag;
}

int main() {
    int n, m;
    //把正方形的点封装到pair内去
    for (int i = 0; i < 4; i++) {
        cin >> x[1][i] >> y[1][i];
        p[1][i] = PII (x[1][i], y[1][i]);
    }
    for (int i = 0; i < 4; i++) {
        cin >> x[2][i] >> y[2][i];
        p[2][i] = PII (x[2][i], y[2][i]);
    }

    int flag = 0;
    if (ok (1, 2) || ok (2, 1) ) flag = 1;
    printf ("%s\n", flag ? "YES" : "NO");
    return 0;
}

 

原文地址:https://www.cnblogs.com/buerdepepeqi/p/9193000.html

时间: 2024-10-13 22:20:05

code forces 994C的相关文章

贪心-Code forces -387B -George and Round

Code forces -387B -George and Round description George decided to prepare a Codesecrof round, so he has prepared m problems for the round. Let's number the problems with integers 1 through m. George estimates the i-th problem's complexity by integer 

Code Forces Gym 100971D Laying Cables(单调栈)

D - Laying Cables Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u One-dimensional country has n cities, the i-th of which is located at the point xi and has population pi, and all xi, as well as all pi, are distinct. Whe

Code Forces Gym 100886J Sockets

J - Sockets Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Valera has only one electrical socket in his flat. He also has m devices which require electricity to work. He's got n plug multipliers to plug the devices, the

code forces 140F New Year Snowflake

F. New Year Snowflake As Gerald ..., in other words, on a New Year Eve Constantine prepared an unusual present for the Beautiful Lady. The present is the magic New Year snowflake that can make any dream come true. The New Year snowflake consists of t

Code Forces 414B 很不错的双手,以促进合规

http://codeforces.com/problemset/problem/414/B 题目挺不错的.留个纪念,活动脑筋不错的题目 #include<iostream> #include<cstdio> #include<list> #include<algorithm> #include<cstring> #include<string> #include<queue> #include<stack>

code forces 148D Bag of mice (概率DP)

time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies

code forces 979C

C. Kuro and Walking Route time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Kuro is living in a country called Uberland, consisting of nn towns, numbered from 11 to nn, and n?1n?1 bidirectio

code forces 990C

http://codeforces.com/contest/990/problem/C C. Bracket Sequences Concatenation Problem time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output A bracket sequence is a string containing only charac

code forces 999C Alphabetic Removals

C. Alphabetic Removals time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given a string ss consisting of nn lowercase Latin letters. Polycarp wants to remove exactly kk characters (k