跪求大佬指点迷津,20分求助

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

R13541444819 @ 2023-04-02 22:36:54

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int l = scan.nextInt() + 1;
        int m = scan.nextInt();
        int arr[] = new int[2 * m];
        for (int i = 0; i < arr.length; i++){
            arr[i] = scan.nextInt();
        }
        for (int i = 2; i < arr.length; i = i + 2) {
            if (arr[i] < arr[0] && arr[i + 1] > arr[0] && arr[i + 1] < arr[1]){
                arr[0] = arr[i];
            }else if (arr[i] >arr[0] && arr[i] < arr[1] && arr[i + 1] > arr[1]) {
                arr[1] = arr[i + 1];
            }else{
                continue;
            }
            arr[i] = arr[i + 1] = 0;
        }
        int sum = 0;
        for (int i = 1; i < arr.length; i = i + 2){
            if (arr[i] == 0) {
                continue;
            }
            sum += arr[i] - arr[i - 1] + 1;
        }
        System.out.println(l - sum);
    }
}

by a15244412002 @ 2023-04-16 20:20:56

AC

#include<iostream>
using namespace std;
int main(){
    int l,m,start,end,a[100000],ans=0;
    cin>>l>>m;
    for(int i=0;i<=l;i++){
        a[i]=1;
    }//0可种树//遍历
    for(int i=1;i<=m;i++){
        cin>>start>>end;
        if(start==1&&end==l){
            cout<<"0";
            return 0;
        }//不加第一个不AC
        for(int j=start;j<=end;j++){
            a[j]=0;
        }//可以重复拔
    }
    for(int i=0;i<=l;i++){
        if(a[i]==1){
            ans++;
        }
    }//累加
    cout<<ans;
    //cout is 298
    //----wrong----
    /*for(int i=1;i<=l;i++){
        a[i]=1;
    }
    for(int i=1;i<=m;i++){
        cin>>start>>end;
        for(int j=start;j<=end;j++){
            a[j]=0;
        }
    }
    for(int i=1;i<=l;i++){
        if(a[i]==1){
            ans++;
        }
    }
    cout<<ans;*/
    //cout is 297
    return 0;
}

|