80分求助啊,模仿区间合并做的,不知道哪里出现了问题!!

P1047 [NOIP2005 普及组] 校门外的树

Exile_Code @ 2023-05-23 21:39:31

#define  _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <list>
#include <string>
#include <cmath>
bool mycmp(pair<int, int> a, pair<int, int> b) {
    if (a.first == b.first) {
        return b.second < a.second;
    }
    return a.first < b.first;
}

int main()
{

    int count = 0;
    int l;
    set<pair<int, int>>sort;
    int m;
    cin >> l >> m;
    int x1, x2;
    for (int i = 0; i < m; i++)
    {
        cin >> x1 >> x2;
        sort.insert(make_pair(x1,1));
        sort.insert(make_pair(x2,-1));
    }
    int begin, end;
    int result = 0;
    for (auto e : sort)
    {
        if (count == 0)
            begin = e.first;

        if (e.second == -1)
            end = e.first;

        count +=e.second;
        if (count == 0)
            result += end - begin + 1;

    }

    cout << l+1-result << endl;
    return 0;
}

by Eliboy @ 2023-05-24 22:36:20

这题不需要这么麻烦吧


by Eliboy @ 2023-05-24 22:39:25

直接打钩


by Eliboy @ 2023-05-24 22:40:07

#include<bits/stdc++.h>
using namespace std;

int a[10005];
int main(){
    int L,m,s=0,t,w;
    cin>>L>>m;
    for(int i=0;i<=L;i++)a[i]=1;
    for(int i=1;i<=m;i++){
        cin>>t>>w;
        for(int j=t;j<=w;j++)a[j]=0;
    }
    for(int i=0;i<=L;i++) s+=a[i];
    cout<<s<<endl;
    return 0;
}

by 2789617221guo @ 2023-12-23 20:36:31

咋这么麻烦?????? 这道题很简单,可以用简单的方法做,不需要用这么复杂的算法ovo


|