原题 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. ObjectARX2012错误1 fatal error C1083: 无法打开包括文件:“arxHeaders.h”: No such file or directory; fatal error C1083: 无法打开包括文件:“map”: No such file or directory

    问题1:fatal error C1083: 无法打开包括文件:“arxHeaders.h”: No such file or directory: 解决办法:这个问题很明显,是因为没有在工程属性里包 ...

  2. .NET下集中实现AOP编程的框架

    一.Castle 使用这个框架呢,首先是需要安装NuGet包. 先建立一个控制台项目,然后在NuGet中搜索Castle.Windsor,不出意外的话应该能找到如下的包 然后安装,会自动的安装包Cas ...

  3. Android偏好设置(7)自定义Preference,和PreferenceDialog

    Building a Custom Preference The Android framework includes a variety of Preference subclasses that ...

  4. 10-1 浮动框架iframe

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  5. Oracle报错:“ORA-18008: 无法找到 OUTLN 方案 ”的解决方案

    Oracle报错:“ORA-18008: 无法找到 OUTLN 方案 ”的解决方案   2.修改replication_dependency_tracking参数 SQL> alter syst ...

  6. LN : leetcode 70 Climbing Stairs

    lc 70 Climbing Stairs 70 Climbing Stairs You are climbing a stair case. It takes n steps to reach to ...

  7. Android与H5互调(通过实例来了解Hybrid App)

    前些日子,Android原生开发将被取缔的吵得火热,JavaScript是能做一个完全的APP,但只使用JavaScript做出来的APP也不会牛逼到哪里去.最好的是混合(Hybrid)开发,在需要的 ...

  8. iOS---UICollectionView自定义流布局实现瀑布流效果

    自定义布局,实现瀑布流效果 自定义流水布局,继承UICollectionViewLayout 实现一下方法 // 每次布局之前的准备 - (void)prepareLayout; // 返回所有的尺寸 ...

  9. http升级https改造方案

    一.解决方案 1.httpClient请求https版蓝鲸接口 (1).原理 https与http最大的区别在于SSL加密传输协议的使用.在自己写的JAVA HttpClient程序,想手动验证证书, ...

  10. vsphere中的vcenter创建esxi模板虚拟机新建无法连接网络

    1.删除网卡配置文件下的uuid和hwaddr  这是因为虚拟机模板创建网卡mac没改变 2.删除规则文件 rm -f /etc/udev/rules.d/-prtsistent-net.rules ...