原题 UVA10020  :http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19688

经典的贪心问题,区间上贪心当然是右区间越右越好了,当然做区间要小于等于当前待覆盖的位置,我们可以用一个变量begin代表当前待覆盖的区间的左值,每一次贪心成功就更新begin的值,然后再进行类似的重复动作

我之前写的版本.两次排序。

#include"iostream"
#include"cstdio"
#include"algorithm"
using namespace std; const int maxn=100000+10; struct node
{
int x,y;
}a[maxn],b[maxn]; bool cmp(node a1,node a2)
{
return a1.x<a2.x;
} bool cm(node a1,node a2)
{
return a1.y>a2.y;
} int main()
{
int T,t;
cin>>T;
t=T;
while(T--)
{
if(T!=t-1) cout<<endl;
int M,f,l,r,x,a1,b1;
cin>>M;
f=0;
while(cin>>a[f].x>>a[f].y&&(a[f].x||a[f].y))
{
if((a[f].x<0&&a[f].y<0)||(a[f].x>M&&a[f].y>M))
continue;
f++;
}
// cout<<f<<endl;
sort(a,a+f,cmp); if(a[0].x>0||f==0) cout<<0<<endl; else
{ sort(a,a+f,cm);
int ff=0,flag=0,sum=0,begi;
begi=0;
while(begi<M)
{
for(int i=0;i<f;i++)
{
if(a[i].x>begi) continue;
if(a[i].y==begi) continue;
begi=a[i].y;flag=1;sum++;b[sum].x=a[i].x;b[sum].y=a[i].y;break;
}
//cout<<begi<<endl;getchar();
if(flag)
{
flag=0;
}
else
{ff=1;break;}
}
if(ff) cout<<0<<endl;
else {
cout<<sum<<endl;
for(int i=1;i<=sum;i++)
cout<<b[i].x<<' '<<b[i].y<<endl;
}
}
}
return 0;
} 也可以只排序一次,这样的话需要记录当前已贪心到的数组下标位置,可以摒弃掉那些已经遍历过的那些数组元素,也是一样的时间
#include"iostream"
#include"cstdio"
#include"algorithm"
using namespace std; const int maxn=100000+10; struct node
{
int x,y;
}a[maxn],b[maxn]; bool cmp(node a1,node a2)
{
return a1.x<a2.x;
} bool cm(node a1,node a2)
{
return a1.y>a2.y;
} int main()
{
int T,t;
cin>>T;
t=T;
while(T--)
{
if(T!=t-1) cout<<endl;
int M,f;
cin>>M;
f=0;
while(cin>>a[f].x>>a[f].y&&(a[f].x||a[f].y))
{
if((a[f].x<0&&a[f].y<0)||(a[f].x>M&&a[f].y>M))
continue;
f++;
} sort(a,a+f,cmp); if(a[0].x>0||f==0) cout<<0<<endl; else
{
int ff=0,sum=0,begi,r;
begi=0;r=-1;
int i=0,j;
while(i<f)
{
j=i;
while(a[j].x<=begi &&j<f)
{
if(r<a[j].y)
{
r=a[j].y;
b[sum].x=a[j].x;
b[sum].y=a[j].y;
}
j++;
}
if(j==i) break;
i=j;
sum++;
begi=r;
if(begi>=M) {ff=1;break;}
}
if(ff==0) cout<<0<<endl;
else {
cout<<sum<<endl;
for(int i=0;i<sum;i++)
cout<<b[i].x<<' '<<b[i].y<<endl;
}
}
}
return 0;
}

集训第四周(高效算法设计)D题 (区间覆盖问题)的更多相关文章

  1. 高效算法——D 贪心,区间覆盖问题

    Given several segments of line (int the X axis) with coordinates [Li , Ri ]. You are to choose the m ...

  2. 集训第四周(高效算法设计)A题 Ultra-QuickSort

    原题poj 2299:http://poj.org/problem?id=2299 题意,给你一个数组,去统计它们的逆序数,由于题目中说道数组最长可达五十万,那么O(n^2)的排序算法就不要再想了,归 ...

  3. 集训第四周(高效算法设计)P题 (构造题)

    Description   There are N<tex2html_verbatim_mark> marbles, which are labeled 1, 2,..., N<te ...

  4. 集训第四周(高效算法设计)O题 (构造题)

    A permutation on the integers from 1 to n is, simply put, a particular rearrangement of these intege ...

  5. 集训第四周(高效算法设计)N题 (二分查找优化题)

    原题:poj3061 题意:给你一个数s,再给出一个数组,要求你从中选出m个连续的数,m越小越好,且这m个数之和不小于s 这是一个二分查找优化题,那么区间是什么呢?当然是从1到数组长度了.比如数组长度 ...

  6. 集训第四周(高效算法设计)M题 (扫描法)

    原题:UVA11078 题意:给你一个数组,设a[],求一个m=a[i]-a[j],m越大越好,而且i必须小于j 怎么求?排序?要求i小于j呢.枚举?只能说超时无上限.所以遍历一遍数组,设第一个被减数 ...

  7. 集训第四周(高效算法设计)I题 (贪心)

    Description Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshe ...

  8. 集训第四周(高效算法设计)E题 (区间覆盖问题)

    UVA10382 :http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21419 只能说这道题和D题是一模一样的,不过要进行转化, ...

  9. 集训第四周(高效算法设计)L题 (背包贪心)

    Description   John Doe is a famous DJ and, therefore, has the problem of optimizing the placement of ...

随机推荐

  1. javascript---DOM大编程

    编程练习 制作一个表格,显示班级的学生信息. 要求: 1. 鼠标移到不同行上时背景色改为色值为 #f2f2f2,移开鼠标时则恢复为原背景色 #fff 2. 点击添加按钮,能动态在最后添加一行 3. 点 ...

  2. ACM_素数环(dfs)

    Problem Description: 如图所示,环由n个圆组成. 将自然数1,2,...,n分别放入每个圆中,并且两个相邻圆中的数字总和应为素数. 注意:第一个圆圈的数量应该始终为1. Input ...

  3. Latex排版工具的使用(一) 分类: Latex 2014-06-14 22:52 448人阅读 评论(0) 收藏

    使用Latex可以排版出漂亮的论文,尤其适合对含有数学公式论文的排版. 下面编写第一Latex源文件,实现对两个数学公式的排版: 新建文件first.tex: \documentclass{artic ...

  4. 468 Validate IP Address 验证IP地址

    详见:https://leetcode.com/problems/validate-ip-address/description/ Java实现: class Solution { public St ...

  5. 【学习笔记】SIFT尺度不变特征 (配合UCF-CRCV课程视频)

    SIFT尺度不变特征 D. Lowe. Distinctive image features from scale-invariant key points, IJCV 2004 -Lecture 0 ...

  6. AJPFX关于abstract的总结

    抽象类: abstract抽象:不具体,看不明白.抽象类表象体现.在不断抽取过程中,将共性内容中的方法声明抽取,但是方法不一样,没有抽取,这时抽取到的方法,并不具体,需要被指定关键字abstract所 ...

  7. ssm(Spring、Springmvc、Mybatis)实战之淘淘商城-第二天(非原创)

    文章大纲 一.课程介绍二.整合淘淘商城ssm项目三.Mybatis分页插件PageHelper使用四.整合测试五.项目源码与资料下载六.参考文章   一.课程介绍 一共14天课程(1)第一天:电商行业 ...

  8. (六)SpringIoc之延时加载

    Spring容器初始化时将所有scope = singleton的bean进行实例化. 通常情况下这是一件好事,因为这样在配置中的错误会更容易发现.但是如果不想spring容器初始化就实例化就要用到延 ...

  9. 虚拟机centOs Linux与Windows之间的文件传输

    一.配置环境 虚拟机Linux:Fedora 9 文件传输工具:SSHSecureShellClient-3.2.9 二.实现步骤 1. 在Windows中安装文件传输工具SSHSecureShell ...

  10. centOS linux 下PHP编译安装详解

    一.下载PHP源码包 wget http://php.net/distributions/php-5.6.3.tar.gz   二.添加依赖应用 yum install -y gcc gcc-c++ ...