【题解】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. 【转】浅谈多核CPU、多线程、多进程

    浅谈多核CPU.多线程.多进程 1.CPU发展趋势 核心数目依旧会越来越多,依据摩尔定律,由于单个核心性能提升有着严重的瓶颈问题,普通的桌面PC有望在2017年末2018年初达到24核心(或者16核3 ...

  2. 修改Tomcat服务中的端口配置

    1.修改Tomcat服务中的端口配置: 分别修改安装目录下的conf子目录中的server.xml文件(注意:两个文件中对应的端口号要不一样),修改如下 : a. 修改Shutdown端口(默认为80 ...

  3. Mac. 文件夹赋予权限

    1. click on your background to go to finder click on go and go to folder /usr right click on local a ...

  4. 11.【nuxt起步】-登录验证

    1.新建/pages/login.vue 2.安装cookie Cnpm install js-cookie --s 3.Login.vue增加 import Cookie from 'js-cook ...

  5. NormalMap 贴图 [转]

    转载: http://www.zwqxin.com/archives/shaderglsl/review-normal-map-bump-map.html   说起Normal Map(法线贴图),就 ...

  6. MapWindowPoints

    中文名 MapWindowPoints Windows CE 1.0及以上版本 头文件 winuser.h 库文件 user32.lib MapWindowPoints函数把相对于一个窗口的坐标空间的 ...

  7. Ubuntu下的计划任务 -- cron的基本知识

    下面不完全: 参考:http://blog.csdn.net/cuker919/article/details/6336457 cron是一个Linux下的后台进程,用来定期的执行一些任务.因为我用的 ...

  8. Linux下快速安装Mysql及使用

    1.安装 查看有没有安装过: yum list installed mysql* rpm -qa | grep mysql* 查看有没有安装包: yum list mysql* 安装mysql客户端: ...

  9. eclipse安装Memory Analyzer

    转载:http://blog.csdn.net/lindir/article/details/8743610 2个月没有写博客了,最近一直在考虑自己未来的方向,再加上项目较紧,一直未更新.今天想着要好 ...

  10. Amazon DynamoDB, 面向互联网应用的高性能、可扩展的NoSQL数据库

    DynamoDB是一款全面托管的NoSQL数据库服务.客户能够很easy地使用DynamoDB的服务.同一时候享受到高性能,海量扩展性和数据的持久性保护. DynamoDB数据库是Amazon在201 ...