模拟,stl,水题
本题要点:
1、定义长方形ret, 左下角(x1, y1), 右上角(x2, y2); 对于要查找的坐标 (x, y)
暴力判断某个长方形是否与之有交集。
2、定义集合 set s1, s2, 分别存放 某个长方形 的横坐标范围(x1, x2) 与 x 有交集,
纵坐标 (y1, y2) 与 y 是否有交集。 注意,集合 s1 和 s2 存的是 长方形的下标。
3、判断连个集合交集中,下标最大的(也就是放在最上面的长方形)
#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
#include <algorithm>
using namespace std;
const int MaxN = 10010;
int n, a, b, g, k, x, y;
struct ret
{
int x1, x2, y1, y2;
}r[MaxN];
void solve()
{
set<int> s1, s2;
for(int i = 1; i <= n; ++i)
{
if(r[i].x1 <= x && x <= r[i].x2)
{
s1.insert(i);
}
if(r[i].y1 <= y && y <= r[i].y2)
{
s2.insert(i);
}
}
int ans = -1;
for(set<int>::iterator it = s1.begin(); it != s1.end(); ++it)
{
if(s2.find(*it) != s2.end())
{
ans = max(ans, *it);
}
}
printf("%d\n", ans);
}
int main()
{
scanf("%d", &n);
for(int i = 1; i <= n; ++i)
{
scanf("%d%d%d%d", &a, &b, &g, &k);
r[i].x1 = a, r[i].x2 = a + g, r[i].y1 = b, r[i].y2 = b + k;
}
scanf("%d%d", &x, &y);
solve();
return 0;
}
/*
3
1 0 2 3
0 2 3 3
2 1 3 3
2 2
*/
/*
3
*/
/*
3
1 0 2 3
0 2 3 3
2 1 3 3
4 5
*/
/*
-1
*/

本博客介绍如何解决洛谷P1003题目,涉及模拟算法和STL使用。通过定义长方形并判断它们的交集,利用集合存储横纵坐标交集信息,找出覆盖最上方的长方形。
&spm=1001.2101.3001.5002&articleId=108539385&d=1&t=3&u=fb59cd011c544d72b9f1161862a9b918)
1911

被折叠的 条评论
为什么被折叠?



