[Luogu-P1003]题解(C++)

Part I Preface

原题目(Luogu)

Part II Sketch

  • 给定一个正整数 \(n\),表示地毯张数。
  • 接下来 \(n\) 行,每行 \(4\) 个整数,\(a,b,g,k\),分别表示一个地毯的左下角和右上角的坐标。
  • 最后一行,\(x,y\),表示查询的点。
  • 求出这个点上面覆盖的最上面的地毯编号。

Part III Analysis

经过观察,我们可以发现,只要一个点 \((x, y)\) 在一矩形上,那一定满足x <= a && y <= b && a >= g && b >= k。创建结构体,输入每张地毯的数据,从高到低搜索,减少时间,判断这个点是否包含于这个矩形内,即可。

Part IV Code

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e4 + 5;
int n, x, y;
int a[MAXN], b[MAXN], g[MAXN], k[MAXN];
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> a[i] >> b[i] >> g[i] >> k[i];
    }
    cin >> x >> y;
    for(int i = n; i >= 1; i--){
        if(x >= a[i] && y >= b[i] && x <= a[i] + g[i] && y <= b[i] + k[i]){
            cout << i;
            return 0;
        }
    }
    cout << -1;
}

Part V Record


Record

posted @ 2023-05-06 10:31  -沉默-  阅读(56)  评论(0)    收藏  举报