zoukankan      html  css  js  c++  java
  • 洛谷P2202 [USACO13JAN]方块重叠Square Overlap

    题目描述

    Farmer John is planning to build N (2 <= N <= 50,000) square fenced-in pastures on his farm, each of size exactly K x K (1 <= K <= 1,000,000). Pasture i is centered at point (x_i, y_i) with integer coordinates in the range -1,000,000...1,000,000. However, in his haste to complete his plans, FJ realizes that he may have accidentally placed two pastures in locations that overlap (by overlap, this means the two pastures share a positive area in common). No two pastures share the exact same center point.

    Given the locations of each of the planned square pastures, please help FJ compute the area shared by the two overlapping pastures. Output zero if no two squares overlap, and -1 if overlap occurs between more than a single pair of pastures.

    在一个直角坐标系中,有N个边长为K的正方形。

    给出每一个正方形的中心,请判断所有的正方形是否有重叠。

    输入数据保证每一个正方形的中心不重合

    输入输出格式

    输入格式:

    * 第1行 :两个正整数: N , K

    其中:2 <= N <= 50 000 ,1 <= K <= 1 000 000 ,K保证是偶数

    *第2 .. i+1行:每行有两个整数xi,yi,描述了第i个正方形的中心。

    其中:xi,yi均在[-1 000 000,1 000 000]内

    输出格式:

    只输出一行:

    如果没有正方形重叠,输出“0”;如果有且只有一对正方形重叠,输出它们重叠的面积;如果有两对及以上的正方形重合,输出"-1";

    注意:在输出答案后一定要输换行符!

    这道题我一开始的思路是定义一个color进行染色标记,但发现这样需要遍历很多次,时间上肯定会炸,于是就借鉴了一些大佬的题解,暴力打出来了50,但是在不会优化了,于是再次,,,。

    这个题给我最深的印象就是判断两个正方形是否相交,只需满足|Xa-Xb|<K && |Ya-Yb|<K((Xa,Ya),(Xb,Yb)分别为两个正方形的中心坐标;K为正方形的边长)。

    另外,最后

    ans = (k - tmp) * (k - abs(e[i].yi - e[j].yi));
    k-tmp即为两正方形相交围成的小矩形的上边的边长,同理k - abs(e[i].yi - e[j].yi)为侧边的边长。
    手动模拟一下就能发现了。
     
    这个题最令我吃惊的就是官方正解竟然是平衡树(暴力大法好)
     
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cstdlib>
     5 #include<algorithm>
     6 #include<cmath>
     7 using namespace std;
     8 #define maxn 20010
     9 #define ll long long
    10 #define IL inline
    11 #define clear(a) memset(a,0,sizeof a)
    12 IL void read(int &x)
    13 {
    14     x = 0;int f = 1;char ch = getchar();
    15     while(ch<'0'||ch>'9'){if(ch=='-')f = -f;ch = getchar();}
    16     while(ch>='0'&&ch<='9'){x = x * 10 + ch - '0', ch = getchar();}x *= f;
    17 }
    18 
    19 int n, k, tot, color = 1, ans;
    20 int lazy[maxn][maxn];
    21 struct edge{
    22     int xi, yi;
    23 } e[3*maxn];
    24 
    25 int main()
    26 {
    27     read(n), read(k);
    28     for (int i = 1; i <= n;i++)
    29         read(e[i].xi), read(e[i].yi);
    30     for (int i = 1; i <= n;i++)
    31     {
    32         for (int j = 1; j <= n;j++)
    33         {
    34             if(i==j)
    35                 continue;
    36             if(lazy[i][j])
    37                 continue;
    38             lazy[i][j] = 1, lazy[j][i] = 1;
    39             int nx = abs(e[i].xi - e[j].xi);
    40             int ny = abs(e[i].yi - e[j].yi);
    41             if(nx<k&&ny<k)
    42             {
    43                 tot++;
    44                 int minx = min(e[i].xi + k / 2, e[j].xi + k / 2);
    45                 int maxx = max(e[i].xi - k / 2, e[j].xi - k / 2);
    46                 int miny = min(e[i].yi + k / 2, e[j].yi + k / 2);
    47                 int maxy = max(e[i].yi - k / 2, e[j].yi - k / 2);
    48                 ans = (minx - maxx) * (miny - maxy);
    49             }
    50         }
    51     }
    52     if(!tot)
    53         cout << "0";
    54     else if(tot>=2)
    55         cout << "-1";
    56     else if(tot==1)
    57         cout << ans;
    58     return 0;
    59 }

    /*50分无优化搜索*/
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cstdlib>
     5 #include<algorithm>
     6 #include<cmath>
     7 using namespace std;
     8 #define maxn 500010
     9 #define ll long long
    10 #define IL inline
    11 #define clear(a) memset(a,0,sizeof a)
    12 IL void read(int &x)
    13 {
    14     x = 0;int f = 1;char ch = getchar();
    15     while(ch<'0'||ch>'9'){if(ch=='-')f = -f;ch = getchar();}
    16     while(ch>='0'&&ch<='9'){x = x * 10 + ch - '0', ch = getchar();}x *= f;
    17 }
    18 
    19 int n, k, tot, ans, have;
    20 struct edge{
    21     int xi, yi;
    22 } e[maxn];
    23 
    24 bool cmp(edge a,edge b)
    25 {
    26     return a.xi < b.xi;
    27 }
    28 
    29 int main()
    30 {
    31 //    freopen("a.in","r",stdin);
    32 //    freopen("a.out","w",stdout);
    33     read(n), read(k);
    34     for (int i = 1; i <= n;i++)
    35         read(e[i].xi), read(e[i].yi);
    36     sort(e + 1, e + n + 1, cmp);
    37     for (int i = 2; i <= n;i++)
    38     {
    39         int tmp = 0;
    40         for (int j = i-1; j >= 1;j--)
    41         {
    42             tmp += e[j + 1].xi - e[j].xi;
    43             if(tmp>=k)
    44                 break;
    45            if(abs(e[i].yi-e[j].yi)<k)
    46            {
    47                if(have)
    48                {
    49                    cout << "-1"
    50                         << "
    ";
    51                    return 0;
    52                }
    53                have = 1;
    54                ans = (k - tmp) * (k - abs(e[i].yi - e[j].yi));
    55            }
    56         }
    57     }
    58     cout << ans << "
    ";
    59     return 0;
    60 }

    /*AC加优化搜索*/
  • 相关阅读:
    微信小程序开发工具 POST net::ERR_PROXY_CONNECTION_FAILED 代理问题
    微信小程序 带参调用后台接口 循环渲染页面 wx.request wx:for
    三下乡感悟心得体会
    Mysql通过Adjacency List(邻接表)存储树形结构
    java的List中使用filter过滤出符合特定条件的元素List
    mybatis报表,动态列与查询参数+行列转换
    mysql行转列转换
    spring配置jackson不返回null值
    mybatis动态列名
    查出最新记录
  • 原文地址:https://www.cnblogs.com/KGW-/p/10364395.html
Copyright © 2011-2022 走看看