ural 1303 Minimal Coverage(贪心)
链接:
http://acm.timus.ru/problem.aspx?space=1&num=1303
按照贪心的思想,每次找到覆盖要求区间左端点时,右端点最大的线段,然后把要求覆盖的区间改为这个右端点到M这个区间。依次类推下去,这样的话就只需要扫一遍就可以找去来。
要做的预备工作就是将线段按照左端点的升序排序就可以了。
它的时间复杂度就是O(n)
代码一直WA,望大神指教
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<map>
#include<vector>
#include<set>
#include<stack>
#include<queue>
#include<algorithm>
#include<cmath>
#include<stdlib.h>
using namespace std;
#define MAX(a,b) (a > b ? a : b)
#define MIN(a,b) (a < b ? a : b)
#define MAXN 200005
#define INF 1000000007
#define mem(a) memset(a,0,sizeof(a))
#define judge(i) (ma[i].l<=L && ma[i].r>=L && ma[i].l != ma[i].r)//判断是否覆盖了点L struct node{int l,r;}ma[MAXN];
int ans[MAXN];
int M,N,T;
int L; int cmp(node a,node b)
{
if(a.l != b.l)return a.l<b.l;
else return a.r<b.r;
} int find_index(int s,int &i)//找到覆盖了点L而且右端点最大的点,并返回;
//同时将后面要覆盖的点设置为它的右端点
{
i = s;
int e = s,endd = ma[i].r;
while(i < N && judge(i) )
{
if(ma[i].r > endd)
{
endd = ma[i].r;
e = i;
}
i++;
}
L = endd;//endd就是满足覆盖条件下的最大的右端点
//并将其设置为下次比较的左端点
return e;//返回此次的线段的下标
} int main()
{
while(~scanf("%d",&M))
{
N=;
int i;
mem(ans);
while(scanf("%d%d",&ma[N].l, &ma[N].r) && (ma[N].l || ma[N].r))
{
if(ma[N].r <= || ma[N].l>=M)ma[N].l = ma[N].r = INF;//吧在要求区间两端之外的线段去掉
//吧它的左右端点值赋值为INF,这样的话排序时自然就会到最后方,也就相当于不用考虑
N++;
}
sort(ma,ma+N,cmp); L = ;//最初要被覆盖的点是0
int num = ;
for(i=;i<N;i++)
{
int t = i+;//将t设置为i+1,如果下面的judge成功,就会进入函数的while中,那么i会++,如果不成功,t=i+1
if(judge(i)) ans[num++] = find_index(i,t);
i = t-;//由于t相当于多+了1,所以-1
if(L >= M)break;//一旦找到,就退出循环
}
if(L < M)printf("No solution\n");
else
{
printf("%d\n",num);
for(i=;i<num;i++)
{
printf("%d %d\n",ma[ans[i]].l,ma[ans[i]].r);
}
}
}
return ;
}
ural 1303 Minimal Coverage(贪心)的更多相关文章
- 贪心 URAL 1303 Minimal Coverage
题目传送门 /* 题意:最少需要多少条线段能覆盖[0, m]的长度 贪心:首先忽略被其他线段完全覆盖的线段,因为选取更长的更优 接着就是从p=0开始,以p点为标志,选取 (node[i].l < ...
- ural 1303 Minimal Coverage【贪心】
链接: http://acm.timus.ru/problem.aspx?space=1&num=1303 http://acm.hust.edu.cn/vjudge/contest/view ...
- Ural 1303 Minimal Coverage(贪心)
题目地址:Ural 1303 先按每一个线段的左端点排序,然后设置一个起点s.每次都从起点小于等于s的线段中找到一个右端点最大的. 并将该右端点作为新的起点s,然后继续找. 从左到右扫描一遍就可以. ...
- URAL 1303 Minimal Coverage
URAL 1303 思路: dp+贪心,然后记录路径 mx[i]表示从i开始最大可以到的位置 sufmx[i]表从1-i的某个位置开始最大可以到达的位置 比普通的贪心效率要高很多 代码: #inclu ...
- URAL 1303. Minimal Coverage(DP)
题目链接 又是输出路径...这题完全受上题影响,感觉两个题差不多..用了基本上一样的算法写了,这题比较纠结,就是卡内存啊...5000*5000的数组开不了..然后没办法,水了好几次MLE,看了一下虎 ...
- UVA 10020 Minimal coverage(贪心 + 区间覆盖问题)
Minimal coverage The Problem Given several segments of line (int the X axis) with coordinates [Li, ...
- uva.10020 Minimal coverage(贪心)
10020 Given several segments of line (int the X axis) with coordinates [Li, Ri]. You are to choose t ...
- Minimal coverage (贪心,最小覆盖)
题目大意:先确定一个M, 然后输入多组线段的左端和右端的端点坐标,然后让你求出来在所给的线段中能够 把[0, M] 区域完全覆盖完的最少需要的线段数,并输出这些线段的左右端点坐标. 思路分析: 线段区 ...
- uva 10020 Minimal coverage 【贪心】+【区间全然覆盖】
Minimal coverage The Problem Given several segments of line (int the X axis) with coordinates [Li,Ri ...
随机推荐
- Only one instance of a ScriptManager can be added to the page.
一般出现在一个页面用了多个用户控件,而每个用户控件中都用到了ScriptManager,最好的办法是控件中不要加上 <asp:ScriptManager ID="Scr ...
- Js获取日期时间及其它操作
var myDate = new Date();myDate.getYear(); //获取当前年份(2位)myDate.getFullYear(); //获取完整的年份(4位,1 ...
- I.MX6 busybox set hosname and login with root
/************************************************************************** * I.MX6 busybox set hosn ...
- Java [Leetcode 231]Power of Two
题目描述: Given an integer, write a function to determine if it is a power of two. 解题思路: 判断方法主要依据2的N次幂的特 ...
- noip2007提高组题解
题外话:这一年的noip应该是最受大众关心的,以至于在百度上输入noip第三个关键字就是noip2007.主要是由于这篇文章:http://www.zhihu.com/question/2110727 ...
- MVC&WebForm对照学习:文件下载
说完了WebForm和MVC中的文件上传,就不得不说用户从服务器端下载资源了.那么今天就扯扯在WebForm和MVC中是如何实现文件下载的.说起WebForm中的文件上传,codeshark在他的博文 ...
- 【转】让 cocos2d-x 的 CCHttpRequest 支持https
肖锐(Cooki)个人原创,欢迎转载,转载请注明地址,肖锐(Cooki)的技术博客 http://blog.csdn.net/xiao0026 由于游戏用到了网络头像, 今天发现换成facebook ...
- POJ 1208 The Blocks Problem
The Blocks Problem Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5397 Accepted: 231 ...
- HNU 13108-Just Another Knapsack Problem (ac自动机上的dp)
题意: 给你一个母串,多个模式串及其价值,求用模式串拼接成母串(不重叠不遗漏),能获得的最大价值. 分析: ac自动机中,在字典树上查找时,用dp,dp[i]拼成母串以i为结尾的子串,获得的最大价值, ...
- [Irving] Wpf DevexPress GridControl 获取选中行
WPF前台绑定事件代码: <RelayAction TargetControl="{Binding ElementName=GCInstoragePart}" MethodN ...