CF1462-F. The Treasure of The Segments
题意:
给出n个线段组成的集合,第i个线段用 \(\{l_i, r_i\}\) 表示线段从坐标轴的点\(l_i\)横跨到点\(r_i\)。现在你可以删除其中的一些线段,使得剩下的线段组成的集合中至少存在一个线段满足:这个线段与所有其他线段都相交。现在问你最少需要删除几条边可以得到满足要求的线段集合。
思路:
我们枚举每个线段,让这个线段能够与所有其他线段相交,这时候我们只需要算出不和这个边相交的线段的个数。算这个的方法也很简单,可以想一下什么情况下两个线段 \(\{l_1, r_1\}\) \(\{l_2, r_2\}\) 不相交,也就是 \(l_2 > r1\) 或者 \(r_2 < l_1\) 的情况下,两个边就不会相交。所以我们可以用两个数组来分别存储边的\(l\) 和\(r\),排序之后,就可以用二分来快速找到不与当前枚举的边相交的边的个数。
小注:
之前有看其他博主写的博客,有提到二分比较难写,主要是因为没有善用\(lower\_bound\)和\(upper\_bound\)。
AC代码:
#include <cstdio>
#include <algorithm>
#define pii pair<int, int>
#define fr first
#define sc second
const int maxn = 2e5 + 5;
const int inf = 0x3f3f3f3f;
std::pii a[maxn];
int l[maxn], r[maxn];
int main () {
int T, n;
scanf ("%d", &T);
while (T--) {
scanf ("%d", &n);
for (int i = 0; i < n; i++) {
scanf ("%d %d", &a[i].fr, &a[i].sc);
l[i] = a[i].fr;
r[i] = a[i].sc;
}
std::sort (l, l + n);
std::sort (r, r + n);
int ans = inf;
for (int i = 0; i < n; i++) {
int tot = 0;
tot = 0;
tot += (int)(std::lower_bound(r, r + n, a[i].fr) - r);
tot += n - (int)(std::upper_bound(l, l + n, a[i].sc) - l);
ans = std::min (ans, tot);
}
printf ("%d\n", ans);
}
return 0;
}
这题一开始没想到那么简洁的做法,最开始用线段树维护不和当前枚举的线段相交的个数,结果。。。T了。
杀鸡用牛刀超时未AC代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#define pii pair<int, int>
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second
const int maxn = 2e5 + 5;
const int inf = 0x3f3f3f3f;
std::pii in[maxn], num[maxn];
int lisan[maxn << 1], tot_lisan = 1;
class seg_tree {
public:
int tree[maxn << 2];
void init() {
memset (tree, 0, sizeof tree);
}
void push_up (int p) {
tree[p] = tree[p << 1] + tree[p << 1 | 1];
}
void update (int p, int l, int r, int idx) {
if (l == r) {
tree[p]++;
} else {
int mid = (l + r) >> 1;
if (idx <= mid) {
update (p << 1, l, mid, idx);
} else {
update (p << 1 | 1, mid + 1, r, idx);
}
push_up(p);
}
}
int query (int p, int l, int r, int ql, int qr) {
if (r < ql || l > qr) {
return 0;
} else if (l >= ql && r <= qr) {
return tree[p];
} else {
int mid = (l + r) >> 1;
int ans = 0;
ans += query (p << 1, l, mid, ql, qr);
ans += query (p << 1 | 1, mid + 1, r, ql, qr);
return ans;
}
}
}t1, t2;
int main () {
int T, n;
scanf ("%d", &T);
while (T--) {
t1.init();
t2.init();
tot_lisan = 1;
memset (num , 0, sizeof num);
scanf ("%d", &n);
for (int i = 0; i < n; i++) {
int l, r;
scanf ("%d %d", &l, &r);
in[i] = std::mp(l, r);
lisan[tot_lisan++] = l;
lisan[tot_lisan++] = r;
}
std::sort (lisan, lisan + tot_lisan);
std::sort (in, in + n);
tot_lisan = (int)(std::unique(lisan, lisan + tot_lisan) - lisan);
for (int i = 0; i < n; i++) {
int pl = (int)(std::lower_bound(lisan, lisan + tot_lisan, in[i].fr) - lisan);
int pr = (int)(std::lower_bound(lisan, lisan + tot_lisan, in[i].sc) - lisan);
num[i].fr = pl;
num[i].sc = pr;
t1.update (1, 0, tot_lisan, pl);
t2.update (1, 0, tot_lisan, pr);
}
int ans = inf;
for (int i = 0; i < n; i++) {
int pl = num[i].fr;
int pr = num[i].sc;
int tot = 0;
tot += t1.query (1, 0, tot_lisan, pr + 1, tot_lisan);
tot += t2.query (1, 0, tot_lisan, 0, pl - 1);
ans = std::min (ans, tot);
}
printf ("%d\n", ans);
}
return 0;
}
CF1462-F. The Treasure of The Segments的更多相关文章
- Codeforces Round #690 (Div. 3)
第一次 ak cf 的正式比赛,不正式的是寒假里 div4 的 Testing Round,好啦好啦不要问我为什么没有 ak div4 了,差一题差一题 =.= 不知不觉已经咕了一个月了2333. 比 ...
- Mysql_以案例为基准之查询
查询数据操作
- Educational Codeforces Round 6 F. Xors on Segments 暴力
F. Xors on Segments 题目连接: http://www.codeforces.com/contest/620/problem/F Description You are given ...
- Codeforces Round #524 (Div. 2) F. Katya and Segments Sets(主席树)
https://codeforces.com/contest/1080/problem/F 题意 有k个区间,区间的种类有n种,有m个询问(n,m<=1e5,k<=3e5),每次询问a,b ...
- Codeforces Round #523 (Div. 2) F. Katya and Segments Sets (交互题+思维)
https://codeforces.com/contest/1061/problem/F 题意 假设存在一颗完全k叉树(n<=1e5),允许你进行最多(n*60)次询问,然后输出这棵树的根,每 ...
- hdu 5446 Unknown Treasure 卢卡斯+中国剩余定理
Unknown Treasure Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- POJ2594 Treasure Exploration
Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 8193 Accepted: 3358 Description Have ...
- codeforces 677D D. Vanya and Treasure(二维线段树)
题目链接: D. Vanya and Treasure time limit per test 1.5 seconds memory limit per test 256 megabytes inpu ...
- BZOJ2101: [Usaco2010 Dec]Treasure Chest 藏宝箱
2101: [Usaco2010 Dec]Treasure Chest 藏宝箱 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 327 Solved: ...
随机推荐
- rm: cannot remove `/tmp/localhost-mysql_cacti_stats.txt': Operation not permitted
[root@DBslave tmp]# chown zabbix.zabbix /tmp/localhost-mysql_cacti_stats.txt
- element el-table表格的vue组件二次封装(附表格高度自适应)
基于vue的el-table表格二次封装组件方法 前言 在公司实习使用vue+element-ui框架进行前端开发,使用表格el-table较为多,有些业务逻辑比较相似,有些地方使用的重复性高,如果多 ...
- ORA-00245 control file backup operation failed 分析和解决
一.问题说明 操作系统: RedHat 5.8 数据库: 11.2.0.3 2节点RAC. 使用RMAN 备份的时候,报如下错误: ORA-00245: control file backup fai ...
- 【CRS】vipca最后一步执行报错CRS-0215
当我们在安装Clusterware 的时候, 需要在第二节点上vipca , 配置到最后安装的时候, 安装到 75% 左右,报错: CRS-0215 : Could not start res ...
- RSA共模攻击
在安恒月赛中碰到一道密码学方向的ctf题 附上源码 from flag import flag from Crypto.Util.number import * p=getPrime(1024) q= ...
- RabbitMQ六种工作模式有哪些?怎样用SpringBoot整合RabbitMQ
目录 一.RabbitMQ入门程序 二.Work queues 工作模式 三.Publish / Subscribe 发布/订阅模式 四.Routing 路由模式 五.Topics 六.Header ...
- rbd-db数据迁移至外部数据库
部署外部数据库 安装Docker export VERSION=19.03 && curl -fsSL http://rainbond-pkg.oss-cn-shanghai.aliy ...
- Connection Manager简称connman
ConnMan Connection Manager简称connman,connman是使用d-bus做为进程间通信机制来管理Linux网络链接的一种软件.在connman的d-bus接口中,有 ...
- 在Sublime Text 2工具下编辑laravel框架
介绍Sublime编辑器 Sublime Text 3官方版是Sublime Text2的升级版.Sublime Text是一款流行的文本编辑器软件,有点类似于TextMate,跨平台,可运行在Lin ...
- In Search of an Understandable Consensus Algorithm" (https://raft.github.io/raft.pdf) by Diego Ongaro and John Ousterhout.
In Search of an Understandable Consensus Algorithm" (https://raft.github.io/raft.pdf) by Diego ...