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: ...
随机推荐
- 【MySQL】Last_SQL_Errno: 1594Relay log read failure: Could not parse relay log event entry...问题总结处理
备库报错: Last_SQL_Errno: 1594 Last_SQL_Error: Relay log read failure: Could not parse relay log event e ...
- 【Linux】linux的所有文件分类解析
今天看书的时候,无意间看到/dev/文件夹,以前没注意,今天去看了下发现,很多文件的开头文件属性都是一些不怎么见到的 常见的是 - 这个是代表文件,可以vim编辑的 d 这个是代表 ...
- 【Linux】Linux下如何分区及如何格式化
环境:CentOS7.1 磁盘大小是1.8T 将磁盘/dev/sda分一个分区,分区类型为xfs fdisk /dev/sda n --创建新分区 p --创建分区类型为主分区 1 --主分 ...
- SDUST数据结构 - chap1 绪论
一.判断题: 二.选择题:
- 【葵花宝典】lvs+keepalived部署kubernetes(k8s)高可用集群
一.部署环境 1.1 主机列表 主机名 Centos版本 ip docker version flannel version Keepalived version 主机配置 备注 lvs-keepal ...
- 爬虫学习(三)Chrome浏览器使用
一.新建隐身窗口 在打开隐身窗口的时候,第一次请求某个网站是没有携带cookie的,和代码请求一个网站一样,不携带cookie.这样就能够尽可能的理解代码请求某个网站的结果:除非数据是通过js加载出来 ...
- jQ实现图片无缝轮播
在铺页面的过程中,总是会遇到轮播图需要处理,一般我是会用swiper来制作,但总会有哪个几个个例需要我自己来写功能,这里制作了一个jq用来实现图片无缝轮播的dome,分享给大家ヽ( ̄▽ ̄)ノ. dom ...
- 解决windows与虚拟机ubuntu互相ping不通的问题
工作中经常用Ubuntu开发,而Ubuntu是安装在虚拟机中的,在弄网络开发的时候经常会用windows下的网络调试工具与Ubuntu中写好的网络程序进行通信,首先要保证windows与Ubuntu能 ...
- 解决 browser-sync start --server --files 文件不能同步的问题!
解决 browser-sync start --server --files 文件不能同步的问题! 请看我的源命令: browser-sync start --server --file 'test2 ...
- 我教你如何解决 Docker 下载 mcr.microsoft.com 镜像慢的办法
我教你如何解决 Docker 下载 mcr.microsoft.com 镜像慢的办法 一.介绍 最近,我在写有关使用 Jenkins 搭建企业级持续集成环境的文章,准备了四台服务器,企业级别嘛,一台就 ...