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.

Input

  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.

Output

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

Sample Input

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

Sample Output

YES
NO

HINT

  题目的意思是要判断是否给定的点有一个和y轴平行的对称轴。采用的策略是分开判断,先先判断纵坐标相同的所有点是否围绕和对称,并且求出对称轴的二倍,就是两个最左和最右侧的坐标的和相同。其中一定要对做标数为奇数的进行一次单独处理,将排序后的数组中间的那个赋值一遍加入到数组再排序。判断完成后再对各个纵坐标所求的对称轴是否相同进行判断。

  存储方式是使用map<int,vector>键为纵坐标,值为横坐标数组。然后对每一个纵坐标判断,并将结果存到一个数组中,最后对所有纵坐标求得值进行判断,如果相同输出YES否则NO。

注意:一定要处理相同纵坐标上得点得个数为奇数得情况。

Accepted

#include<iostream>
#include<algorithm>
#include<map>
#include<vector>
using namespace std; int main()
{
int m, n,a,b;
cin >> m;
while (m--) { //m组数据
cin >> n; //每组数据的坐标数目
map<int, vector<int> >arr;//键为纵坐标,值为横坐标数组
vector<int>arry; //存储纵坐标
while (n--) {
cin >> a >> b; //录入坐标
if (!arr.count(b))arry.push_back(b);//将纵坐标录入数组并查重
arr[b].push_back(a); //录入map
}//while n
int flag = 0; //标志位
for (int i = 0;i < arry.size();i++) {//对每一个横坐标进行判断
vector<int>temp; //将键值复制出来
temp = arr[arry[i]];
sort(temp.begin(), temp.end());//排序
if (temp.size() % 2) {
temp.push_back(temp[temp.size() / 2]);//如果是奇数将中间的坐标再次压进数组
sort(temp.begin(), temp.end());//再次排序
}
for (int j = 0;j < temp.size()/ 2;j++)//将两侧的横坐标两两相加并赋值给数组
temp[j] = temp[temp.size() - 1 - j] = temp[j] + temp[temp.size() - 1 - j];
sort(temp.begin(), temp.end()); //再次排序
if (temp.front() == temp[temp.size() - 1])arry[i] = temp.front();//如果过首尾相同。说明纵坐标相同的所有点的横坐标是对称的,将凉凉相加的和赋给记录纵坐标的数组
else { //否则不是对称的,直接退出
flag = 1;break;
}//else
}//for i
if (!flag) { //如果过符合要求
sort(arry.begin(), arry.end()); //如果过所有的元素相等,说明不同的纵坐标之间的对称轴也是相同的
if (arry.front() == arry[arry.size() - 1])cout << "YES" << endl;
else flag = 1; //否则输出NO
}
if (flag)cout << "NO" << endl;
}//while m
}

Symmetry UVA - 1595的更多相关文章

  1. UVa 1595 (水题) Symmetry

    颓废的一个下午,一直在切水题,(ˉ▽ ̄-) 首先如果这些点是对称的话,那么它们的对称轴就是x = m,m是横坐标的平均值. 把这些点放到一个集合里,然后扫描每个点,计算出它关于x = m的对称点,看这 ...

  2. uva 1595 - Symmetry

    思路:首先,如果这些点对称,那么它们的对称轴是x = m(m是所有点横坐标的平均值):   把这些点放到一个集合里,然后扫描每个点,计算出它关于x = m的对称点,看这个点是否在集合里面.   如果有 ...

  3. uva 1595 Symmetry“结构体”

    给出平面上N(N<=1000)个点.问是否可以找到一条竖线,使得所有点左右对称,如图所示: 则左边的图形有对称轴,右边没有.   Sample Input  3 5 -2 5 0 0 6 5 4 ...

  4. UVa 1595 Symmetry(set)

    We call a figure made of points is left-right symmetric as it is possible to fold the sheet of paper ...

  5. UVa 1595 Symmetry (set && math)

    题意:给出n个在直角坐标系上的点,问你能不能找出一条竖轴(即垂直于x的轴)使得所有的点根据这条轴对称,能则输出YES,否则输出NO 分析:首先需要找到对称轴的值,将所有n个点的x轴的值加起来然后去除以 ...

  6. UVa第五章STL应用 习题((解题报告))具体!

    例题5--9 数据库 Database UVa 1592 #include<iostream> #include<stdio.h> #include<string.h&g ...

  7. 【UVA】1595 Symmetry(模拟)

    题目 题目     分析 理清思路,上模拟.     代码 #include <bits/stdc++.h> using namespace std; const int maxn=100 ...

  8. Uva 3226 Symmetry

    题目给出一些点的坐标(横坐标,纵坐标),没有重叠的点,求是否存在一条竖线(平行于y轴的线),使线两边的点左右对称. 我的思路:对于相同的纵坐标的点,即y值相同的点,可以将x的总和计算出,然后除以点的数 ...

  9. UVA 10585 Center of symmetry

    题意:给出一个点集,问这个集合有没有中心点使点集对称,这个点可以是点集中的点也可以不是点集的点. 解法:一开始我枚举每两个点连线的中点……结果T了orz当时也不知道怎么想的…… 将点按横坐标排序,如果 ...

随机推荐

  1. 谁手握账本?趣讲 ZK 的内存模型

    本文作者:HelloGitHub-老荀 Hi,这里是 HelloGitHub 推出的 HelloZooKeeper 系列,免费开源.有趣.入门级的 ZooKeeper 教程,面向有编程基础的新手. 本 ...

  2. 学习一下 SpringCloud (五)-- 配置中心 Config、消息总线 Bus、链路追踪 Sleuth、配置中心 Nacos

    (1) 相关博文地址: 学习一下 SpringCloud (一)-- 从单体架构到微服务架构.代码拆分(maven 聚合): https://www.cnblogs.com/l-y-h/p/14105 ...

  3. 后端程序员之路 54、go 日志库

    一个朋友写的日志库 https://github.com/vizee/echo go get -u -v github.com/vizee/echo package main import (    ...

  4. 后端程序员之路 31、Protocol Buffer

    google/protobuf: Protocol Buffers - Google's data interchange formathttps://github.com/google/protob ...

  5. CentOS Install NMP

    目录 Installation steps of the Nginx install run 默认安装路径 指定安装目录 Installation steps of the MySQL 下载源码包 解 ...

  6. ZooKeeper未授权访问漏洞确认与修复

    目录 探测2181 探测四字命令 用安装好zk环境的客户端连接测试 修复 修复步骤一 关闭四字命令 修复步骤二 关闭未授权访问 zookeeper未授权访问测试参考文章: https://www.cn ...

  7. DES加密详解

    目录 1 根据输入的秘钥得到16个子秘钥 1.1 大致流程 1.2 利用PC-1从K_0中挑出K_1 1.3 利用PC-2从K_1中挑出16个子秘钥 2 利用16个子秘钥对明文进行加密 2.1 大致流 ...

  8. C#类中的成员

    @ 目录 字段 属性 方法 构造函数 类和对象的简单解释 创建类和对象 类中成员的归属问题 字段 字段的声明与声明变量类似,可以添加访问修饰符,通常情况下字段设置为私有的,然后定义属性对字段的读写进行 ...

  9. ES系列(一):编译准备与server启动过程解析

    ES作为强大的和流行的搜索引擎服务组件,为我们提供了方便的和高性能的搜索服务.在实际应用中也是用得比较爽,但如果能够更深入一点.虽然网上有许多的文章已经完整说明,ES是如何如何做到高性能,如何做到高可 ...

  10. 使用自定义注解和切面AOP实现Java程序增强

    1.注解介绍 1.1注解的本质 Oracle官方对注解的定义为: Annotations, a form of metadata, provide data about a program that ...