跳到正文

题解

题解:P1003 [NOIP2011 提高组] 铺地毯

WsW_约 1 分钟
原文
文章信息与版本

文章信息

发布
2025年1月7日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 1 分钟
获赞
26
题目
P1003

版本

  1. v1当前

思路

后放的地毯在先放的地毯之上,这和栈是一样的,所以可以用栈来模拟。
按照输入顺序把地毯依次放入栈中,最后取出栈顶,如果栈顶地毯覆盖了 (x,y)(x,y) 就输出此时栈的中剩余元素数量加一;否则继续取出栈顶。

如果栈空了还没找到覆盖 (x,y)(x,y) 的地毯,就输出 -1。

时间复杂度为 O(n)O(n)。


代码

C++
#include<bits/stdc++.h>
using namespace std;
int n;
struct node{
	int sx,sy,ex,ey;
};
stack<node>s;
int mx,my;
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin>>n;
	while(n--){
		int x,y,g,k;
		cin>>x>>y>>g>>k;
		s.push({x,y,x+g,y+k});
	}
	cin>>mx>>my;
	while(!s.empty()){
		node t=s.top();
		if(t.sx<=mx&&t.ex>=mx&&t.sy<=my&&t.ey>=my){
			cout<<s.size();
			return 0;
		}
		s.pop();
	}
	cout<<-1;
	return 0;
}
原文
存档评论2 条抓取于