90分,#1WA,怎样改正啊?

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

Zzz123456789101112 @ 2024-03-07 15:54:01

#include<iostream>
using namespace std;
int main() {
    int l, m,count=0;
    int arr[100000] = { 0 };
    cin >> l >> m;
    for (int i = 0; i <= l; i++) {
        arr[i] = 1;
    }
    for (int j = 0; j < m; j++) {
        int u = 0, v = 0;
        cin >> u >> v;
        for (int i = u - 1; i < v; i++) {
            arr[i] = 0;
        }
    }
    for (int i = 0; i <= l; i++) {
        if (arr[i] == 1) {
            count++;
        }
    }
    cout << count;
    return 0;
}

by tang2010 @ 2024-03-07 16:27:44

微调```

include<bits/stdc++.h>

using namespace std; int main(void) { int l, m,count=0,a[100],b[100],j; set<int>box; int arr[100000] = { 0 }; cin >> l >> m; for (int i = 0; i < m; i++) { cin>>a[i]>>b[i]; } for(int i=0;i<m;i++) for(j=a[i];j<=b[i];j++) box.insert(j); cout <<l+1-box.size()<<endl; return 0; }


by tang2010 @ 2024-03-07 16:46:08

@Zzz123456789101112


by yinjiayu @ 2024-03-08 21:18:11

希望更丰富的展现?使用 Markdown。


by 7up0714 @ 2024-03-27 18:40:18

建议使用前缀和


|