洛谷 P3079 [USACO13MAR]农场的画Farm Painting

P3079 [USACO13MAR]农场的画Farm Painting

题目描述

After several harsh winters, Farmer John has decided it is time to re-paint his farm. The farm consists of N fenced enclosures (1 <= N <= 50,000), each of which can be described by a rectangle in the 2D plane whose sides are parallel to the x and y axes. Enclosures may be contained within other enclosures, but no two fences intersect, so if two enclosures cover the same area of the 2D plane, one must be contained within the other.

FJ figures that an enclosure contained within another enclosure will not be visible to the outside world, so he only wants to re-paint enclosures that are themselves not contained within any other enclosures. Please help FJ determine the total number of enclosures he needs to paint.

经过几个严冬,农场主约翰决定是重新粉刷农场的时候了。该农场由nn个围栏围成(1<=n=500001<=n=50000),每一个都可以用二维平面上的矩形来描述,其两侧平行于x和y轴。牛圈可能包含在其他牛圈中,但没有两个栅栏相交(不同牛圈的边不会有接触)。因此如果两个牛圈覆盖了二维平面的同一区域,那么一个必须包含在另一个内。

FJ知道,被其他牛圈包含的牛圈是不会被外面的人看到的。众所周知,FJ非常懒,所以他只想刷露在外面的牛圈,请帮助他求出总共需要刷的牛圈的个数。

输入输出格式

输入格式:

  • Line 1: The number of enclosures, N.
  • Lines 2..1+N: Each line describes an enclosure by 4 space-separated integers x1, y1, x2, and y2, where (x1,y1) is the lower-left corner of the enclosure and (x2,y2) is the upper-right corner. All coordinates are in the range 0..1,000,000.

第一行 一个数,牛圈的总数nn

第二到n+1n+1行 每行四个数,起点坐标x1,y1x1,y1和终点坐标x2,y2x2,y2

输出格式:

  • Line 1: The number of enclosures that are not contained within other enclosures.

FJ总共需要刷的牛圈的个数

输入输出样例

输入样例#1: 复制

3
2 0 8 9
10 2 11 3
4 2 6 5

输出样例#1: 复制

2

说明

There are three enclosures. The first has corners (2,0) and (8,9), and so on.

Enclosure 3 is contained within enclosure 1, so there are two enclosures not contained within other enclosures.

思路:围栏i包含围栏j的必要条件:x1[j]>x2[i]

  也就是这样的:

然后还应该满足一下几个条件:

  x1[i]<x1[j];

  y1[i]<y1[j];

  x2[i]>x2[j];

  y2[i]>y2[j];

 所以就可以写出cpp了:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n,L,R,ans;
struct nond{
    int x1,y1,x2,y2;
}v[50010];
int cmp(nond a,nond b){
    return a.x1<b.x1;
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        int a,b,c,d;
        scanf("%d%d%d%d",&a,&b,&c,&d);
        v[i].x1=a;v[i].y1=b;v[i].x2=c;v[i].y2=d;
    }
    sort(v+1,v+1+n,cmp);ans=n;
    for(L=1,R=2;R<=n;R++){
        while(v[L].x2<=v[R].x1)    L++;
        for(int i=L;i<=R;i++)
            if(v[R].x1>v[i].x1&&v[R].y2<v[i].y2&&v[i].y1<v[R].y1&&v[i].x2>v[R].x2){
                ans--;break;
            }
    }
    cout<<ans;
}

原文地址:https://www.cnblogs.com/cangT-Tlan/p/8227967.html

时间: 2024-07-31 10:30:56

洛谷 P3079 [USACO13MAR]农场的画Farm Painting的相关文章

洛谷 P2905 [USACO08OPEN]农场危机Crisis on the Farm(恶心的DP)

P2905 [USACO08OPEN]农场危机Crisis on the Farm 1605: [Usaco2008 Open]Crisis on the Farm 牧场危机 题目描述 约翰和他的奶牛组建了一只乐队“后街奶牛”,现在他们正在牧场里排练.奶牛们分成一堆 一堆,共1000)堆.每一堆里,30只奶牛一只踩在另一只的背上,叠成一座牛塔.牧场 里还有M(1 < M < 1000)个高高的草垛. 作为出色的指挥家,约翰可以通过口哨指挥奶牛们移动.他的口哨有四个音,分别能使所有 的牛塔向东南

洛谷P2905 [USACO08OPEN]农场危机Crisis on the Farm

P2905 [USACO08OPEN]农场危机Crisis on the Farm 题目描述 约翰和他的奶牛组建了一只乐队“后街奶牛”,现在他们正在牧场里排练.奶牛们分成一堆 一堆,共1000)堆.每一堆里,30只奶牛一只踩在另一只的背上,叠成一座牛塔.牧场 里还有M(1 < M < 1000)个高高的草垛. 作为出色的指挥家,约翰可以通过口哨指挥奶牛们移动.他的口哨有四个音,分别能使所有 的牛塔向东南西北四个方向移动一格. 每一次,当一个牛塔到达了一个草垛所在的格子,牛塔最上方的奶牛就会跳到

洛谷 P2905 [USACO08OPEN]农场危机Crisis on the Farm

题目描述 约翰和他的奶牛组建了一只乐队“后街奶牛”,现在他们正在牧场里排练.奶牛们分成一堆 一堆,共1000)堆.每一堆里,30只奶牛一只踩在另一只的背上,叠成一座牛塔.牧场 里还有M(1 < M < 1000)个高高的草垛. 作为出色的指挥家,约翰可以通过口哨指挥奶牛们移动.他的口哨有四个音,分别能使所有 的牛塔向东南西北四个方向移动一格. 每一次,当一个牛塔到达了一个草垛所在的格子,牛塔最上方的奶牛就会跳到草垛上,而且 不再下来,而其他奶牛仍然呈塔状站在草垛所在的格子里.当牛塔只剩一只奶牛

洛谷 P2919 [USACO08NOV]守护农场Guarding the Farm

P2919 [USACO08NOV]守护农场Guarding the Farm 题目描述 The farm has many hills upon which Farmer John would like to place guards to ensure the safety of his valuable milk-cows. He wonders how many guards he will need if he wishes to put one on top of each hill

洛谷 P2205 [USACO13JAN]画栅栏Painting the Fence

P2205 [USACO13JAN]画栅栏Painting the Fence 题目描述 Farmer John has devised a brilliant method to paint the long fence next to his barn (think of the fence as a one-dimensional number line). He simply attaches a paint brush to his favorite cow Bessie, and t

洛谷 P3671 [USACO17OPEN]Where&#39;s Bessie? 贝西在哪呢

P3671 [USACO17OPEN]Where's Bessie? 贝西在哪呢 题目背景 农夫John正在测试一个他新发明的全自动寻找奶牛无人机,它能够照一张农场的图片然后自动找出奶牛的位置. 不幸的是,这个相机并不包含一个优秀的寻找奶牛函数,所以农夫John需要你来写一个. 农场的俯瞰图被定义为一个n * n的字符矩阵.矩阵由大写字母A到Z组成,每个字母表示一种可行 的颜色.农夫John发现一个可能是奶牛的位置(以下简称PCL)的最好定义如下: 一个PCL是一个矩阵(可能是整张图),矩阵的边

洛谷——P1821 [USACO07FEB]银牛派对Silver Cow Party

P1821 [USACO07FEB]银牛派对Silver Cow Party 题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads co

[题解]洛谷比赛『期末考后的休闲比赛2』

[前言] 这场比赛已经结束了有几天,但我各种忙,虽然AK但还是没来得及写题解.(我才不会告诉你我跑去学数据结构了) T1 区间方差 (就不贴题好了) 首先可以推公式(我们可以知道,线段树然而并不能通过初中学过的方差公式在log(L)内求出方差): (s2表示方差,L表示区间长度,xi表示区间的每一项,最后一个x上画了一根线表示这些数据的平均数) 用二项式定理完全平方公式可得: 再次展开: 另外,再代入以下这个 得到了: 然后继续吧.. 然后duang地一声合并同类项,于是我们得到了: 然后可以高

洛谷 P3106 [USACO14OPEN]GPS的决斗Dueling GPS&#39;s

P3106 [USACO14OPEN]GPS的决斗Dueling GPS's 题目描述 Farmer John has recently purchased a new car online, but in his haste he accidentally clicked the "Submit" button twice when selecting extra features for the car, and as a result the car ended up equip