本文主要是介绍分治,CF 1237C2 - Balanced Removals (Harder),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
一、题目
1、题目描述
2、输入输出
2.1输入
2.2输出
3、原题链接
二、解题报告
1、思路分析
2、复杂度
3、代码详解
一、题目
1、题目描述
2、输入输出
2.1输入
2.2输出
3、原题链接
https://codeforces.com/problemset/problem/1237/C2
二、解题报告
1、思路分析
考虑问题是一维:按照坐标非降排序,两个两个拿
考虑问题是二维:按照第一维坐标非降排序,按第一维分组,组内按照第二维排序,就变成了一维的问题
问题是三维:类似处理即可
2、复杂度
时间复杂度: O(NlogN)空间复杂度:O(N)
3、代码详解
#include <bits/stdc++.h>using i64 = long long;
using i32 = unsigned int;
using u64 = unsigned long long;
using i128 = __int128;constexpr int inf32 = 1E9 + 7;
constexpr i64 inf64 = 1E18 + 7;
constexpr int P = 998'244'353;void solve() {int n;std::cin >> n;std::vector<std::tuple<int, int, int, int>> points(n);for (int i = 0, a, b, c; i < n; ++ i)std::cin >> a >> b >> c, points[i] = { a, b, c, i + 1 };std::vector<int> buf2, buf3;auto help1 = [&](int l, int r) -> void{std::sort(points.begin() + l, points.begin() + r + 1, [](auto &x, auto &y) -> bool {return std::get<2>(x) < std::get<2>(y);});for (int i = l; i < r; i += 2)std::cout << std::get<3>(points[i]) << ' ' << std::get<3>(points[i + 1]) << '\n';if ((r - l + 1) & 1) {buf2.push_back(std::get<3>(points[r]));}};auto help2 = [&](int l, int r) -> void{buf2.clear();std::sort(points.begin() + l, points.begin() + r + 1, [](auto &x, auto &y) -> bool {return std::get<1>(x) < std::get<1>(y);});for (int i = l, j; i <= r; ) {j = i;while (j <= r && std::get<1>(points[j]) == std::get<1>(points[i]))++ j; help1(i, j - 1);i = j;}for (int i = 0; i + 1 < buf2.size(); i += 2)std::cout << buf2[i] << ' ' << buf2[i + 1] << "\n";if ((r - l + 1) & 1)buf3.push_back(buf2.back());};std::ranges::sort(points);for (int i = 0, j; i < n; ) {j = i;while (j < n && std::get<0>(points[j]) == std::get<0>(points[i]))++ j;help2(i, j - 1);i = j;}for (int i = 0; i < buf3.size(); i += 2)std::cout << buf3[i] << ' ' << buf3[i + 1] << '\n';
}auto FIO = []{std::ios::sync_with_stdio(false);std::cin.tie(nullptr);std::cout.tie(nullptr);return 0;
}();int main () {#ifdef DEBUGfreopen("in.txt", "r", stdin);freopen("out.txt", "w", stdout);#endifint T = 1;// std::cin >> T;while (T --) {solve();}return 0;
}
这篇关于分治,CF 1237C2 - Balanced Removals (Harder)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!