LA 4851 Restaurant 找规律 -

该博客主要解析了编程竞赛题目LA 4851 Restaurant的问题,探讨了如何找到题目中隐藏的规律。内容包括对案例中的合法点进行分析,并提示要将起点A和终点B视为餐馆的一部分。博主分享了实现解题的代码片段。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目地址:http://vjudge.net/problem/UVALive-4851

找规律


案例中的合法点:

注意:A,B也算个餐馆,也要考虑进去



代码如下:

#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b)  for(int i=a;i<=(int)(b);++i)
#define REPD(i,a,b) for(int i=a;i>=(int)(b);--i)
const int maxn=60000+10;
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
struct Res
{
	int x,y;
}res[maxn];
int h1[maxn],h2[maxn];
typedef long long LL;
void Update(int i){
	REP(dx,0,maxn){
		int x1=res[i].x+dx,x2=res[i].x-dx;
		if(x1>=res[1].x&&x2<=res[0].x) break;
		if(x1<res[1].x){
			h1[x1]=min(h1[x1],max(res[i].y,2*res[0].y-res[i].y)+dx);
			h2[x1]=max(h2[x1],min(res[i].y,2*res[0].y-res[i].y)-dx);
		}
		if(x2>res[0].x) {
			h1[x2]=min(h1[x2],max(res[i].y,2*res[0].y-res[i].y)+dx);
			h2[x2]=max(h2[x2],min(res[i].y,2*res[0].y-res[i].y)-dx);
		}
	}
}
int main(int argc, char const *argv[])
{
	int T,M,n; scanf("%d",&T);
	while(T--&&scanf("%d%d",&M,&n)==2){
		
		REP(i,0,n-1) scanf("%d%d",&res[i].x,&res[i].y),res[i].x++,res[i].y++;
		if(res[0].x>res[1].x) swap(res[0],res[1]);
		if(res[1].x-res[0].x==1) {printf("0\n"); continue;}

		REP(x,res[0].x+1,res[1].x-1) {  //确定边界
			int dx0=x-res[0].x,dx1=res[1].x-x,y=res[0].y;
			h1[x]=min3(y+dx0,y+dx1,M+1);
			h2[x]=max3(y-dx0,y-dx1,0);
		}

		REP(i,0,n-1) {
			int x=res[i].x;
			if(x>res[0].x&&x<res[1].x&&res[i].y<h1[x]&&res[i].y>h2[x]) Update(i); 
		}

		LL ans=0;
		REP(x,res[0].x+1,res[1].x-1) if(h1[x]>h2[x]) ans+=h1[x]-h2[x]-1;
		printf("%lld\n", ans);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值