Charles_koyi @ 2024-08-20 12:51:47
附上代码
#include<bits/stdc++.h>
using namespace std;
int l,m;
struct d{
int u,v;
}doge[101];
bool cmp(d x,d y)
{
if(x.u>y.u)
{
return x.u<y.u;
}
}
int main()
{
cin>>l>>m;
l++;
for(int i=0;i<m;i++)
{
cin>>doge[i].u>>doge[i].v;
}
sort(doge,doge+m,cmp);
for(int i=1;i<m;i++)
{
if(doge[i].u<doge[i-1].v)//去重合部分
{
doge[i].u=doge[i-1].v+1;
}
if(doge[i].v<doge[i].u)//如果全重合就删去
{
doge[i].u=0;
doge[i].v=0;
}
}
for(int i=0;i<m;i++)
{
if(doge[i].u==0 and doge[i].v==0)
{
continue;
}
l=l-(doge[i].v-doge[i].u+1);
}
cout<<l;
return 0;
}
by Lisuyang @ 2024-08-20 13:21:58
#include <iostream>
using namespace std;
int a[10006];
int main(){
int L, M, x, y, sum = 0;
scanf("%d%d", &L, &M);
for(int i = 1; i <= M; ++ i){
scanf("%d%d", &x, &y);
for(int j = x; j <= y; ++ j){
if(a[j] == 0)
a[j] = 1;
}
}
for(int i = 0; i <= L; ++ i){
if(a[i] == 0)
++ sum;
}
printf("%d", sum);
return 0;
}
by ycy130114 @ 2024-08-21 11:30:28
#include<bits/stdc++.h>
using namespace std;
int a[100010],ans;
int main()
{
memset(a,0,sizeof(a));
int l,m;
cin>>l>>m;
for(int i=0;i<=l;i++)
a[i]=0;
for(int i=1;i<=m;i++)
{
int q,p;
cin>>q>>p;
for(int j=q;j<=p;j++)
{
if(a[j]==0)
{
a[j]=1;
}
}
}
for(int i=0;i<=l;i++)
{
if(a[i]==0)
{
ans++;
}
}
cout<<ans;
return 0;
}
by wangyanxi @ 2024-08-21 23:13:26
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
bool tree[10005];
int main(){
int l, m;
cin >> l >> m;
memset(tree,true,sizeof(tree));
for (int i = 1;i <= M;i++)
{
int start,end;
cin >> start >> end;
//把范围内的数去掉
for (int j = start;j <= end;j++) tree[j] = false;
}
int cnt = 0;
for (int i = 0;i <= L;i++) //必须从0开始
cnt += tree[i];
cout << cnt;
return 0;
}
需要注意的是最后一个循环