• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

  • 联系
  • 订阅
  • 管理

View Post

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;	
}

posted on 2010-12-10 19:48  sysuwhj  阅读(215)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3