multiset 样例过,全WA,不知道op=1为啥要用迭代器
查看原帖
multiset 样例过,全WA,不知道op=1为啥要用迭代器
607834
yangCode01楼主2022/12/10 14:35
```cpp
#include<bits/stdc++.h>

using namespace std;

multiset<int> s;
multiset<int>::iterator it;
int q, op, x;
//对于op=1操作时,为啥必须要遍历迭代器,不能直接增强for暴力查找(如果不用迭代器去找全WA)
int main() {
    cin >> q;
    //对set做个边界处理
    s.insert(-2147483647);
    s.insert(2147483647);
    while (q--) {
        cin >> op >> x;
        if (op == 1) {//找值为x的排名
            int idx = -1;
            /*不能这样去找排名,会WA,为什么呢??
             * for (int t: s) {
                idx++;
                if (t == x) {
                    cout << idx << endl;
                    break;
                }
            }*/
            it=s.lower_bound(x);
            for (multiset<int>::iterator i = s.begin(); i != s.end(); ++i) {
                idx++;
                if(i==it){
                    cout << idx << endl;
                    break;
                }
            }
        } else if (op == 2) {//查排名为x的值
            int idx = -1;
            for (int t: s) {
                idx++;
                if (idx == x) {
                    cout << t << endl;
                    break;
                }
            }
        } else if (op == 3) {
            //第一个大于等于x的数
            it = s.lower_bound(x);
            it--;
            cout << *it << endl;
        } else if (op == 4) {
            //第一个大于x的数
            it = s.upper_bound(x);
            cout << *it << endl;
        } else {
            s.insert(x);
        }
    }
    return 0;
}
2022/12/10 14:35
加载中...