题解 P1003 【铺地毯】

智子

2019-06-02 03:30:00

Solution

看标签 > 枚举,模拟,暴力。 首先,很容易想到开一个大小 $N * N$ 的二维数组,然而数据范围是 $0 ≤ N ≤10,000$ [提交记录R21442990](https://www.luogu.org/fe/record/21442990) ```cpp #include<iostream> #include<cstring> using namespace std; const int MAXN = 10000 + 5; int v[MAXN][MAXN]; //爆0的罪魁祸首 int main() { memset(v, -1, sizeof(v)); int n; cin >> n; for(int i = 1; i <= n; i++) { int a, b, g, k; cin >> a >> b >> g >> k; for(int x = a; x <= a + g; x++) { for(int y = b; y <= b + k; y++) { v[x][y] = i; } } } int x, y; cin >> x >> y; cout << v[x][y] << endl; return 0; } ``` ~~成功MLE~~ 数组大小:$4 * 10000 * 10000 = 400000000 Byte = 400000 KB = 400MB$ 跨过了MLE的红线。 另外一种方法是用四个数组来记录矩形的四个参数,再扫一遍数组,通过检查坐标 $(x, y)$ 是否在矩形 $Rect_i$ 内,更新最上面的地毯。 [提交记录](https://www.luogu.org/record/show?rid=17813771) 亲测,完美AC。 至于矩形 $Rect_i$ 的存储方法,可以用 $a, b, g, k$ 四个数组分别存放左上角的坐标和左上角与右下角的距离。 ```cpp #include<stdio.h> const int MAXN = 10000 + 5;//程序里出现幻数可不是好习惯哦~ int a[MAXN], b[MAXN], g[MAXN], k[MAXN]; int main() { int n, x, y; scanf("%d", &n); for(int i = 0; i < n; i++) { scanf("%d%d%d%d", &a[i], &b[i], &g[i], &k[i]);//输入 } scanf("%d%d", &x, &y); int ans = -1; for(int i = 0; i < n; i++) { if(x >= a[i] && y >= b[i] && x <= a[i] + g[i] && y <= b[i] + k[i]) { ans = i + 1;//ans的最终值恰好是最上面的那张地毯编号 } } printf("%d\n", ans);//输出结果 return 0; } ```