[USACO13MAR]Farm Painting【枚举】

Pro

Luogu3079

Sol

第一次打的时候,我认为是比较接近正解的,虽然正解是枚举吧,但是我感觉优化的一样啊!嗯,只是感觉。其实不然。因为我把很多可行的方案给剪下去了,所以答案就不一样了。

正解:排序是肯定的,那么,对于排序后的每一个矩形来说,能判断另一个矩形是否在该矩形之内,可以先根据左下角点的横坐标确定一个区间,然后再在这个区间里面去枚举每一个矩形,判断一下是否可以就好。

Code

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

template <typename T> inline void read(T &x) {
    int c = getchar();
    bool f = false;
    for (x = 0; !isdigit(c); c = getchar()) {
        if (c == '-') {
            f = true;
        }
    }
    for (; isdigit(c); c = getchar()) {
        x = x * 10 + c - '0';
    }
    if (f) {
        x = -x;
    }
}

void put(int x)  
{  
    int num = 0; char c[15];
    while(x) c[++num] = (x%10)+48, x /= 10;
    while(num) putchar(c[num--]);
    putchar('\n'); 
}

struct Ju {
    int x1 , y1 , x2 , y2;
    bool operator < (const Ju &a) const {
        return x1 < a.x1;
    }
};
Ju map[50005];
int n , top = 1 , ans , vis[50005];

int jud(int x , int y) {
    if(map[x].x1 > map[y].x1 && map[x].y1 > map[y].y1 && map[x].x2 < map[y].x2 && map[x].y2 < map[y].y2)
        return 1;
    return 0;
}

int main() {
    read(n);
    for(int i=1; i<=n; i++)
        read(map[i].x1) , read(map[i].y1) , read(map[i].x2) , read(map[i].y2);
    sort(map+1 , map+n+1);
    ans = n; 
    for(int i=2; i<=n; i++) {
        top = 1;
        while(map[top].x2<=map[i].x1)
            top++;
        for(int j=top; j<i; j++)
            if(jud(i , j)) {
                ans--;
                break;          
            }
    }
    put(ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43061009/article/details/82083512