[luoguP1227] [JSOI2008]完美的对称(sort)

排序算法应用实例
本文介绍了一个使用C++实现的排序算法实例,通过定义结构体并使用标准模板库STL进行排序,来解决特定问题。该实例展示了如何自定义比较函数,并应用于节点数组的排序中。

传送门

 

排序!

 

#include <cstdio>
#include <iostream>
#include <algorithm>
#define N 20001

int n;

struct node
{
	double x, y;
}a[N], b[N];

inline int read()
{
	int x = 0, f = 1;
	char ch = getchar();
	for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
	for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
	return x * f;
}

inline bool cmp1(node x, node y)
{
	return x.x < y.x || (x.x == y.x && x.y < y.y);
}

inline bool cmp2(node x, node y)
{
	return x.x > y.x || (x.x == y.x && x.y > y.y);
}

int main()
{
	int i;
	double x, y;
	n = read();
	for(i = 1; i <= n; i++)
	{
		a[i].x = b[i].x = read();
		a[i].y = b[i].y = read();
	}
	std::sort(a + 1, a + n + 1, cmp1);
	std::sort(b + 1, b + n + 1, cmp2);
	x = a[1].x + b[1].x;
	y = a[1].y + b[1].y;
	for(i = 2; i <= n; i++)
		if(a[i].x + b[i].x != x || a[i].y + b[i].y != y)
		{
			puts("This is a dangerous situation!");
			return 0;
		}
	x /= 2.0;
	y /= 2.0;
	printf("V.I.P. should stay at (%.1lf,%.1lf).\n", x, y);
	return 0;
}

  

转载于:https://www.cnblogs.com/zhenghaotian/p/7498342.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值