用 multiset 过了,但关于查询排名的一个小问题
查看原帖
用 multiset 过了,但关于查询排名的一个小问题
932039
lzy20091001楼主2023/12/4 23:08
#include <iostream>
#include <set>

std::multiset<int> ds;

int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);

    int q;

    std::cin >> q;
    ds.insert(-2147483647);
    ds.insert(2147483647);

    while (q--)
    {
        int opt, x;

        std::cin >> opt >> x;

        if (opt == 1)
        {
            // int order = -1;
            // for (auto i : ds)
            // {
            //     order++;
            //     if (i == x)
            //         break;
            // }
            int order = 0;
            auto pos = ds.lower_bound(x);
            for (auto i = ds.begin(); i != pos; i++)
                order++;
            std::cout << order << "\n";
        }
        else if (opt == 2)
        {
            int order = 0;
            auto i = ds.begin();
            while (order < x)
            {
                i++;
                order++;
            }
            std::cout << *i << "\n";
        }
        else if (opt == 3)
            std::cout << *(--ds.lower_bound(x)) << "\n";
        else if (opt == 4)
            std::cout << *(ds.upper_bound(x)) << "\n";
        else
            ds.insert(x);
    }

    return 0;
}

查询排名部分注掉的部分只有 30 分, 下面的则 AC, 请问区别在哪?

2023/12/4 23:08
加载中...