【题解】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. Write a function that generates one of 3 numbers according to given probabilities

    You are given a function rand(a, b) which generates equiprobable random numbers between [a, b] inclu ...

  2. java文件下载导出

    前台代码: $("#btnExport").click(function(){ top.$.jBox.confirm("确认要导出房屋信息吗?","系 ...

  3. Understanding Objective-C Blocks

    The aim of this tutorial is to give a gentle introduction to Objective-C blocks while paying special ...

  4. Fresco框架SimpleDraweeView控件的简单使用

    首先把网络.SD卡的读写权限添加上:<span style="font-family: Arial, Helvetica, sans-serif;">   </s ...

  5. hdu1708(C++)

    这个题目明确说了不涉及大数,假设第i个为b[i]: b[0]=s1; b[1]=s2; b[3]=s1+s2; b[4]=s1+2*s2; b[5]=2*s1+3*s2: b[6]=3*s1+5*s2 ...

  6. Apache OFBIZ高速上手(三)--文件夹&amp;&amp;配置文件介绍

    1.OFBiz简单介绍,什么是OFBiz           OFBiz is an Apache Software Foundation top level project.           A ...

  7. ElasticSearch查询max_result_window问题处理

    需要出一份印地语文章的表,导出规则为: 1.所有印地语(包含各种颜色,各种状态)的文章 2.阅读数大于300 3.按照阅读推荐比进行排序,取前3000篇文章 说明: 1.文章信息,和阅读推荐数量在两个 ...

  8. Solidworks如何生成爆炸图

    1 自动爆炸 点击"爆炸视图"按钮,然后全部选中装配体(被选中的零件会变为蓝色,全部选中即全部变色)然后在组成偶尔的爆炸窗口中下拉,点击应用.   再点击完成   回到装配体面板, ...

  9. 出现“Windows资源管理器已停止工作”错误

    出现"Windows资源管理器已停止工作"错误 什么是资源管理器呢,explorer.exe进程的作用就是让我们管理计算机中的资源! 今天开电脑的时候就一直提示windows资源管 ...

  10. dede内容页调用点击数

     <script src="{dede:field name='phpurl'/}/count.php?view=yes&aid={dede:field name='id'/} ...