码迷,mamicode.com
首页 > 其他好文 > 详细

UVA-10020-贪心

时间:2018-12-09 12:08:25      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:gre   tor   mes   ++   解题思路   highlight   constexpr   col   sharp   

  题意:给你一些数轴上的线段,要求寻找出某些线段能够完全覆盖[0,M],并且取的线段数目最小.

解题思路:

 贪心思路,

1.每个线段都有一个L和R,代表它的起点和终点,对于所有R <= 0 ,  L>=R的线段全不要,不符合题意.

2.对于每个线段,根据L进行排序,如果L相同,长度长的排前面.

那么选取的时候只要从原点0开始,每次选取最长的线段即可.

附上一组用例.

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

1
-1 0
0 1
0 0 
 

17
0 4
0 1
1 5
4 10
10 13
13 17
5 17
0 0

 

#include <string>
#include<iostream>
#include<map>
#include<memory.h>
#include<vector>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<math.h>
#include<iomanip>


namespace cc
{
	using std::cout;
	using std::endl;
	using std::cin;
	using std::map;
	using std::vector;
	using std::string;
	using std::sort;
	using std::priority_queue;
	using std::greater;
	using std::vector;
	using std::swap;
	using std::stack;

	class Point
	{

	public: int l, r, len;
			Point() {};
			Point(int x, int y) :l(x), r(y) {
				if (l < 0)
					this->len = r;
				else
					this->len = r - l; 
			
			};

			int operator < (Point& p)
			{
				int l1 = p.l;
				int l2 = this->l;
				if (l1 < 0)
					l1 = 0;
				if (l2 < 0)
					l2 = 0;
				if (l1 < l2)
					return 0;
				if (l1 == l2)
					return p.len < this->len;
				return 1;
			}

	};

	constexpr int N = 100000;

	int M;
	int ok;
	int ans = 0;
	Point result[N + 1];
	Point input[N + 1];
	int n;

	void init()
	{
		ok = M = ans =n = 0;
	}

	void dump()
	{
		for (int i = 0;i < n;i++)
		{
			cout << input[i].l << " " << input[i].r << endl;
		}
	}

	void search(int curR, int curIndex)
	{
		int curMax = 0;
		int okIndex = -1;
		int endIndex = -1;
		for (int i = curIndex;i < n;i++)
		{
			if ((input[i].l <= curR && input[i].r > curR))
			{
				if (input[i].r > curMax)
				{
					curMax = input[i].r;
					okIndex = i;
					continue;
				}
			}
			if (input[i].l > curR)
			{
				endIndex = i;
				break;
			}
		}
		if (okIndex != -1)
		{
			result[ans++] = input[okIndex];
			if (input[okIndex].r >= M)
			{
				ok = 1;
				return;
			}
			if (endIndex == -1)
				return;
			search(curMax,endIndex);
		}
	}

	void solve()
	{
		int cases;
		cin >> cases;
		int t = 0;
		while (cases--)
		{
			if (t != 0)
				cout << endl;
			t++;
			int l, r;
			init();
			cin >> M;
			while (cin >> l && cin >> r && (l || r))
			{
				if (r <= 0||l >= r)
					continue;
				Point p(l, r);
				input[n++] = p;
			}
			sort(input, input + n);
			//dump();
			search(0,0);
			if (ok)
			{
				cout << ans << endl;
				for (int i = 0;i < ans;i++)
					cout << result[i].l << " " << result[i].r << endl;

			}
			else
			{
				cout << 0 << endl;
			}

		}

	}

};


int main()
{

#ifndef ONLINE_JUDGE
	freopen("d://1.text", "r", stdin);
#endif // !ONLINE_JUDGE
	cc::solve();

	return 0;
}

  

UVA-10020-贪心

标签:gre   tor   mes   ++   解题思路   highlight   constexpr   col   sharp   

原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/10090416.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!