[luogu 2862] [USACO06JAN] Corral the Cows

Title:

I am a hyperlink

Explanation:

I don't know why I put it in tarjan's classification
Similar to a two-dimensional queue.
Sort + Discrete (of course, this problem does not need discrete data, but discrete can save space?)
After discretization, y coordinates have been arranged in order. Fix the interval with two pointers a and b. By the way, mark the x in the interval clearly.
So let's start fixing the interval of X. I fix an interval and then look directly at the intersection of two fixed intervals --- how many x directly accumulate in the range.

Code:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
struct hh{int x,y;}p[505];
int mx,my,rx[505],ry[505],n,cc,ans,s[505];
int cmp(hh a,hh b){return a.x<b.x;}
int cmp1(hh a,hh b){return a.y<b.y;}
bool check(int mid)
{    
    memset(s,0,sizeof(s));
    int a=1,b=0;
    while (b<n && ry[p[b+1].y]-ry[1]+1<=mid) s[p[++b].x]++;
    for (;b<=n;s[p[++b].x]++)
    {
        while (ry[p[b].y]-ry[p[a].y]+1>mid) s[p[a++].x]--; 
        int c=1,d=0,sd=0,sc=0;
        while (d<mx && rx[d+1]-rx[1]+1<=mid) sd+=s[++d];
        for (;d<=mx;sd+=s[++d])
        {
            while (rx[d]-rx[c]+1>mid) sc+=s[c++];
            if (sd-sc>=cc) return 1;  
        }
    }
    return 0;
}
int main()
{
    int i;
    scanf("%d%d",&cc,&n);
    for (i=1;i<=n;i++) scanf("%d%d",&p[i].x,&p[i].y);
    sort(p+1,p+n+1,cmp); 
    for (i=1;i<=n;i++) 
    {
        if (p[i].x>rx[mx]) rx[++mx]=p[i].x;
        p[i].x=mx;
    }
    sort(p+1,p+n+1,cmp1);//dispersed 
    for (i=1;i<=n;i++) 
    {
        if (p[i].y>ry[my]) ry[++my]=p[i].y;
        p[i].y=my;
    }
    int l=1,r=max(rx[mx],ry[my]);
    while (l<=r)
    {
        int mid=(l+r)>>1;
        if (check(mid)) ans=mid,r=mid-1;
        else l=mid+1;
    } 
    printf("%d",ans);
}

Posted by fredcool on Fri, 08 Feb 2019 17:03:17 -0800