weichenyu @ 2023-07-09 10:21:26
蒟蒻求助,第一个测试点没过
#include<bits/stdc++.h>
using namespace std;
int main(){
int a[100001],z;
memset(a,0,sizeof(a));
int l,f,b,c;
cin>>l>>f;
for(int k=1;k<=f;k++){
cin>>b>>c;
for(int i=b;i<=c;i++){
a[i]++;
}
}
for(int i=1;i<=l;i++){
if(a[i]>1)
a[i]=1;
z+=a[i];
}
cout<<l-z+1;
return 0;
}
by WhitD @ 2023-07-09 10:31:33
第二个for循环要从0开始统计\ for(int i=0;i<=l;i++)
by Chenrunqi135 @ 2023-07-09 10:33:50
#include <iostream>
using namespace std;
int main() {
int l, q;
int top, bot;
int sum = 0;
cin >> l >> q;
int owl[++l];
for (int i = 0; i < l; i++) {
owl[i] = 1;
}
for (int j = 0; j < q; j++) {
cin >> bot >> top;
for (int ij = bot; ij < top + 1; ij++) {
owl[ij] = 0;
}
}
for (int iji = 0; iji < l; iji++) {
if (owl[iji] == 1) {
sum++;
}
}
cout << sum;
return 0;
}
by Intro_zzz_ @ 2023-07-09 10:34:01
@weichenyu
因为数轴从0开始,所以
for(int i=1;i<=l;i++){
改成
for(int i=0;i<=l;i++){
就过了。
by hexuchen @ 2023-07-09 10:37:01
@weichenyu 我对比了下我的代码与你的,其实你大部分都对,但第二个循环得从0开始。
我的AC代码:
#include <bits/stdc++.h>
using namespace std;
int main(){
int l,m,u,v,a[10005]={1},sum=0;
cin>>l>>m;
for(int i=0;i<=l;i++){
a[i]=1;
}
for(int i=1;i<=m;i++){
cin>>u>>v;
for(int j=u;j<=v;j++){
a[j]=0;
}
}
for(int k=0;k<=l;k++){
if(a[k]==1)
sum++;
}
cout<<sum;
return 0;
}
其实不用那么麻烦
by Chenrunqi135 @ 2023-07-09 10:37:21
第二个for循环要从零开始
by Chenrunqi135 @ 2023-07-09 10:38:49
#include<bits/stdc++.h>
using namespace std;
int main(){
int a[100001],z;
memset(a,0,sizeof(a));
int l,f,b,c;
cin>>l>>f;
for(int k=1;k<=f;k++){
cin>>b>>c;
for(int i=b;i<=c;i++){
a[i]++;
}
}
for(int i=0;i<=l;i++){
if(a[i]>1)
a[i]=1;
z+=a[i];
}
cout<<l-z+1;
return 0;
}
by weichenyu @ 2023-07-09 10:39:40
谢谢
by Lv_Boxiu @ 2023-07-09 10:45:12
#include<bits/stdc++.h>
using namespace std;
int main(){
int a[100001],z;
memset(a,0,sizeof(a));
int l,f,b,c;
cin>>l>>f;
for(int k=1;k<=f;k++){
cin>>b>>c;
for(int i=b;i<=c;i++){
a[i]=1;
}
}
for(int i=0;i<=l;i++){
if(!a[i]){
z++;
}
}
cout<<z;
return 0;
}