题意:

给出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的更多相关文章

  1. Codeforces Round #690 (Div. 3)

    第一次 ak cf 的正式比赛,不正式的是寒假里 div4 的 Testing Round,好啦好啦不要问我为什么没有 ak div4 了,差一题差一题 =.= 不知不觉已经咕了一个月了2333. 比 ...

  2. Mysql_以案例为基准之查询

    查询数据操作

  3. Educational Codeforces Round 6 F. Xors on Segments 暴力

    F. Xors on Segments 题目连接: http://www.codeforces.com/contest/620/problem/F Description You are given ...

  4. 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 ...

  5. Codeforces Round #523 (Div. 2) F. Katya and Segments Sets (交互题+思维)

    https://codeforces.com/contest/1061/problem/F 题意 假设存在一颗完全k叉树(n<=1e5),允许你进行最多(n*60)次询问,然后输出这棵树的根,每 ...

  6. hdu 5446 Unknown Treasure 卢卡斯+中国剩余定理

    Unknown Treasure Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

  7. POJ2594 Treasure Exploration

    Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 8193   Accepted: 3358 Description Have ...

  8. codeforces 677D D. Vanya and Treasure(二维线段树)

    题目链接: D. Vanya and Treasure time limit per test 1.5 seconds memory limit per test 256 megabytes inpu ...

  9. BZOJ2101: [Usaco2010 Dec]Treasure Chest 藏宝箱

    2101: [Usaco2010 Dec]Treasure Chest 藏宝箱 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 327  Solved:  ...

随机推荐

  1. 19.java设计模式之备忘录模式

    基本需求 游戏的角色有攻击力和防御力,在大战Boss之前保存自身的状态(攻击力和防御力),当大战Boss之后攻击力和防御力下降,从备忘录对象恢复到大战前的状态 传统方案 一个对象,就对应一个保存对象状 ...

  2. Flink的状态与容错

    本文主要运行到Flink以下内容 检查点机制(CheckPoint) 状态管理器(StateBackend) 状态周期(StateTtlConfig) 关系 首先要将state和checkpoint概 ...

  3. jmeter-命令行执行及测试报告导出

    问题1:GUI方式能够进行测试报告导出? 回答:目前找了很多资料,没有找到采用GUI方式测试完成,然后命令方式导出测试报告: 问题2:命令行导出测试报告的前提都有啥?---- 这里参考了老_张大大的博 ...

  4. 三. SpringCloud服务注册与发现

    1. Eureka 1.1 Eureka理解 什么是服务治理 Spring Cloud封装了Netflix公司开发的Eurkeka模块来实现服务治理 在传统的rpc远程调用框架中,管理每个服务与服务之 ...

  5. 导出带有图片的excel

    public static void main(String[] args) { try { FileOutputStream out = new FileOutputStream("d:\ ...

  6. protoc-gen-validate (PGV)

    https://github.com/envoyproxy/protoc-gen-validate This project is currently in alpha. The API should ...

  7. 【rz】【sz】参数详解

    参数 SYNOPSIS sz [-+8abdefkLlNnopqTtuvyY] file ... b:以二进制方式,默认为文本方式 e:对所有控制字符转义 待续 常见问题: 1.xshell 使用rz ...

  8. 这几个小技巧,让你书写不一样的Vue!

    前言 最近一直在阅读Vue的源码,发现了几个实战中用得上的小技巧,下面跟大家分享一下. 同时也可以阅读我之前写的Vue文章 vue开发中的"骚操作" 挖掘隐藏在源码中的Vue技巧! ...

  9. springBoot controller入参LocalDateTime

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss") @DateTimeForma ...

  10. codevs 1344 模拟退火

    1344 线型网络  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamo   题目描述 Description 有 N ( <=20 ) 台 PC 放在机房内 ...