求大佬帮我看一下这个代码什么意思
  • 板块学术版
  • 楼主WA_csp_noip
  • 当前回复0
  • 已保存回复0
  • 发布时间2025/1/12 14:37
  • 上次更新2025/1/12 19:00:39
查看原帖
求大佬帮我看一下这个代码什么意思
1559398
WA_csp_noip楼主2025/1/12 14:37

题目

求大佬帮我看一下下面的代码什么意思,蒟蒻看不懂。。。

#include <stdio.h>
#include <algorithm>
#include <vector>
#include <utility>
using namespace std;

int n, m, q;

int main() {
	int t;
    scanf("%d", &t);
    for (; t--; ) {
        scanf("%d%d%d", &n, &m, &q);
        vector<pair<int, int>> a;
        a.push_back({m, m});
        for (; q--; ) {
            int x;
            scanf("%d", &x);
            vector<pair<int, int>> tot;
            for (auto [l, r] : a) {
                if (x < l)
                    tot.push_back({l - 1, r});
                else if (x > r)
                    tot.push_back({l, r + 1});
                else {
                    if (l != r)
                        tot.push_back({l , r});
                    tot.push_back({1, 1}), tot.push_back({n, n});
                }
            }
            a.clear(); //记录新的一组的贡献
            sort(tot.begin(), tot.end());
            for (auto [l, r] : tot) {
                if (a.empty() || (a.back().second < l))	//若最大的小于上一组, 全更新
                    a.push_back({l, r});
                else //处理重复的情况
                    a.back().second = max(a.back().second, r);
            }
            int ans = 0;
            for (auto [l, r] : a)
                ans += (r - l + 1);
            printf("%d ", ans);
        }
        putchar('\n');
    }
}
2025/1/12 14:37
加载中...