求助,只对了第一个

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

albertww @ 2022-05-31 13:25:05

#include <bits/stdc++.h>
using namespace std;
int n,m,a[105],b[105],sum=0;
bool k[1005];
int main()
{
    cin >> n >> m;
    for (int i=1;i<=m;i++)
        cin >> a[i] >> b[i];
    for (int i=1;i<=n;i++)
        k[i]=true;
    for (int i=1;i<=m;i++)
        for (int j=a[i];j<=b[i];j++)
            k[j]=false;
    for (int i=1;i<=n;i++)
        if (k[i])
            sum++;
    cout << sum;
    return 0;
}

各位大佬帮忙看看


by 大眼仔Happy @ 2022-05-31 13:26:29

@albertww 数组开小了


by Gilly985 @ 2022-05-31 13:29:30

@大眼仔Happy

没用的

#include <bits/stdc++.h>
using namespace std;
long long n,m,a[100005],b[100005],sum=0;
bool k[100005];
int main()
{
    cin >> n >> m;
    for (int i=1;i<=m;i++)
        cin >> a[i] >> b[i];
    for (int i=1;i<=n;i++)
        k[i]=true;
    for (int i=1;i<=m;i++)
        for (int j=a[i];j<=b[i];j++)
            k[j]=false;
    for (int i=1;i<=n;i++)
        if (k[i])
            sum++;
    cout << sum;
    return 0;
}

还是错


by albertww @ 2022-05-31 13:31:18

@大眼仔Happy

变成

#include <bits/stdc++.h>
using namespace std;
int n,m,a[105],b[105],sum=0;
bool k[10005];
int main()
{
    cin >> n >> m;
    for (int i=1;i<=m;i++)
        cin >> a[i] >> b[i];
    for (int i=1;i<=n;i++)
        k[i]=true;
    for (int i=1;i<=m;i++)
        for (int j=a[i];j<=b[i];j++)
            k[j]=false;
    for (int i=1;i<=n;i++)
        if (k[i])
            sum++;
    cout << sum;
    return 0;
}

后从RE变为WA了


by 大眼仔Happy @ 2022-05-31 13:31:30

@Gilly985

for (int i=0;i<=n;i++)

by 大眼仔Happy @ 2022-05-31 13:33:11

……

原来有两个人


|