pku acm 1065

//貪心算法

#include <iostream>
#include <cstdio>
#include <list>
#include <algorithm>
using namespace std;

typedef pair<int,int> PINTINT;

bool cmp1(PINTINT p1, PINTINT p2)
{
	if (p1.first < p2.first)return true;
	if (p1.first > p2.first)return false;
	
	//p1.first == p2.first
	if (p1.second < p2.second)return true;
	if (p1.second > p2.second)return false;
	
	//p1 == p2
	return false;
}

int main()
{
	freopen("in.txt","r",stdin);
	
	PINTINT tpair;
	list<PINTINT> listpairs;
	list<PINTINT>::iterator it;
	int time;

	int t,n;
	cin>>t;
	while(t--)
	{
		cin>>n;

		listpairs.clear();
		while (n--)
		{
			cin>>tpair.first>>tpair.second;
			listpairs.push_back(tpair);
		}
	
		listpairs.sort(cmp1);
		/*for (it = listpairs.begin();it != listpairs.end(); ++it)
			cout<<it->first<<" "<<it->second<<endl;*/

		time = 0;
		int prev;
		while(listpairs.size() > 0)
		{
			it = listpairs.begin();
			prev = it->second;	
			for (++it;it != listpairs.end();)
			{			 
				if (it->second >= prev)
				{
					prev = it->second;
					it = listpairs.erase(it);
				}
				else
					 ++it;
				
			}
			listpairs.erase(listpairs.begin());
			time++;
		}
		
		cout<<time<<endl;		
	}	
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章