0分用时超了

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

RLFXRMSC @ 2023-08-08 22:21:15

#include <iostream>
using namespace std;

int main()
{
    int l;
    cin >> l;
    int trees[10000];
    int m;
    cin >> m;
    int a, b;
    for (int i = 0; i < m; i++)
    {
        cin >> a >> b;
        for (int j = a; j < b; a++)
        {
            trees[j] = 0;
        }
    }
    int num = m;
    for (int i = 0; i < l; i++)
    {
        if (trees[i] == 0)
        {
            num--;
        }
    }
    cout << num;
}

by xuzihao123 @ 2023-08-08 22:22:29

@RLFXRMSC

问题不少


by CoderMeow @ 2023-08-08 22:31:19

题解是个好东西

比如for (int j = a; j < b; a++)循环中应当更新j而非a;还有数组tree的初始化即每个元素为0,再设为0相当于没有操作。这里可以把0改成1。


by xuzihao123 @ 2023-08-08 22:33:32

#include <iostream>
#include <cstring>
using namespace std;
int main()
{
    int l;
    cin>>l;
    int trees[100003];
    for(int i=0;i<=l;i++)trees[i]=1;
    int m;
    cin>>m;
    int a,b;
    for (int i = 0; i < m; i++)
    {
        cin >> a >> b;
        for (int j=a;j<=b;j++)
        {
            trees[j]=0;
        }
    }
    int num = 0;
    for (int i = 0; i <=l; i++)
    {
        if(trees[i]==1)num++;
    }
    cout << num;
}

|