Celestinefly @ 2024-07-01 14:03:43
#include <bits/stdc++.h>
using namespace std;
const int N = 100;
typedef pair<int, int> ii;
int l, m;
queue<ii> q;
ii mm[N];
int main() {
scanf("%d %d", &l, &m);
for (int i = 0; i < m; i++) {
scanf("%d %d", &mm[i].first, &mm[i].second);
}
sort(mm, mm + m);
q.push(mm[0]);
for (int i = 1; i < m; i++) {
auto current = q.back();
q.pop();
if (mm[i].first <= current.second) {
current.second = max(current.second, mm[i].second);
q.push(current);
} else {
q.push(current);
q.push(mm[i]);
}
}
int sum = 0;
while (!q.empty()) {
auto n = q.front();
q.pop();
sum += n.second - n.first + 1;
}
printf("%d\n", l + 1 - sum);
return 0;
}
wa了1和2,求助
by libohan356218 @ 2024-07-01 14:18:16
这样不好标记
by libohan356218 @ 2024-07-01 14:21:32
这样可以AC(用数组标记,刚开始全为1,为地铁部分则标为0,最后统计一下1的数量)
#include <bits/stdc++.h>
using namespace std;
const int N = 100;
int a[10005],s,r,ans = 0;
int main() {
int l,m;
scanf("%d %d", &l, &m);
for(int i = 0;i<=l;i++){
a[i] = 1;
}
for(int i = 0;i<m;i++){
cin>>s>>r;
for(int j = s;j<=r;j++){
a[j] = 0;
}
}
for(int i = 0;i<=l;i++){
if(a[i] == 1){
ans++;
}
}
cout<<ans;
return 0;
}
by libohan356218 @ 2024-07-01 14:21:58
@Celestinefly
by Celestinefly @ 2024-07-01 14:38:29
@libohan356218 谢谢你,关注了