0分,求助

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

LubsWangKillThemAll @ 2023-08-05 22:08:46

#include<bits/stdc++.h>
#define ll long long
#define N 2000001
using namespace std;
ll n=0;
ll ptime,l=0,flag[N],start[N],pend[N],count=0;
int main(){
    cin>>ptime>>n;
    for(int i=1;i<=n;i++){
        cin>>start[i]>>pend[i];
    }
    for(int i=1;i<=ptime;i++){//ptime=500
        for(int j=1;j<=n;j++){//n=3
            if(start[j]==i){
                //cout<<"find"<<start[j]<<endl;
                for(int m=start[j];m<=pend[j];m++){
                    flag[m]++;
                }
            }
        }
    }
    for(int i=1;i<=ptime;i++){
        if(flag[i]>0){
            ptime--;
        }
    }
    cout<<ptime-1;
    return 0;
}

by Akomiju @ 2023-08-05 22:34:28

#include<bits/stdc++.h>
#define ll long long
#define N 2000001
using namespace std;
ll n=0;
ll ptime,l=0,flag[N],start[N],pend[N],count=0;
int main(){
    cin>>ptime>>n;
    for(int i=1;i<=n;i++){
        cin>>start[i]>>pend[i];
    }
    for(int i=0;i<=ptime;i++){//ptime=500
        for(int j=1;j<=n;j++){//n=3
            if(start[j]==i){
                //cout<<"find"<<start[j]<<endl;
                for(int m=start[j];m<=pend[j];m++){
                    flag[m]++;
                }
            }
        }
    }

    int res = 0;
    for (int i = 0; i <= ptime; i++)
        if(flag[i] == 0)
            res++;
    cout << res << endl;

    return 0;
}

by Akomiju @ 2023-08-05 22:35:19

有//prime=500那行注释的for应该从0开始,因为起点是0不是1

最后的循环如果ptime--,那么for的结束条件也变了


|