原题 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. 在代码里更新autolayout布局

    //遍历view约束(高,宽) NSArray* constrains = self.View.constraints; for (NSLayoutConstraint* constraint in ...

  2. shiro之jdbcRealm

    Shiro认证过程 创建SecurityManager--->主体提交认证--->SecurityManager认证--->Authenticsto认证--->Realm验证 ...

  3. MoveTo和LineTo函数的意思

    这是个画线函数, moveto是移动到某个坐标,lineto是从当前坐标, 移动的某个坐标连接早当前坐标.这两个函数加起来就是画一条直线.

  4. python中字典的陷阱

    把字典与列表组合,如 i=20 s=[]#定义一个空列表 b={'d':i}#定义一个字典 while i>0: i=i-1 b['d']=i#更新字典的值 s.append(b) print( ...

  5. android开发学习 ------- 【转】Gradle相关

    一直在用AndroidStudio,但是对于其Gradle了解的很少. 推荐 http://www.jianshu.com/p/9df3c3b6067a  觉得说的很棒!

  6. mysql自动获取时间日期

    实现方式: 1.将字段类型设为  TIMESTAMP  2.将默认值设为  CURRENT_TIMESTAMP 举例应用: 1.MySQL 脚本实现用例 --添加CreateTime 设置默认时间 C ...

  7. WPF学习08:MVVM 预备知识之COMMAND

    WPF内建的COMMAND是GOF 提出的23种设计模式中,命令模式的实现. 本文是WPF学习07:MVVM 预备知识之数据绑定的后续,将说明实现COMMAND的三个重点:ICommand  Comm ...

  8. TNS-00511: 无监听程序

    这里到服务里面打开 tns 的监听服务

  9. 图解 TCP/IP 第六章 TCP与UDP 笔记6.1 传输层的作用

     图解 TCP/IP  第六章 TCP与UDP   笔记6.1 传输层的作用   传输层必须指出这个具体的程序,为了实现这一功能,使用端口号这样一种识别码.根据端口号,就可以识别在传输层上一层的应用程 ...

  10. SQLServer性能优化专题

    SQLServer性能优化专题 01.SQLServer性能优化之----强大的文件组----分盘存储(水平分库) http://www.cnblogs.com/dunitian/p/5276431. ...