1 条题解

  • 0
    @ 2026-9-2 10:20:36

    思路

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

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

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


    代码

    #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;
    }
    
    • 1

    信息

    ID
    314
    时间
    1000ms
    内存
    128MiB
    难度
    5
    标签
    递交数
    19
    已通过
    14
    上传者