带来个新奇算法但80分,求助!(悬关)

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

morax_zhongli @ 2024-08-04 19:48:13

include<bits/stdc++.h>

using namespace std; int s[100005]; int main() { int l,m,a,b,i,j,n=0; cin>>l>>m; for(i=1;i<=m;i++) { cin>>a>>b; for(j=a;j<=b-1;j++) { s[j]=1; } } for(i=1;i<=l;i++) { if(s[i]==0) { n++; } } cout<<n; }


by morax_zhongli @ 2024-08-04 19:49:04

#include<bits/stdc++.h>
using namespace std;
int s[100005];
int main()
{
    int l,m,a,b,i,j,n=0;
    cin>>l>>m;
    for(i=1;i<=m;i++)
    {
        cin>>a>>b;
        for(j=a;j<=b-1;j++)
        {
            s[j]=1;
        }
    }
    for(i=1;i<=l;i++)
    {
        if(s[i]==0)
        {
            n++;
        }
    }
    cout<<n;
}

by Vocaloid世末歌者 @ 2024-08-04 19:49:07

望丰展?


by Vocaloid世末歌者 @ 2024-08-04 19:49:31

好了使了 md 了


by Fur_Zes @ 2024-08-04 19:51:03

@morax_zhongli

1、希丰展,用md

2、算法不新奇

3、两个端点上的树也要算上


by zhanghengrui0502 @ 2024-08-04 19:51:18

给你发个解法:

#include <iostream>
using namespace std;
int main(){
    int i,j,k,m,n,l,sum=0;
    int a[10001],q,z; cin>>l>>m;
    for(i=0;i<=l;i++)a[i]=0;
    for(i=1;i<=m;i++){cin>>q>>z;     
    for(j=q;j<=z;j++)if(a[j]==0)a[j]++;} 
    for(i=0;i<=l;i++)if(a[i]==0)sum++;
    cout<<sum;
}

本人已试,AC


|