题意:

给出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. 【Oracle】Oracle 10g下载路径

    ORACLE 10g下载地址 下载方法: 直接复制下面的链接,打开迅雷,自动会识别下载的内容 Oracle Database 10g Release 2 (10.2.0.1.0) Enterprise ...

  2. 攻防世界—pwn—int_overflow

    题目分析 checksec检查文件保护机制 ida分析程序 经典整数溢出漏洞示例 整数溢出原理整数分为有符号和无符号两种类型,有符号数以最高位作为其符号位,即正整数最高位为1,负数为0, 无符号数取值 ...

  3. Inlook - 你的私人工作助理 V1.0.0.2

    Inlook - Your personal assistant 中文版|English version Introduction Inlook是为在桌面上直观地提醒用户收到未读邮件和日程安排而开发的 ...

  4. nodejs中使用worker_threads来创建新的线程

    目录 简介 worker_threads isMainThread MessageChannel parentPort和MessagePort markAsUntransferable SHARE_E ...

  5. torch.optim.SGD()各参数的解释

    看pytorch中文文档摘抄的笔记. class torch.optim.SGD(params, lr=, momentum=0, dampening=0, weight_decay=0, neste ...

  6. kvm虚拟机管理(创建、连接)

    创建虚机.远程管理kvm虚机.virsh命令行下管理虚机..kvm通过virsh console 连入虚拟机   一.创建虚机 1)打开虚拟化管理器

  7. 冷饭新炒:理解JDK中UUID的底层实现

    前提 UUID是Universally Unique IDentifier的缩写,翻译为通用唯一标识符或者全局唯一标识符.对于UUID的描述,下面摘录一下规范文件A Universally Uniqu ...

  8. Certbot CA 证书 https

    certbot (base) a@test:~# certbot --help - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ...

  9. Hash Array Mapped Trie

    Hash Array Mapped Trie   Python\hamt.c

  10. epoll在fork子进程中的问题

    epoll_create 创建的 文件描述符和其他文件描述符一样,是被fork出的子进程继承的,那也就是子进程可以使用这个epoll fd添加感兴趣的io(epoll_ctl),然后是可以影响到父进程 ...