紫书刷题 UVA1595 题解

前排提醒: 我就是个菜鸟,水平很低.

UVA1595

The figure shown on the left is left-right symmetric as it is possible to fold the sheet of paper along a vertical line, drawn as a dashed line, and to cut the figure into two identical halves. The figure on the right is not left-right symmetric as it is impossible to find such a vertical line.

在这里插入图片描述

Write a program that determines whether a figure, drawn with dots, is left-right symmetric or not. The dots are all distinct.

输入格式:

The input consists of T test cases. The number of test cases T is given in the first line of the input file. The first line of each test case contains an integer N, where N (1 ≤ N ≤ 1, 000) is the number of dots in a figure. Each of the following N lines contains the x-coordinate and y-coordinate of a dot. Both x-coordinates and y-coordinates are integers between −10, 000 and 10, 000, both inclusive.

输出格式:

Print exactly one line for each test case. The line should contain ‘YES’ if the figure is left-right symmetric, and ‘NO’, otherwise.

题目大意:

给出笛卡尔坐标系上的N个点,判断是否存在一条竖线,使所有点左右对称。
如图,左侧可以,右侧不可以。

输入样例:
3
5
-2 5
0 0
6 5
4 0
2 3
4
2 3
0 4
4 0
0 0
4
5 14
6 10
5 10
6 14
输出样例:
YES
NO
YES
思路:

map<int, vector<int>>储存所有数据。其中一个Y对应多个X。也就是说,map中的每一个元素都代表一横行。每行所有点按照x值升序排列。先随意取一行,计算最左与最右元素之和,记为mid

然后逐行判断最左与最右元素和是否等于mid,再判断次左与次右元素和是否等于mid,以此类推。

代码:
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

int main()
{
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif

	int t;
	cin >> t;
	while (t--)
	{
		int n;
		cin >> n;
		map<int, vector<int>> mp;
		while (n--)
		{
			int x, y;
			cin >> x >> y;
			mp[y].push_back(x);
		}
		
		bool isdefine = false;
		int mid;
		bool isright = true;
		for (auto v : mp)
		{
			sort(v.second.begin(), v.second.end());
			
			if (!isdefine) {
				mid = v.second[0] + v.second[v.second.size() - 1];
				isdefine = true;
			}
			for (int i = 0; i < v.second.size(); i++)
			{
				if (v.second[i] + v.second[v.second.size() - 1 - i] != mid) {
					isright = false;
					goto endloop;
				}
			}
			
		}
	endloop:;
		if (isright)
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
				
	}	
}
收获:

这破题做半天才做上来,让我认清了我自己,我是个废物。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FengLing255

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值