URAL 1303. Minimal Coverage(最小覆盖 数学啊 )
来源:互联网 发布:淘宝网秋冬男式上衣 编辑:程序博客网 时间:2024/06/05 11:43
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1303
input output
Given set of line segments [Li, Ri] with integer coordinates of their end points. Your task is to find the minimal subset of the given set which covers segment [0, M] completely (M is a positive integer).
Input
First line of the input contains an integer M (1 ≤ M ≤ 5000). Subsequent lines of input contain pairs of integers Li and Ri (−50000 ≤ Li < Ri ≤ 50000). Each pair of coordinates is placed on separate line. Numbers in the pair are separated with space. Last line of input data contains a pair of zeroes. The set contains at least one and at most 99999 segments.
Output
Your program should print in the first line of output the power of minimal subset of segments which covers segment [0, M]. The list of segments of covering subset must follow. Format of the list must be the same as described in input with exception that ending pair of zeroes should not be printed. Segments should be printed in increasing order of their left end point coordinate.
If there is no covering subset then print “No solution” to output.
Samples
1-1 0-5 -32 50 0
No solution
1-1 00 10 0
10 1
题意:
最小区间覆盖,求使用最少的木板,能覆盖区间0----M!
PS:
按照贪心的思想;
每次找到左端点在未被覆盖区间的左端点的左边,且右端点最远的木板,
然后把要求的覆盖区间改为这个右端点到M这个区间。
把此时木板的右端点作为未被覆盖区间的左端点!
依次类推下去。
代码如下:
#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>using namespace std;struct node{ int l, r;} a[100017];int vis[100017];bool cmp(node a, node b){ return a.l < b.l;}int main(){ int m; while(~scanf("%d",&m)) { memset(vis, 0,sizeof(vis)); int x, y; int k = 0; while(1) { scanf("%d%d",&x,&y); if(x==0 && y==0) { break; } a[k].l = x, a[k].r = y; k++; } sort(a,a+k,cmp); int cont = 0; int num = 0, p = 0; int flag = 0; int ans = 0; for(int i = 0; ; i++) { if(cont >= m) { break; } int t_end = 0, tt = 0; while(num < k && a[num].l <= cont) { if(a[num].r > t_end) { t_end = a[num].r; tt = num; } num++; } if(t_end == 0) { flag = 1; break; } cont = t_end; vis[tt] = 1; ans++; } if(flag) { printf("No solution\n"); continue; } printf("%d\n",ans); for(int i = 0; i < k; i++) { if(vis[i]) { printf("%d %d\n",a[i].l,a[i].r); } } printf("\n"); } return 0;}
1 0
- URAL 1303. Minimal Coverage(最小覆盖 数学啊 )
- Ural 1303. Minimal Coverage / 最小区间覆盖
- ural Minimal Coverage (区间覆盖)
- Ural 1303. Minimal Coverage
- URAL 1303. Minimal Coverage 贪心
- uva 10020 Minimal coverage(贪心-最小覆盖问题)
- uva 10020 Minimal coverage(贪心-最小覆盖问题)
- UVA 10020 Minimal coverage(最小覆盖问题)
- Ural 1303 Minimal Coverage(贪心)
- Ural 1303 Minimal Coverage(贪心)
- Ural 1303 Minimal Coverage
- UVa 10020 - Minimal coverage(区间覆盖)
- uva10020 - Minimal coverage(区间覆盖)
- uva10020 - Minimal coverage(区间覆盖)
- UVA 10020——Minimal coverage(最小覆盖,贪心问题)
- ural 1303 Minimal Coverage【贪心】
- URAL 1303 Minimal Coverage (贪心)
- Minimal coverage-区间覆盖问题
- Java 多线程并发解决方案
- 第二学期 3月 随笔
- 数据结构-单向链表相关操作算法
- android 获取apk md5值
- UVA - 10591 - Happy Number (STL)
- URAL 1303. Minimal Coverage(最小覆盖 数学啊 )
- [LeetCode]Maximum Subarray
- CHECK_NRPE: Error - Could not complete SSL handshake.
- 分布式系统的一些原则的本质
- Struts2原理及源码分析
- File类
- 第二周 项目一-旱冰场造价
- hdoj 4500 小Q系列故事——屌丝的逆袭
- 使用Spring Web Flow-程序流程定义