铺地毯

原题链接:https://www.luogu.org/problem/show?pid=1003

从讲义上的离散化部分看到的这个题,还以为用啥高级操作才能过的呢。。其实就是离散化

仔细一看。。诶我怎么做过这道题?

其实很简单。首先这个数据范围要模拟铺地毯二维数组肯定开不了。那么我们想用其他方式记录一下每一块地毯的相关信息,离线处理答案。

开一个结构体就好,记录每块地毯的左下角坐标,在x和y方向的长度。

我们知道要求的(x,y),我们枚举所有地毯,每次枚举判断这个地毯能不能覆盖这个点,如果可以就更新答案。

答案可以直接初始化为-1,因为无解的时候肯定是没法更新的。

(这样的题加读优是不是大材小用了?)

参考代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #define maxn 10010
 5 using namespace std;
 6 int n,x,y;
 7 int ans = -1;
 8 struct di_tan{
 9     int x,y;
10     int d1,d2;
11 };
12 di_tan a[maxn];
13 inline int read(){
14     int num = 0;
15     char c;
16     bool flag = false;
17     while ((c = getchar()) == ' ' || c == '
' || c == '
');
18     if (c == '-')
19         flag = true;
20     else
21         num = c - '0';
22     while (isdigit(c = getchar()))
23         num = num * 10 + c - '0';
24     return (flag ? -1 : 1) * num;
25 }
26 
27 int main(){
28     n = read();
29     for (register int i=1;i<=n;i++){
30         a[i].x = read();
31         a[i].y = read();
32         a[i].d1 = read();
33         a[i].d2 = read();
34     }
35     x = read(); y = read();
36     for (register int i=1;i<=n;i++){
37         int tmpx = a[i].x + a[i].d1;
38         int tmpy = a[i].y + a[i].d2;
39         if (x <= tmpx && x >= a[i].x && y <= tmpy && y >= a[i].y)
40             ans = i;
41     }
42     printf("%d
",ans);
43     return 0;
44 }
原文地址:https://www.cnblogs.com/OIerShawnZhou/p/7702443.html