2025 ICPC Nanchang Invitational and Jiangxi Provincial Collegiate Programming Contest D题(离散化+差分+前缀和)

发布时间:2026/10/11 1:37:28

2025 ICPC Nanchang Invitational and Jiangxi Provincial Collegiate Programming Contest D题(离散化+差分+前缀和) 题目链接https://codeforces.com/gym/105911/problem/D题目大意给定三维空间内的若干条线段限制其端点在一给定长方体上求对于任意与坐标轴垂直的平面最多能和多少条线段相交。题目思路考虑垂直于x轴切一刀的情况对于一条线段从x1到x2它能被xc 切断当且仅当x1≤c≤x2。 所以问题转化为给定n条线段求最多有多少条线段覆盖同一位置。 那么我们将线段离散化考虑差分对于x1到x2把x1加上1x21 减去1然后求一遍前缀和即可。 y, z 轴同理代码如下:时间复杂度O(nlogn)#include bits/stdc.h using namespace std; #define ll long long #define endl \n struct segment { int x1, y1, z1; int x2, y2, z2; }; int maxoverlap(vectorpairint,intintervals) { if(intervals.empty()) { return 0; } //1.收集所有需要离散化的坐标点 vectorint coords; for(auto p:intervals) { coords.push_back(p.first);//L coords.push_back(p.second 1); // R1 } //2.排序去重 sort(coords.begin(), coords.end()); coords.erase(unique(coords.begin(), coords.end()), coords.end()); //3.差分数组 vectorint diff(coords.size(), 0); for(auto p:intervals) { int L p.first; int R p.second; int idxL lower_bound(coords.begin(), coords.end(), L) - coords.begin(); int idxR lower_bound(coords.begin(), coords.end(), R 1) - coords.begin(); diff[idxL]; diff[idxR]--; } //4.前缀和求最大值 int cur 0; int ans 0; for(auto i:diff) { cur i; ans max(ans, cur); } return ans; } void solve() { int n, a, b, c; cin n a b c; vectorsegment segs(n); for (int i 0; i n;i) { cin segs[i].x1 segs[i].y1 segs[i].z1; cin segs[i].x2 segs[i].y2 segs[i].z2; } int ans 0; //处理x方向 vectorpairint, int intervals_x; for(auto s:segs) { int L min(s.x1, s.x2); int R max(s.x1, s.x2); intervals_x.push_back({L, R}); } ans max(ans, maxoverlap(intervals_x)); //处理y方向 vectorpairint, int intervals_y; for (auto s : segs) { int L min(s.y1, s.y2); int R max(s.y1, s.y2); intervals_y.push_back({L, R}); } ans max(ans, maxoverlap(intervals_y)); //处理z方向 vectorpairint, int intervals_z; for (auto s : segs) { int L min(s.z1, s.z2); int R max(s.z1, s.z2); intervals_z.push_back({L, R}); } ans max(ans, maxoverlap(intervals_z)); cout ans endl; } signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T 1; // cin T; while (T--) { solve(); } return 0; }map简化// https: // codeforces.com/gym/105911/problem/D #include bits/stdc.h using namespace std; #define int long long #define endl \n struct node { int x, y, z; int x1, y1, z1; }; // 对某个方向求区间覆盖最多点 // coords[i]{l,r}表示第i条线段在该方向上的区间 int f(const vectorpairint, int coords) { mapint, int diff; for (auto p : coords) { diff[p.first]; diff[p.second 1]--; } int cur 0; int ans 0; for (auto p : diff) { cur p.second; ans max(cur, ans); } return ans; } void solve() { int n, a, b, c; cin n a b c; vectornode v(n); // 每个方向存一个区间数组 vectorpairint, int xs, ys, zs; // xs[i]第i条线段在x方向上的区间[min(v[i].x, v[i].x1), max(v[i].x, v[i].x1)] for (int i 0; i n; i) { cin v[i].x v[i].y v[i].z v[i].x1 v[i].y1 v[i].z1; xs.push_back({min(v[i].x, v[i].x1), max(v[i].x, v[i].x1)}); ys.push_back({min(v[i].y, v[i].y1), max(v[i].y, v[i].y1)}); zs.push_back({min(v[i].z, v[i].z1), max(v[i].z, v[i].z1)}); } int ans 0; ans max(ans, f(xs)); ans max(ans, f(ys)); ans max(ans, f(zs)); cout ans endl; } signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T 1; // cin T; while (T--) { solve(); } return 0; }
延伸阅读

更多相关文章

2026/10/11 3:57:38

Cursor 20美元订阅在Agent时代为何成了亏本生意?

我先理清这篇文章要表达的核心观点:Cursor 的 20 美元包月订阅,放在 agent 时代越来越像一门亏本生意。用户侧的亏,是活儿越来越多、额度越来越不够用;厂商侧的亏,是每个 agent 任务背后都在烧真金白银的算力。这篇文章…

2026/10/11 3:57:38

ViT小数据微调猫狗分类实战:避开5大参数坑

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/11 0:02:13

Python调用Gemini Structured Outputs实现工单路由门禁

客服工单最怕的不是模型“答错一句话”,而是它给出一段看起来合理的说明,程序却从中猜错优先级。通俗做法是:要求模型只交 JSON(JavaScript Object Notation,轻量数据格式),再让代码验证它。Gem…

2026/10/11 0:02:13

Spring Boot超市进销存系统毕设实战:从需求拆解到答辩通关

最近带的一个学生项目组里,有A同学跑来问我:选什么毕设题目最稳妥,既能让评审老师觉得工作量够,又不会在答辩时被问到语无伦次。我第一反应就是推荐基于Spring Boot的超市仓库管理系统——也就是超市进销存系统。这个题目乍一看平…

2026/10/11 0:02:13

Flutter StatefulWidget 生命周期核心解析

很多刚开始接触 Flutter 的朋友,在看完一堆“Hello World”和基础组件之后,大概率都会撞上同一堵墙:StatefulWidget 里那堆 initState、build、dispose 方法,到底什么时候被调用?为什么顺序是那样?在里面到…

2026/10/11 0:02:13

Python调用Gemini Structured Outputs实现工单路由门禁

客服工单最怕的不是模型“答错一句话”,而是它给出一段看起来合理的说明,程序却从中猜错优先级。通俗做法是:要求模型只交 JSON(JavaScript Object Notation,轻量数据格式),再让代码验证它。Gem…

2026/10/11 0:02:13

Spring Boot超市进销存系统毕设实战:从需求拆解到答辩通关

最近带的一个学生项目组里,有A同学跑来问我:选什么毕设题目最稳妥,既能让评审老师觉得工作量够,又不会在答辩时被问到语无伦次。我第一反应就是推荐基于Spring Boot的超市仓库管理系统——也就是超市进销存系统。这个题目乍一看平…

2026/10/11 0:02:13

Flutter StatefulWidget 生命周期核心解析

很多刚开始接触 Flutter 的朋友,在看完一堆“Hello World”和基础组件之后,大概率都会撞上同一堵墙:StatefulWidget 里那堆 initState、build、dispose 方法,到底什么时候被调用?为什么顺序是那样?在里面到…

还想了解更多?直接咨询顾问

免费诊断 + 免费方案 + 透明报价。

全国咨询热线400-8866-253
免费获取方案
☎咨询二维码 ☎ ↑