UVa-10020 - Minimal coverage

原创 2013年12月05日 21:50:12

The Problem


Given several segments of line (int the X axis) with coordinates [Li,Ri]. You are to choose the minimal amount of them, such they would completely cover the segment [0,M].

The Input


The first line is the number of test cases, followed by a blank line.


Each test case in the input should contains an integer M(1<=M<=5000), followed by pairs "Li Ri"(|Li|, |Ri|<=50000, i<=100000), each on a separate line. Each test case of input is terminated by pair "0 0".


Each test case will be separated by a single line.


The Output


For each test case, in the first line of output your programm should print the minimal number of line segments which can cover segment [0,M]. In the following lines, the coordinates of segments, sorted by their left end (Li), should be printed in the same format as in the input. Pair "0 0" should not be printed. If [0,M] can not be covered by given line segments, your programm should print "0"(without quotes).


Print a blank line between the outputs for two consecutive test cases.


Sample Input


2


1
-1 0
-5 -3
2 5
0 0


1
-1 0
0 1
0 0
Sample Output


0


1
0 1
Alex Gevak

September 10, 2000 (Revised 2-10-00, Antonio Sanchez)


主要任务是找出一个位置到其余位置的距离和最短。


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
	int m;
	cin >> m;
	while(m--)
	{
		int r;
		vector<int> number;
		cin >> r;
		for(int i = 0; i < r; ++i)
		{
			int temp;
			cin >> temp;
			number.push_back(temp);	
		}
		sort(number.begin(), number.end());// 排序
		int mid_tag = r / 2;
		int medians = number[mid_tag], distance = 0, i;
		for(i = 0; i <= mid_tag; ++i)
		{
			distance += number[mid_tag] - number[i];
		}
		for(;i < r; ++i)
		{
			distance += number[i] - number[mid_tag];
		}
		cout << distance << endl;
	}
	return 0;
}

相关文章推荐

UVA 10020 Minimal coverage(最小覆盖问题)

Minimal coverage  The Problem Given several segments of line (int the X axis) with coordinates...

UVa 10020 - Minimal coverage(区间覆盖、贪心)

算法入门经典关于区间覆盖的讲解: 8.4.6:区间覆盖问题

uva 10020 - Minimal coverage

点击打开链接uva 10200 题目意思:     给定一个值m作为区间的右端点,区间的左端点为0,现在给定一些小区间,要求找到最少的区间数完全覆盖区间[0,m] 解题思路:     1:第一种区...

UVa 10020 - Minimal coverage

题意是:输入几个样例,每个样例第一行输入从0开始需要覆盖的长度M,即[0,M]。之后输入覆盖的线段,求需要的线段条数最小值。   思路:贪心算法,具体见代码及注释。 #include #inclu...

UVA_10020_Minimal coverage

#include #include #include #include #include #include #include #include #include #include #pragma wa...
  • cxy7tv
  • cxy7tv
  • 2016年04月24日 21:54
  • 82

UVa 10020 Minimal coverage (贪心&区间覆盖)

10020 - Minimal coverage Time limit: 3.000 seconds  http://uva.onlinejudge.org/index.php?option=com_...

uva 10020 Minimal coverage 【贪心】+【区间完全覆盖】

Minimal coverage  The Problem Given several segments of line (int the X axis) with coordinates [Li,R...

uva 10020- Minimal coverage (贪心思想 简单区间覆盖)

题目大意:给出一个范围M,然后给出若干的区间,以0 0 终止, 要求用最少的区间将0 ~M 覆盖,输出最少个数以及方案。 解题思路:典型的区间覆盖问题,算法竞赛入门经典P154上有讲。 /...

[算法入门经典] 区间最少覆盖问题 UVA 10020 - Minimal coverage

按算法入门经典的思路去模拟

UVA - 10020 Minimal coverage(区间覆盖问题)

Minimal coverage  The Problem Given several segments of line (int the X axis) with coordinat...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:UVa-10020 - Minimal coverage
举报原因:
原因补充:

(最多只允许输入30个字)