dalao们求助啊!!,不管怎么弄都是#1#2#3wa,但是自检又没问题

P1047 [NOIP2005 普及组] 校门外的树

LxAnC @ 2024-02-26 22:59:10

#include<iostream>
using namespace std;
int main()
{
    int l, m,i=1,u,v;
    cin >> l >> m;
    int* p = new int[l];
    for (int i = 0; i < l; i++)
        p[i] = 1;
    while (i <= m)
    {
        cin >> u >> v;
        for (int j = u; j <= v; j++)
            if(p[j]!=0)
            p[j]--, l--;
        i++;
    }
    l += 1;
    cout << l ;
}

by LxAnC @ 2024-02-26 22:59:37

@LxAnC wa在这https://www.luogu.com.cn/record/148476804


by Manchester_Is_Blue @ 2024-02-26 23:11:41

在第9行和第10行之间加一行

i=1

by abc_abc_abc @ 2024-02-28 18:20:47

这样写(c++)

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int l,m,x,y,ans = 0;
    cin >> l >> m;
    int arr[10010];
    for (int i = 0; i <= l; i++)
    {
        arr[i] = 1;
    }
    for (int i = 0; i < m; i++)
    {
        cin >> x >> y;
        for (int j = x; j <= y; j++)
        {
            arr[j] = 0;
        }
    }
    for (int i = 0; i <= l; i++)
    {
        if (arr[i] == 1)
        {
            ans++;
        }
    }
    cout << ans;
    return 0;
}

by abc_abc_abc @ 2024-02-28 18:22:45

@Manchester_Is_Blue 牛啊 我太垃圾 cpu都干废了


by LxAnC @ 2024-02-28 21:53:54

@Manchester_Is_Blue 谢谢哥,不过加上了还是无法解决


by Manchester_Is_Blue @ 2024-02-29 16:26:58

@LxAnC

再把第7,8行改为

int* p = new int[l+1];
for (int i = 0; i <= l; i++)

by Manchester_Is_Blue @ 2024-02-29 16:27:52

@LxAnC 因为 l 这个端点也有树,但你没算进去。


by LxAnC @ 2024-03-03 21:07:35

@Manchester_Is_Blue 牛啊,我靠解决了谢谢大佬


|