第一个点WA了,求大佬指点

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

sj143714 @ 2022-10-02 20:33:55

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

by Wy_x @ 2022-10-02 21:01:48

  1. 树的编号为 0-l (包括 l
  1. cb ,不是从 c-1b-1

改动地方加了注释,改后AC代码:

#include <bits/stdc++.h>
using namespace std;
bool a[10005];
int main()
{
    int n;
    cin >> n;
    int m;
    cin >> m;
    for (int i = 0 ; i <= n ; i++)//改为 i<=n
    {
        a[i] = true;
    }
    int cnt = 0;
    for (int i = 1 ; i <= m ; i++)
    {
        int c, b;
        cin >> c >> b;
        for (int i = c; i <= b ;i++)
        {//从 c-b 枚举
            if(a[i] == true)
            {
                cnt++;
                a[i] = false;
            }
        }
    }
    cout << n + 1 - cnt  << endl;
}

by Wy_x @ 2022-10-02 21:01:59

@sj143714


by sj143714 @ 2022-10-03 08:13:38

@wangyixuan20090401 谢谢,过了


by RenSiYu6 @ 2022-10-03 16:11:37

#include <iostream>
using namespace std;
int main ()
{
int l,m;
cin>>l>>m;
int *p=new int [l+1];
for(int i=0;i<l+1;i++)
*(p+i)=1;
for(int i=0;i<m;i++)
{
int temp1,temp2;
cin>>temp1>>temp2;
for(int j=temp1;j<=temp2;j++)
*(p+j)=0;
}
int count=0;
for(int i=0;i<l+1;i++)
if(*(p+i)) count++;
cout<<count<<endl;
return 0;
 } 

|