sicily 1796. Max's kingdom

刚开始还打算用map,后来发现,按x第一关键字, y第二关键字排序即可, 感觉有点像基数排序
 
#include <iostream>

#include <stdio.h>

#include <algorithm>



using namespace std;

#define  MAX 1000000



struct per

{

	long x,y;

};



per num[MAX];



bool cmp (const per& a, const per& b)

{

	if (a.x < b.x) 

		return true;

	else if (a. x > b.x)

		return false;

	else

		return a.y < b.y;

}

int main()

{

	int n;

	long x, y;

	int count = 0;

	int i;

	while (cin >> n)

	{

		 count = 0;

		for ( i = 0; i < n; i++)

			scanf("%ld%ld", &num[i].x, &num[i].y);		

		

		sort(num, num+n, cmp);



		i = 0;

		long key;

	    while (1)

		{

			if (i >= n)

				break;

			key = num[i].x;

			count = 1;

			for (int j = i+1; j < n; j++)

				if (num[j].x == key)

					count++;

				else

					break;

			cout << key << " ";



			if (count % 2 == 0)

				cout << (num[i + (count-1)/2+1].y + num[i+(count-1)/2].y)/2 << endl;

			else

				cout << num[i + (count-1)/2].y << endl;

			i += count;

		}

	

	}

	return 0;	

}

你可能感兴趣的:(dom)