C++语法
  • 板块学术版
  • 楼主_zhangcx
  • 当前回复5
  • 已保存回复5
  • 发布时间2025/6/15 08:27
  • 上次更新2025/6/15 19:53:42
查看原帖
C++语法
1651763
_zhangcx楼主2025/6/15 08:27

这是abc410G题的题解代码,想问一下for循环里面的|符号是干嘛的,然后还有<range>库的sortlower_bound<algorithm>库中的有什么不同。此外想知道这些是哪个版本的语法

#include <vector>
#include <algorithm>
#include <iostream>
#include <ranges>

int main() {
    using namespace std;
    unsigned N;
    cin >> N;
    vector<pair<unsigned, unsigned>> intervals;
    intervals.reserve(2 * N);
    for (unsigned i{}, a, b; i < N; ++i) {
        cin >> a >> b;
        if (a > b)
            swap(a, b);
        intervals.emplace_back(a, b); // Add [a, b]
        intervals.emplace_back(b, a + 2 * N); // and [b, a + 2N]
    }
    // Sort by one endpoint
    ranges::sort(intervals, greater{}, &pair<unsigned, unsigned>::second); // 这里

    // and find an LIS (Longest Increasing Subsequence)
    vector<unsigned> lis_dp;
    for (const auto l : intervals | views::keys) // 这里
        if (const auto it{ranges::lower_bound(lis_dp, l)}; it != end(lis_dp))
            *it = l;
        else
            lis_dp.emplace_back(l);

    // The answer is the length of LIS
    cout << size(lis_dp) << endl;
    return 0;
}
2025/6/15 08:27
加载中...