列表

详情


NC216123. 小C的飞机跑道

描述

寒假到了,小C家里有很大一条飞机跑道。
想清理一下积雪 但是小C很喜欢雪不想全部清理掉。
所以找了工人来清理 这群工人有强迫症,清理的时候只会清理整数的米数。
而且还喜欢偷懒 反复清理被清理过的地方。
现在已知道飞机跑道长度L 这些工人清理的次数N 清理的起点Q终点P 现在小C想知道没有被清理的长度。(终点也会被清理哦 比如从1清到9 1和9也会被清理)

输入描述

共N+1行输入
第一行为两个整数
1<=L<=10000
1<=N<=100
以下n行为Q与P
Q<=P<=L

输出描述

输出一个整数 代表没有被清理的长度

示例1

输入:

1000 2     
1 3 
3 100

输出:

900

示例2

输入:

1000 5     
1 9
10 12
16 18
100 333
369 500

输出:

619

说明:

1 9 10 12

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++(g++ 7.5.0) 解法, 执行用时: 3ms, 内存消耗: 488K, 提交时间: 2023-01-08 13:35:21

#include <iostream>
using namespace std;
int main(){
    int a,b,x[10001]={0},m,n,c=0;
    cin>>a>>b;
    for(int i=0;i<b;i++){
        cin>>m>>n;
        for(int o=m;o<=n;o++){
            if(x[o]==0){x[o]++;c++;}
        }
    }
    cout<<a-c;
    return 0;
}

C++ 解法, 执行用时: 3ms, 内存消耗: 400K, 提交时间: 2021-05-18 20:33:06

#include<iostream>
using namespace std;
bool st[1000010];
int main()
{
	int  n,m,res=0;
	cin>>n>>m;
	while(m--)
	{
		int  a,b;
		cin>>a>>b;
		for(int i=a;i<=b;i++) st[i]=true;
	}
	while(n--)
		if(!st[n+1])res++;
	cout<<res;
	return 0;
}

C(clang11) 解法, 执行用时: 2ms, 内存消耗: 352K, 提交时间: 2020-12-27 14:22:25

#include<stdio.h>
main()
{
	int l,i,k,j,m=0;
	int a[10001]={0};
	scanf("%d %d",&l,&k);
	while(k--)
	{
		scanf("%d %d",&i,&j);
		for(i;i<=j;i++)
		{
			a[i]=1;
		}	
	}
	for(i=0;i<l;i++)
	if(a[i]==0)m++;
	printf("%d",m);
 } 

Python3(3.9) 解法, 执行用时: 130ms, 内存消耗: 18480K, 提交时间: 2020-12-27 14:10:08

l,n=map(int,input().split())
s=[]
ll=[]
for i in range(n):
    s.append(list(map(int,input().split())))

    
for i in s:
    ll=ll+list(range(i[0],i[1]+1))
sss=list(set(ll))
print(l-len(sss))

上一题