蒟蒻求助,样例不过QWQ

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

ai_Jarvis @ 2022-08-15 23:12:08

#include<bits/stdc++.h>
using namespace std;
int l,m,a[10002],b,c,s; 
int main(){
    //freopen("b.in","r",stdin);
    //freopen("b.out","w",stdout);
    cin>>l;
    for(int i=1;i<=l*2;i++){
        cin>>b>>c;
        a[b]=1;a[c]=1;
        for(int j=b;j<=c;j++){
            if(a[j]==0)
            a[j]=1;
        }
    }
    for(int i=1;i<=l*2;i++){
        if(a[i]!=0)
            s++;
    }
    cout<<s;
    //fclose(stdin);fclose(stdout);
    return 0;
}

by metaphysis @ 2022-08-16 12:07:07

@ai_Jarvis

解题思路存在问题。

#include<bits/stdc++.h>
using namespace std;
int l,m,a[10002],b,c,s; 
int main(){
    //freopen("b.in","r",stdin);
    //freopen("b.out","w",stdout);
    //cin>>l;
    cin >> l >> m;

    //for(int i=1;i<=l*2;i++){
    for (int i = 1; i <= m; i++) {
        cin>>b>>c;
        a[b]=1;a[c]=1;
        for(int j=b;j<=c;j++){
            if(a[j]==0)
            a[j]=1;
        }
    }
    //for(int i=1;i<=l*2;i++){
    for (int i = 0; i <= l; i++) {
        if(a[i]!=0)
            s++;
    }
    //cout << s;
    cout<<(l + 1) - s;
    //fclose(stdin);fclose(stdout);
    return 0;
}

by metaphysis @ 2022-08-16 12:08:45

您是标记移走的树,那么最终需要使用总的树木数量减去移走的树木数量。输入处理也存在小问题。


by ai_Jarvis @ 2022-08-16 13:20:06

@metaphysis 谢谢谢谢,蒟蒻无能,我研究一下


by ai_Jarvis @ 2022-08-16 13:27:45

/*#include<bits/stdc++.h>
using namespace std;
int l,m,a[10002],b,c,s; 
int main(){
    //freopen("b.in","r",stdin);
    //freopen("b.out","w",stdout);
    cin>>l;
    for(int i=1;i<=l*2;i++){
        cin>>b>>c;
        a[b]=1;a[c]=1;
        for(int j=b;j<=c;j++){
            if(a[j]==0)
            a[j]=1;
        }
    }
    for(int i=1;i<=l;i++){
        if(a[i]==0)
            s++;
    }
    cout<<s;
    //fclose(stdin);fclose(stdout);
    return 0;
}*/
#include<bits/stdc++.h>
using namespace std;
int l,m,a[10002],b,c,s; 
int main(){
    //freopen("b.in","r",stdin);
    //freopen("b.out","w",stdout);
    cin >> l >> m;
    //for(int i=1;i<=l*2;i++){
    for (int i = 1; i <= m; i++) {
        cin>>b>>c;
        a[b]=1;a[c]=1;
        for(int j=b;j<=c;j++){
            if(a[j]==0)
            a[j]=1;
        }
    }
    //for(int i=1;i<=l*2;i++){
    for (int i = 0; i <= l; i++) {
        if(a[i]==0)
            s++;
    }
    cout << s;
    //fclose(stdin);fclose(stdout);
    return 0;
}

@metaphysis ,已AC,输出我稍作修改,代码如上,谢谢Dalao


|