跳到正文

个人记录

[POJ-3179] && [P2862] Corral the Cows 题解报告

くろねこ约 5 分钟
原文
文章信息与版本

文章信息

发布
2019年1月5日
收录
2026年6月19日
更新
2026年11月17日
篇幅
约 5 分钟

版本

  1. v1当前

原题传送门!

本题采用二分边长,按横坐标从小到大排序,二分后枚举该边长内所有满足条件的草场. 注意,我们只用横坐标排序,完全没有考虑关于纵坐标的情况,所以可能有纵坐标超限的情况,一定要加以判断


(这里是蒟蒻的某个美妙的70分想法以及自我hack,配合代码食用更佳)

我们是不是可以直接横纵坐标一起枚举然后直接看中间有没有那么多点呢qwq

C++
 rg int tot = 0,maxx = x + cow[i].x - 1,maxy = x + cow[i].y - 1;
        for (rg int j = i;j <= n;++j)
        {
            if (cow[j].x <= maxx && cow[j].y <= maxy) ++tot;
            else break;
            if (tot == c) return true;

然后70分 理由如下..

qwq

这种写法就是绿色部分被我吃了…


写完正解,然后又有美妙的想法:

我是不是可以直接判断这些点到起始点的距离满不满足边长呢qwq

C++
for (rg int j = tot;j >= c;--j)
{
	if (abs(cow[mem[j]].y - cow[i].y) + 1 <= x) return true;              
}

然后开心题解,70分*2

理由如下..

qwq

绿色点纵坐标那么小,但他还是不在边长内qwq…


C++
//二分边长 
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <queue>
using namespace std;
#define rg register
#define LL long long
template <typename qwq> inline void read(qwq & x)
{
	x = 0;
	rg int f = 1;
	rg char c = getchar();
	while(c < '0' || c > '9')
	{
		if (c == '-') f = -1;
		c = getchar();
	}
	while(c >= '0' && c <= '9')
	{
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	x *= f;
}
template <typename qaq> inline void print(qaq x)
{
	if (x < 0)
	{
		putchar('-');
		x = -x;
	}
	if (x > 9) print(x / 10);
	putchar(x % 10 + '0');
}
struct node
{
	int x,y;
}cow[233333];
int c,n,l = 1,r = 20500,ans;
inline bool cmp(node x,node y)//按照x左标小的排序 
{
	if (x.x == y.x) return x.y < y.y;
	return x.x < y.x;
}
inline bool cmmp(int x,int y)//由于要从后往前扫,故按从小到大排序 
{
	return cow[x].y < cow[y].y;
}
int mem[233333];
inline bool check(int x)
{
	for (rg int i = 1;i <= n;++i)//枚举当前草地 
	{
		rg int tot = 0,maxx = x + cow[i].x - 1;//maxx:当前矩阵的最大横坐标 
		for (rg int j = i;j <= n;++j)
		{
			if (cow[j].x <= maxx) mem[++tot] = j;
			else break;
//			if (tot == c) break;
		}
		if (tot >= c)//由于排序时我们没有考虑有关y坐标的问题,故在此判断 
		{
			stable_sort(mem + 1,mem + 1 + tot,cmmp);
			for (rg int j = tot;j >= c;--j)//只需判断这些y最大的点即可,小于c的话不满足条件了,可以直接跳过 
			{
				if (cow[mem[j]].y - cow[mem[j - c + 1]].y + 1 <= x) return true; 
				//由于已经用y排了序,故j - c + 1到 j 之间一定有c个点(包含端点),只要有1组满足,说明该边长可以实现
				//但如果不行的话,第c - 1个点已经不满足 c 个点,故再考虑下一个点 
			}
		}
	}
	return false;
}
int main()
{
	read(c),read(n);
	for (rg int i = 1;i <= n;++i)
	{
		read(cow[i].x),read(cow[i].y);
//		r = max(r,max(cow[i].x,cow[i].y));
	}
	stable_sort(cow + 1,cow + 1 + n,cmp);
	while (l <= r)
	{
		rg int mid = (l + r) >> 1;
		if (check(mid)) ans = mid,r = mid - 1;
		else l = mid + 1;
	}
	print(ans);
}
原文