UVA10020->贪心
2016-07-26 01:27
302 查看
题意:给一些区间,求能把[0,M]所有点覆盖需要的最少区间个数,并输出这些区间的左右端点
题解:贪心,关键在于贪心策略能不能想对。
贪心策略:把所有可能有用的区间按照左端点递增的顺序排序,然后依次去找能够缩小区间右边界范围的区间,直到区间的左端点也被覆盖,这里枚举的复杂度为O(N^2),
这样的贪心策略就能保证每次选取的区间都是最优的解
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <vector>
using namespace std ;
#define MAX 100005
int visit[MAX] ;
struct Node
{
int l , r ;
}len[MAX] ;
Node out[MAX] ;
bool comp(const struct Node &x , const struct Node &y)
{
return x. l < y.l ;
}
int main()
{
int T ,M;
scanf("%d" , &T) ;
while(T --)
{
scanf("%d" , &M) ;
int LL = 0 , RR = M ;
int n ;
int pos = 0 ;
int i= 0 ;
while(1)
{
scanf("%d%d", &len[i].l , &len[i].r) ;
if(len[i].r<0 || len[i].l>M) continue ;
if(len[i].l==0&&len[i].r==0)
{
n = i ;
break ;
}
i ++ ;
}
sort(len , len + n ,comp) ;
int ans = 0;
memset(visit , 0 , sizeof(visit)) ;
int judge = 0 ;
for (i = 0; i < n ; ++i)
{
//cout << len[i].l << ' ' << len[i].r << ' ' << len[i].length<<endl;
if(len[i].r >= RR && len[i].l < RR && !visit[i])
{
ans ++ ;
out[pos] = len[i] ;
pos ++ ;
if(len[i].l <= LL)
{
judge = 1;
break ;
}
RR = len[i].l ;
visit[i] = 1;
i = -1 ;
}
}
if(judge)
{
cout << ans <<endl;
for(i = pos - 1 ; i >= 0 ;i --)
{
cout<<out[i].l<<' '<< out[i].r << endl ;
}
}
else cout <<'0'<<endl ;
if(T>0) cout << endl ;
}
return 0;
}
题解:贪心,关键在于贪心策略能不能想对。
贪心策略:把所有可能有用的区间按照左端点递增的顺序排序,然后依次去找能够缩小区间右边界范围的区间,直到区间的左端点也被覆盖,这里枚举的复杂度为O(N^2),
这样的贪心策略就能保证每次选取的区间都是最优的解
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <vector>
using namespace std ;
#define MAX 100005
int visit[MAX] ;
struct Node
{
int l , r ;
}len[MAX] ;
Node out[MAX] ;
bool comp(const struct Node &x , const struct Node &y)
{
return x. l < y.l ;
}
int main()
{
int T ,M;
scanf("%d" , &T) ;
while(T --)
{
scanf("%d" , &M) ;
int LL = 0 , RR = M ;
int n ;
int pos = 0 ;
int i= 0 ;
while(1)
{
scanf("%d%d", &len[i].l , &len[i].r) ;
if(len[i].r<0 || len[i].l>M) continue ;
if(len[i].l==0&&len[i].r==0)
{
n = i ;
break ;
}
i ++ ;
}
sort(len , len + n ,comp) ;
int ans = 0;
memset(visit , 0 , sizeof(visit)) ;
int judge = 0 ;
for (i = 0; i < n ; ++i)
{
//cout << len[i].l << ' ' << len[i].r << ' ' << len[i].length<<endl;
if(len[i].r >= RR && len[i].l < RR && !visit[i])
{
ans ++ ;
out[pos] = len[i] ;
pos ++ ;
if(len[i].l <= LL)
{
judge = 1;
break ;
}
RR = len[i].l ;
visit[i] = 1;
i = -1 ;
}
}
if(judge)
{
cout << ans <<endl;
for(i = pos - 1 ; i >= 0 ;i --)
{
cout<<out[i].l<<' '<< out[i].r << endl ;
}
}
else cout <<'0'<<endl ;
if(T>0) cout << endl ;
}
return 0;
}
相关文章推荐
- 【转】一看就明白的爬虫入门讲解:基础理论篇
- [React Native] Error Handling and ActivityIndicatorIOS
- 04Spring_bean 后处理器(后处理Bean),BeanPostProcessor ,bean创建时序,动态代理
- 航空专用名词缩略语
- new/delete 和new[]/delete[]的解读(转)
- 你不知道的JavaScript--Item38 自定义事件
- 数据库设计中的14个技巧
- OptiScroll 公共例子(只修改了滚动条颜色)
- HTTP Keep-Alive 详解
- 利用Cookie记录用户浏览的图片
- ListView中的Item相对布局设置控件大小
- POJ 1459 Power Network 最大流
- Android—WebView介绍
- 滑动效果是如何产生的(Android群英传)
- scala基础26-高阶函数
- SCU 4529 An Easy Problem(强连通分量+缩点+最小路径覆盖+剪枝)
- HDU 2680 Choose the best route 最短路、Dijkstra、多源化单源最短路
- HDU4430->二分&&枚举
- 一个栈溢出的实验
- DEVO10美国手改日本手