Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

赋错一个值,结果一夜没睡好,早上早早起来发现了。O(n)复杂度

Posted by zpdlut at 2010-07-13 08:31:20 on Problem 2376
#include<iostream>
using namespace std;
struct Cow
{
	int l;
	int r;
};
int cmp(const void * Aa,const void * Bb)
{
	Cow A = *(Cow *)Aa;
	Cow B = *(Cow *)Bb;
	if(A.l == B.l)
		return A.r - B.r;
	return A.l - B.l;

}
int main()
{	
	int nums,T;
	scanf("%d%d",&nums,&T);
	Cow cow[25000];
	
	for(int i=0;i<nums;i++)
	{
		scanf("%d%d",&cow[i].l,&cow[i].r);

	}
	qsort(cow,nums,sizeof(Cow),cmp);
	if(cow[0].l != 1)
	{
		printf("%d\n",-1);
		return 1;
	}
	int begin=0;
	while(cow[begin].l==1){begin++;}
	int right = cow[begin-1].r;
	int max = right;
	int count_cows =1;
	for(int i=begin;i<nums;i++)
	{
	
		if(cow[i].l <= right+1)
		{
			if(cow[i].r > max )
			{
				max = cow[i].r;
				if(max == T)
				{	
					count_cows++;
					break;
				}
				if(i==nums-1)
					count_cows++;
			
			}
		

		}
		else
		{
			if(cow[i].l-max>1)
			{
				printf("%d\n",-1);
				return 1;

			}
			count_cows++;
			right = max; 
			i--;
		}
	}
	if(max == T)
		printf("%d\n",count_cows);
	else
		printf("%d\n",-1);

	return 0;

}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator