1 solutions

  • 1
    @ 2024-6-4 13:59:48

    1.20pts

    使用总的树的个数减去每次移走的树的个数,因为有20%的数据没有重复,所以可以得到20分

    时间复杂度 O(m)O(m)

    #include<bits/stdc++.h>
    using namespace std;
    
    int main()
    {
    	
    	int L,m;
    	cin>>L>>m; //获取马路长度和区域个数 
    	int sum=0;
    	while(m--) //获取每个移走区域 
    	{
    		int l,r;
    		cin>>l>>r;
    		sum+=r-l+1;
    	} 
    	int ans=L+1-sum;
    	cout<<ans;
        return 0;
    }
    

    2.100pts

    获取所有的移走区间,标记移走的树的每个位置,最后统计每个位置,判断是否有树没有被移走。

    时间复杂度O(nm)

    #include<bits/stdc++.h>
    using namespace std;
    const int N=10010;
    int a[N];
    int main()
    {
    	int L,m;
    	cin>>L>>m;
    	for(int i=1;i<=m;i++) //枚举区间个数 
    	{
    		int l,r;
    		cin>>l>>r;
    		for(int j=l;j<=r;j++) //标记区间l到r之间的所有数 
    		{
    			a[j]=1;
    		}
    	}
    	int c=0;
    	for(int i=0;i<=L;i++) //判断整个区间的每个位置 
    	{
    		if(a[i]==0)
    		{
    			c++;
    		}
    	}
    	cout<<c;
    	return 0;
    }
    
    • 1

    Information

    ID
    410
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    7
    Tags
    # Submissions
    23
    Accepted
    9
    Uploaded By