KY24 剩下的树

KY24 剩下的树

⭐️难度:中等(其实简单)

⭐️类型:模拟

📖题目:题目链接

📚题解:

要提前填充固定个0进数组,所以用动态数组,静态数组做不到。

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<vector>  // vector不需要.h
#include<list>
using namespace std;

int main() {
    int l, m;
    scanf("%d%d", &l, &m);

    vector<int> vec(l + 1); // 填充(l+1)个0进数组,只能用动态数组
    for (int i = 0; i < m; i++) {
        int left, right;
        scanf("%d%d", &left, &right);
        for (int j = left; j <= right; j++) {
            vec[j] = 1;
        }
    }

    int sum = 0;
    for (int i = 0; i <= l; i++) {
        if (vec[i] != 1) {
            sum++;
        }
    }
    printf("%d", sum);

    return 0;
}
相关推荐
Tisfy10 天前
LeetCode 2553.分割数组中数字的数位:模拟(maybe+翻转)——java也O(1)
java·数学·算法·leetcode·题解·模拟·取模
E等于MC平方17 天前
AI 辅助物理课堂实验
人工智能·ai·大模型·模拟·物理·实验
Tisfy18 天前
LeetCode 0796.旋转字符串:暴力模拟
算法·leetcode·题解·模拟·字符串匹配
Qres82120 天前
Rabrg/artificial-life test
python·模拟
Tisfy21 天前
LeetCode 0396.旋转函数:求diff
算法·leetcode·题解·模拟·增量法
Tisfy1 个月前
LeetCode 2833.距离原点最远的点:计数
算法·leetcode·字符串·题解·模拟·计数
浅念-1 个月前
LeetCode 模拟算法:用「还原过程」搞定编程题的入门钥匙
开发语言·c++·学习·算法·leetcode·职场和发展·模拟
qeen871 个月前
【算法笔记】模拟与高精度加减乘除
c++·笔记·算法·高精度·模拟
Q741_1471 个月前
每日一题 力扣 1848. 到目标元素的最小距离 模拟 C++题解
c++·算法·leetcode·模拟
Tisfy1 个月前
LeetCode 3740.三个相等元素之间的最小距离 I:今日先暴力,“明日“再哈希
算法·leetcode·哈希算法·题解·模拟·遍历·暴力