Luo Gu P2202 [USACO13JAN] box overlapping Square Overlap

Luo Gu P2202 [USACO13JAN] box overlapping Square Overlap

Description

  • In a Cartesian coordinate system, there are N K square side length.

    Each is given a square Center, determines whether all the squares overlap.

    To ensure that each of the input data do not coincide with the center of a square

Input

  • * Line 1: two positive integers: N, K

    Wherein: 2 <= N <= 50 000, 1 <= K <= 1 000 000, K is an even number to ensure

    * 2 .. i + 1 of the row: Each row has two integers xi, yi, i-described center squares.

    Where: xi, yi are in [-1 000 000,1 000 000] inner

Output

  • Only one line of output:

    If there is no overlap square, outputs "0"; and if there is only one pair of overlapping square, the area where they overlap the output; and if there are more than two pairs of square coincidence, outputs "-1";

    Note: After the answer must lose output line breaks!

Sample Input

4 6
0 0
8 4
-2 1
0 7

Sample Output

20

answer:

  • simulation.
  • Positive solution looks like a linear scan, in fact, is to optimize the violence. But I own this metaphysics optimized code on the card over.
  • Normal write is then calculated directly judge whether the overlapping area of ​​overlap, O (n ^ 2)
  • I am nothing more than a keyword with x row a sequence, other practices above, O (n *?),Card is easy to O (n ^ 2)
  • This question is a little trouble spots count the overlapping area. First, an overlapping area overlapping the horizontal distance = vertical distance * overlap.
  • Because the horizontal distance x in accordance with the order of the rows, it is easy to draw. The vertical distance is needed to discuss the classification, after I hand painted, there are the following three conditions:

  • Pictures are then based on it is easy to calculate the vertical distance.
#include <iostream>
#include <cstdio>
#include <algorithm>
#define N 50005
using namespace std;

struct A {int x, y;} a[N];
int n, k, ans, tot;

int read()
{
    int x  = 0, f = 1; char c = getchar();
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0'  && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
    return x *= f;
}

bool cmp(A u, A v) {return u.x < v.x;}

int main()
{
    n = read(), k = read(), k /= 2;
    for(int i = 1; i <= n; i++) a[i].x = read(), a[i].y = read();
    sort(a + 1, a + 1 + n, cmp);
    for(int i = 1; i < n; i++)
    {
        int j = i + 1;
        while(a[i].x + k > a[j].x - k && j <= n)
        {
            int len1 = a[i].x + k - a[j].x + k, len2 = 0, tmp;
            if(a[i].y == a[j].y) len2 = 2 * k;
            else if(a[i].y + k >= a[j].y - k && a[j].y - k >= a[i].y - k) len2 = a[i].y + k - a[j].y + k;
            else if(a[i].y - k <= a[j].y + k && a[j].y + k <= a[i].y + k) len2 = a[j].y + k - a[i].y + k;
            tmp = len1 * len2;
            if(tmp)
            {
                ans = tmp;
                if(++tot == 2) {cout << -1 << endl; return 0;}
            }
            j++;
        }
    }
    if(!tot) cout << 0 << endl;
    else cout << ans << endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/BigYellowDog/p/11323486.html