【题解】Cut the Sequence(贪心区间覆盖)
【题解】Cut the Sequence(贪心区间覆盖)
题意:
给定一大堆线段,问用这些线段覆盖一个连续区间1-x的最小使用线段的数量。
题解
考虑一个这样的贪心:
先按照左端点排序,若左端点一样则谁长谁在前。现在判无解就方便了,记录一下前缀max即可。然后现在要最小化选择。
记录一个最右端点\(R\),一个暴力的办法是暴力循环判断所有线段是否满足条件,这样显然超时,你决定优化一下常数,所以你记录一下从哪个线段开始才\(l_i \ge R\)。你以为你是常数优化,其实你复杂度就对了(\(O(n)\))...
const int QAQ=2020;
while(QAQ){
register int l=r+1;
for(register int t=can;t<=n;++t){
if(data[t].l<=l&&data[t].r>=l)
r=max(r,data[t].r);
if(data[t].l>r) {can=t;break;}
}
if(l>r)break;
else if(++ans,r>=T) break;
}
分析一下这个复杂度,首先是can一定会一直变大,并且变大\(O(n)\)的代价是\(O(n)\)的,所以就是\(O(n)\)的。你可能会说跳出那个for循环不一定是由于break,但是如果不是那个break跳掉的话说明不存在更多合法方案了。
//@winlere
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std; typedef long long ll;
inline int qr(){
register int ret=0,f=0;
register char c=getchar();
while(c<48||c>57)f|=c==45,c=getchar();
while(c>=48&&c<=57) ret=ret*10+c-48,c=getchar();
return f?-ret:ret;
}
int n,T,ans,top,r;
struct LINE{
int l,r;
LINE(){l=r=0;}
LINE(const int&a,const int&b){l=a;r=b;}
}data[25005];
inline bool cmp(const LINE&a,const LINE&b){
if(a.l!=b.l) return a.l<b.l;
return a.r>b.r;
}
queue < int > q;
int main(){
#ifndef ONLINE_JUDGE
freopen("in.in","r",stdin);
//freopen("out.out","w",stdout);
#endif
int n=qr();T=qr();
for(register int t=1,t1,t2;t<=n;++t){
t1=qr();t2=qr();
data[t]=LINE(t1,t2);
}
sort(data+1,data+n+1,cmp);
while(r<T){
register int ew=r+1;
for(register int t=1;t<=n;++t)
if(data[t].l<=ew&&data[t].r>=ew)
r=max(r,data[t].r);
else if(data[t].l>r){
top=t;
break;
}
if(ew>r)break;
else ++ans;
}
if(r<T) ans=-1;
cout<<ans<<endl;
return 0;
}
【题解】Cut the Sequence(贪心区间覆盖)的更多相关文章
- 高效算法——E - 贪心-- 区间覆盖
E - 贪心-- 区间覆盖 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/E 解题思路: 贪心思想, ...
- B. Heaters 思维题 贪心 区间覆盖
B. Heaters 这个题目虽然只有1500的分数,但是我还是感觉挺思维的,我今天没有写出来,然后看了一下题解 很少做这种区间覆盖的题目,也不是很擅长,接下来讲讲我看完题解后的思路. 题目大意是:给 ...
- UVA 10382 - Watering Grass【贪心+区间覆盖问题+高精度】
UVa 10382 - Watering Grass n sprinklers are installed in a horizontal strip of grass l meters long a ...
- 南阳OJ-12-喷水装置(二)贪心+区间覆盖
题目链接: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=12 题目大意: 有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有 ...
- nyoj 12——喷水装置二——————【贪心-区间覆盖】
喷水装置(二) 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n(n<=10000)个点状的 ...
- 51nod 1091 线段的重叠【贪心/区间覆盖类】
1091 线段的重叠 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注 X轴上有N条线段,每条线段包括1个起点和终点.线段的重叠是这样来算的,[10 2 ...
- UVA 10382 Watering Grass 贪心+区间覆盖问题
n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each spri ...
- UVA 10020 Minimal coverage(贪心 + 区间覆盖问题)
Minimal coverage The Problem Given several segments of line (int the X axis) with coordinates [Li, ...
- hdoj 4883 TIANKENG’s restaurant【贪心区间覆盖】
TIANKENG’s restaurant Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/O ...
随机推荐
- Move resources allocated using unmanaged interface to managed devm interface
转载:http://blog.csdn.net/swingboard/article/details/27207497 So today let’s talk about devm functio ...
- tomcat使用安全及CVE-2017-12615
tomcat安全情报的收集 1.首先定期查看官网各个版本存在的安全漏洞公告: http://tomcat.apache.org/security.html 2.去各大漏洞网站查看漏洞披露信息 看几个漏 ...
- sublime text常用快捷键(转)
选择一个选中项的下一个匹配项: ctrl+d 把光标放在一个单词上,按下ctrl+ D,将选择这个单词.一直按住ctrl且按D多次,将选择当前选中项的下一个匹配项.通过按住ctrl,再按D三次,将选择 ...
- 在Android上编译OSG[3.0.2 ] (转)
在Android上编译OSG[3.0.2 ] 分类:Android This file contents can be applied for version OpenSceneGraph(OSG ...
- HTML5 Canvas 绘制星条旗
代码: <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type ...
- Micro:Bit手柄试用之一MagicPad (解决蓝牙与gamePad包共存)
前言 原创文章,转载引用务必注明链接.由于本人初次接触Micro:Bit,水平有限,如有疏漏,欢迎指正. Micro:Bit真好玩! DFRobot的论坛相关资料算是国内比较丰富的了,个人感觉MB比A ...
- vue 中 this.$router.push() 路由跳转传参 及 参数接收的方法
传递参数的方法:1.Params 由于动态路由也是传递params的,所以在 this.$router.push() 方法中 path不能和params一起使用,否则params将无效.需要用name ...
- Shell脚本之:变量
与编译型语言不同,shell脚本是一种解释型语言. 执行这类程序时,解释器(interpreter)需要读取我们编写的源代码(source code),并将其转换成目标代码(object code), ...
- 【Python】Python中in与not in
在python中,要判断特定的值是否存在列表中,可使用关键字in,判断特定的值不存在列表中,可使用关键字not in letters = ['A','B','C','D','E','F','G'] i ...
- Java 实现对Sql语句解析
原文地址:http://www.cnblogs.com/zcftech/archive/2013/06/10/3131286.html 最近要实现一个简易的数据库系统,除了要考虑如何高效的存储和访问数 ...