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;
}
相关推荐
Tisfy16 天前
LeetCode 3838.带权单词映射:求和、取模、拼接(附python一行版)
python·算法·leetcode·字符串·题解·模拟·取模
Tisfy21 天前
LeetCode 3689.最大子数组总值 I:What The Medium
算法·leetcode·题解·贪心·模拟·脑筋急转弯
Tisfy1 个月前
LeetCode 3300.替换为数位和以后的最小元素:一次遍历
数学·算法·leetcode·模拟
Tisfy2 个月前
LeetCode 2553.分割数组中数字的数位:模拟(maybe+翻转)——java也O(1)
java·数学·算法·leetcode·题解·模拟·取模
E等于MC平方2 个月前
AI 辅助物理课堂实验
人工智能·ai·大模型·模拟·物理·实验
Tisfy2 个月前
LeetCode 0796.旋转字符串:暴力模拟
算法·leetcode·题解·模拟·字符串匹配
Qres8212 个月前
Rabrg/artificial-life test
python·模拟
Tisfy2 个月前
LeetCode 0396.旋转函数:求diff
算法·leetcode·题解·模拟·增量法
Tisfy2 个月前
LeetCode 2833.距离原点最远的点:计数
算法·leetcode·字符串·题解·模拟·计数
浅念-2 个月前
LeetCode 模拟算法:用「还原过程」搞定编程题的入门钥匙
开发语言·c++·学习·算法·leetcode·职场和发展·模拟