题解 | 校门外的树-NOIP2005普及组复赛

校门外的树

https://ac.nowcoder.com/acm/contest/233/B

解题思路:

用布尔数组表示每个树是否被移走。对于每个操作直接扫描区间中的所有位置即可。

C++ 代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int N = 10010;
 
int n, m;
bool st[N];
 
int main()
{
    scanf("%d%d", &m, &n);
    while (n--)
    {
        int l, r;
        scanf("%d%d", &l, &r);
        for (int i = l; i <= r; i++) st[i] = true;
    }
 
    int res = 0;
    for (int i = 0; i <= m; i++)
        if (!st[i])
            res++;
 
    printf("%d\n", res);
 
    return 0;
}


另外,牛客暑期NOIP真题班限时免费报名
报名链接:https://www.nowcoder.com/courses/cover/live/248
报名优惠券:DCYxdCJ
全部评论

相关推荐

2025-12-26 14:44
复旦大学 Java
点赞 评论 收藏
分享
2025-12-27 22:21
门头沟学院 Java
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务