【题解】Cut the Sequence(贪心区间覆盖)

POJ - 3017

题意:

给定一大堆线段,问用这些线段覆盖一个连续区间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(贪心区间覆盖)的更多相关文章

  1. 高效算法——E - 贪心-- 区间覆盖

    E - 贪心-- 区间覆盖 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/E 解题思路: 贪心思想, ...

  2. B. Heaters 思维题 贪心 区间覆盖

    B. Heaters 这个题目虽然只有1500的分数,但是我还是感觉挺思维的,我今天没有写出来,然后看了一下题解 很少做这种区间覆盖的题目,也不是很擅长,接下来讲讲我看完题解后的思路. 题目大意是:给 ...

  3. UVA 10382 - Watering Grass【贪心+区间覆盖问题+高精度】

    UVa 10382 - Watering Grass n sprinklers are installed in a horizontal strip of grass l meters long a ...

  4. 南阳OJ-12-喷水装置(二)贪心+区间覆盖

    题目链接: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=12 题目大意: 有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有 ...

  5. nyoj 12——喷水装置二——————【贪心-区间覆盖】

    喷水装置(二) 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n(n<=10000)个点状的 ...

  6. 51nod 1091 线段的重叠【贪心/区间覆盖类】

    1091 线段的重叠 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题  收藏  关注 X轴上有N条线段,每条线段包括1个起点和终点.线段的重叠是这样来算的,[10 2 ...

  7. UVA 10382 Watering Grass 贪心+区间覆盖问题

    n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each spri ...

  8. UVA 10020 Minimal coverage(贪心 + 区间覆盖问题)

     Minimal coverage  The Problem Given several segments of line (int the X axis) with coordinates [Li, ...

  9. hdoj 4883 TIANKENG’s restaurant【贪心区间覆盖】

    TIANKENG’s restaurant Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/O ...

随机推荐

  1. Java---杨辉三角简易解法(通俗易懂,逻辑严密)

  2. PHP message: PHP Fatal error: require(): Failed opening required

    PHP message: PHP Warning: require(/data/wwwroot/blog.sgfoot.com/bootstrap/autoload.php): failed to o ...

  3. meta 标签大全

    相信在html5之前,很少人会关注html页面上head里标签元素的定义和应用场景,可能记得住的只有"title"."keyword"和"descri ...

  4. DIV相对于父DIV底部对齐的实现方法

    代码如下 <style type="text/css"> .box1 {border:1px #cccccc solid; width:500px; height:60 ...

  5. linux登录用户(终端)间的通信

    转载 1.write命令 使用权限 : 所有使用者  使用方式 :  write user [ttyname]  说明 : 通过write指令可传递信息给另一位登入系统的用户,当输入完毕后,键入EOF ...

  6. FTP经典常用命令

    FTP命令是Internet用户使用最频繁的命令之一,不论是在DOS还是UNIX操作系统下使用FTP,都会遇到大量的FTP内部命令. 熟悉并灵活应用FTP的内部命令,可以大大方便使用者,并收到事半功倍 ...

  7. Android性能专项测试之耗电量统计API

    版权声明:本文为Doctorq原创文章,未经博主允许不得转载. https://blog.csdn.net/qhshiniba/article/details/49155979 参考文章:Androi ...

  8. Notification(二)——PendingIntent的flag导致数据同样的问题

    MainActivity例如以下: package cc.cu; import android.os.Bundle; import android.view.View; import android. ...

  9. 如何删除mysql 主键索引

    如果一个主键是自增长的,不能直接删除该列的主键索引, 应当先取消自增长,再删除主键特性   alter table 表名 drop primary key; [如果这个主键是自增的,先取消自增长.] ...

  10. Shell脚本之:for

    与其他编程语言类似,Shell支持for循环. for循环一般格式为: for 变量 in 列表 do command1 command2 ... commandN done 列表是一组值(数字.字符 ...