1. 程式人生 > 其它 >[NOIP2011 提高組 鋪地毯]

[NOIP2011 提高組 鋪地毯]

[NOIP2011 提高組] 鋪地毯

題目

傳送門:https://www.luogu.com.cn/problem/P1003

題目大意:給你n張地毯的左下角的座標\(a_{i},b_i\),可其的橫座標長度\(g_i\),縱座標長度\(k_i\)按輸入順序將他們鋪在地上。再給出一個座標\(x,y\),問能覆蓋座標\((x,y)\)的最後鋪的地毯是第幾個輸入的,沒有地毯輸出-1

資料範圍: \(0≤n≤10000\)\(0≤a,b,g,k≤100000\)

題解

難度:普及-

標籤:NOIPTG2011,暴力模擬

正解思路:因為我們只關心點的座標。所以把每張地毯的左下角座標和計算出它的右上角座標存起來,在從後往前列舉每張地毯看其是否包含座標\((x,y)\)

,是就直接輸出,結束程式,迴圈一遍後若程式還未輸出,就輸出-1。

​ 右上角座標計算方式:\((x_i+g_i,y_i+k_i)\)

​ 判斷座標是否在地毯內:\(x_i≤x≤x_i+g_i\)\(y_i≤y≤y_i+k_i\)

Code:

#include<cstdio>
using namespace std;

const int A=1e5+5;

int a[A],b[A],g[A],k[A];

int read() {
	int num=0,sign=1;
	char ch=getchar();
	while(ch<'0' or ch>'9') {
		if(ch=='-') sign=-1;
		ch=getchar();
	}
	while(ch>='0' and ch<='9') {
		num=(num<<1)+(num<<3)+(ch^48);
		ch=getchar();
	}
	return num*sign;
}

int main() {
	int n=read();
	int ans=-1;
	for(register int i=1;i<=n;i++)
		a[i]=read(),b[i]=read(),g[i]=read(),k[i]=read();
	int basex=read(),basey=read();
	for(register int i=n;i>=1;i--) {
		if(a[i]<=basex and a[i]+g[i]>=basex and b[i]<=basey and b[i]+k[i]>=basey) {
			printf("%d",i);
			return 0;
		}
	}
	printf("-1");
	return 0;
}