zoj 3197 Google Book//最小区间覆盖

最悲哀的,莫过于死活不过...

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
#define N 5005
class node
{
public:
	int left,right;
};
node st[N];
int n;
int cmp(const void *a,const void *b)
{
	node *c =(node *)a;
	node *d =(node *)b;
	if(c->left >d->left ) return 1;
	if(c->left ==d->left &&c->right >d->right ) return 1;
	return 0;
}
//void work()
//{
//	int i,j,k;
//    int ans =0;
//	int ts=1,ed=1;
//	for(i=0;ed<=n;)
//	{
//		bool flag=false;
//		while(st[i].left<=ts&&st[i].right>=ed)
//		{
//			flag=true; 
//			ed =st[i].right;i++;
//			if(i>=n) break;
//		}
//		if(flag&&ed==n) { printf("%d\n",++ans);return ;}
//		if(flag) {ans++; ed+=1;ts=ed;}
//		while(st[i].right <ed&&i<n) i++;
//	}
//	printf("%d\n",ans);
//}
int work()
{
	int minp=1,maxp=n;
	int cur=minp;
	int i=0,sum=0;
	while(cur<=maxp&&i<n)
	{
		int temp =cur;
		while(st[i].left <=cur)
		{
			if(temp<st[i].right ) temp =st[i].right ;
			i++;
		}
		cur=temp+1;sum++;
	}
	return sum;
} 
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(int i=0;i<n;i++)
		{
			scanf("%d%d",&st[i].left ,&st[i].right );
			if(st[i].right >n) st[i].right =n;
		}
		qsort(st,n,sizeof(st[0]),cmp);
		printf("%d\n",work());
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值