题目大意

给定一个环,环上有一些线段,试选出最多的线段


题解

提醒:这可能是一篇非常欢乐的题解

我们考虑倍长环,然后断环为链

我们考虑枚举开头的线段,然后做一次贪心

这样子的复杂度根据实现的不同是\(O(n^2 \log n)\)或者\(O(n^2)\)

不妨假设我们不知道倍增能优化,我们考虑答案的构成,记答案为\(B\)

如果\(B < \sqrt n\),那么我们只需要每次跳\(B\)次就可以出解

如果\(B > \sqrt n\),那么我们随机取\(\frac{n}{B}\)个线段作为端点,然后取最优值

这样子,我们就得到了一个看起来完全不对劲的\(O(n \sqrt n)\)的算法

但是人都是懒惰的,我们考虑直接用第二种方法

经过实践,实际上只要取\(2\)个线段作为端点就足够\(A\)掉本题了

复杂度\(O(n \log n)\),虽然正确性完全无法保证呢

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; #define ri register int
#define rep(io, st, ed) for(ri io = st; io <= ed; io ++)
#define drep(io, ed, st) for(ri io = ed; io >= st; io --) #define gc getchar
inline int read() {
int p = 0, w = 1; char c = gc();
while(c > '9' || c < '0') { if(c == '-') w = -1; c = gc(); }
while(c >= '0' && c <= '9') p = p * 10 + c - '0', c = gc();
return p * w;
} const int sid = 2e5 + 5; int n, m, tot;
int L[sid], R[sid]; struct seg {
int l, r;
seg() {}
seg(int _l, int _r) : l(_l), r(_r) {}
friend bool operator < (seg a, seg b)
{ return a.r > b.r; }
} A[sid];
priority_queue <seg> q; inline int solve(int o) {
int ret = 0, nr = -1;
rep(i, 1, tot)
if(A[i].l >= L[o] && A[i].r <= L[o] + m) q.push(A[i]);
while(!q.empty()) {
seg B = q.top(); q.pop();
if(B.l < nr) continue;
nr = max(nr, B.r); ret ++;
}
return ret;
} int main() {
m = read(); n = read();
rep(i, 1, n) {
int l = read(), r = read();
L[i] = l; R[i] = r;
if(l > r) A[++ tot] = seg(l, r + m);
else {
A[++ tot] = seg(l, r);
A[++ tot] = seg(l + m, r + m);
}
}
int ans = 0, k = 2;
for(ri i = 1; i <= n; i += n / k) ans = max(ans, solve(i));
printf("%d\n", ans);
return 0;
}

bzoj5397 circular 随机化(的更多相关文章

  1. BZOJ.5397.circular(随机化 贪心)

    BZOJ 感觉自己完全没做过环上选线段的问题(除了一个2-SAT),所以来具体写一写qwq. 基本完全抄自remoon的题解qwq... (下标从\(0\sim m-1\)) 拆环为链,对于原线段\( ...

  2. APP漏洞扫描用地址空间随机化

    APP漏洞扫描用地址空间随机化 前言 我们在前文<APP漏洞扫描器之本地拒绝服务检测详解>了解到阿里聚安全漏洞扫描器有一项静态分析加动态模糊测试的方法来检测的功能,并详细的介绍了它在针对本 ...

  3. 如何在Spring MVC Test中避免”Circular view path” 异常

    1. 问题的现象 比如在webConfig中定义了一个viewResolver public class WebConfig extends WebMvcConfigurerAdapter { //配 ...

  4. Circular Buffer

    From:http://bradforj287.blogspot.com/2010/11/efficient-circular-buffer-in-java.html import java.util ...

  5. 在.NET Core中遭遇循环依赖问题"A circular dependency was detected"

    今天在将一个项目迁移至ASP.NET Core的过程中遭遇一个循环依赖问题,错误信息如下: A circular dependency was detected for the service of ...

  6. Circular progress bar in Unity 3D

    Circular progress bar in Unity 3D - UnityScripthttp://stackoverflow.com/questions/22662706/circular- ...

  7. rabin 素性检验 随机化算法

    #include <cstdio> #include <cstdlib> #include <ctime> typedef long long int LL; in ...

  8. A CIRCULAR PROGRESSBAR STYLE USING AN ATTACHED VIEWMODEL

    This blog post describes how to re-template the Silverlight ProgressBar control to render a circular ...

  9. Linux平台Qt creator报错:Circular all <- first dependency dropped

    在Linux下安装好Qt 5.0之后,使用Qt Creator创建了一个基于QMainWindow的框架程序.原本应该可以顺利的完成编译工作,因为自带的模板工程没有经过任何修改.可是在编译整个工程的时 ...

随机推荐

  1. elasticsearch5之Elastalert 安装使用 配置邮件报警和微信报警

    简介 Elastalert是用python2写的一个报警框架(目前支持python2.6和2.7,不支持3.x),github地址为 https://github.com/Yelp/elastaler ...

  2. 什么是Tensor

    https://blog.csdn.net/kansas_lh/article/details/79321234 tensor是tensorflow基础的一个概念——张量. Tensorflow用到了 ...

  3. mongodb中比较级查询条件:($lt $lte $gt $gte)(大于、小于)、查找条件

    查询表中学生年级大于20,如下: db.getCollection('student').find({'age':{'$gt':'20'}}) $lt    <   (less  than ) ...

  4. LVM备份(1)-创建LVM逻辑卷

    LV(Logical Volume) - 逻辑卷 VG(Volume Group) - 卷组 PV(Physical Volume) - 物理卷 1.查看分区信息:fdisk -l 可看到磁盘大小为1 ...

  5. 【机器学习】BP & softmax求导

    目录 一.BP原理及求导 二.softmax及求导 一.BP 1.为什么沿梯度方向是上升最快方向     根据泰勒公式对f(x)在x0处展开,得到f(x) ~ f(x0) + f'(x0)(x-x0) ...

  6. VMware 设置网络

    在VMware上安装 系统完成后,设置虚拟网络 这里的VMware 版本为 14. 本文以window server 2016 为例. 在虚拟机上菜单栏中, 编辑  >> 虚拟网络编辑器 ...

  7. JS正则与PHP正则

  8. burp suite 基础入门超详细教程

    介绍: 都是我个人了解到的信息,,分享给大家 欢迎指正 burp suite 被誉为web安全工具中的瑞士军刀. 大家知道,瑞士军刀,都是体积小,功能强悍,.西方军队的标配.说这么多,只是想强调这款工 ...

  9. nnet3 TDNN chunk, left-context, right-context

    chunk-width 数据块的宽度 NnetIo name=="input" indexes,left-context+num-frame+right-context=5+7+6 ...

  10. mybatis查询语句获取自增主键

    第一种方式: 主键回填useGeneratedKeys 代表采用JDBC的Statment对象的getGeneratedKeys方法返回主键keyProperty 代表将用哪个POJO的属性去匹配这个 ...