[レビュー] NOIPの試験のトピックLuogu P1003

思考

既然给出了每个地毯的坐标和长度,在地毯数目不是特别多的情况下,我们可以用一个结构体来存储所有地毯的信息。那么计算的时候从0开始枚举,如果这个地方有一张地毯在上面,就让答案等于它就好了。注意因为要求的是最后一张地毯是哪个,所以我们判断的范围应该一步一步缩小,保证求出来的是最终答案。

コード

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <utility>

int nextInt()
{
    int num = 0;
    char c;
    bool flag = false;
    while ((c = std::getchar()) == ' ' || c == '\r' || c == '\t' || c == '\n');
    if (c == '-')
        flag = true;
    else
        num = c - 48;
    while (std::isdigit(c = std::getchar()))
        num = num * 10 + c - 48;
    return (flag ? -1 : 1) * num;
}

int n, x, y, ans = -1;

struct dt
{
    int x, y, d1, d2;
} all[100123];

int main()
{
    n = nextInt();
    for (int i = 1; i <= n; i++)
    {
        all[i].x = nextInt();
        all[i].y = nextInt();
        all[i].d1 = nextInt();
        all[i].d2 = nextInt();
    }
    x = nextInt();
    y = nextInt();
    for(int i = 1; i <= n; i++)
    {
        int tmpx = all[i].x + all[i].d1;
        int tmpy = all[i].y + all[i].d2;
        if(x <= tmpx && x >= all[i].x && y <= tmpy && y >= all[i].y)
            ans = i; 
    }
    std::cout << ans << std::endl;
#ifdef __EDWARD_EDIT
    std::cin.get();
    std::cin.get();
#endif
    return 0;
}
公開された40元の記事 ウォンの賞賛0 ビュー5154

おすすめ

転載: blog.csdn.net/edward00324258/article/details/78387434