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. 1047 Student List for Course ——PAT甲级真题

    1047 Student List for Course Zhejiang University has 40,000 students and provides 2,500 courses. Now ...

  2. Golang 实现 Redis(9): 使用GeoHash 搜索附近的人

    本文是使用 golang 实现 redis 系列的第九篇,主要介绍如何使用 GeoHash 实现搜索附近的人. 搜索附近的POI是一个非常常见的功能,它的技术难点在于地理位置是二维的(经纬度)而我们常 ...

  3. Linux 网络分析必备技能:tcpdump 实战详解

    大家好,我是肖邦,这是我的第 11 篇原创文章. 今天要分享的是 tcpdump,它是 Linux 系统中特别有用的网络工具,通常用于故障诊断.网络分析,功能非常的强大. 相对于其它 Linux 工具 ...

  4. 边缘计算k8s集群之SuperEdge

    什么是边缘计算? 边缘计算,是指在靠近物或数据源头的一侧,采用网络.计算.存储.应用核心能力为一体的开放平台,就近提供最近端服务.其应用程序在边缘侧发起,产生更快的网络服务响应,满足行业在实时业务.应 ...

  5. java算法题

    1.下面输出结果是什么? public class Test { public static void main(String[] args) { Person person=new Person(& ...

  6. 基于CefSharp开发浏览器(八)浏览器收藏夹栏

    一.前言 上一篇文章 基于CefSharp开发(七)浏览器收藏夹菜单 简单实现了部分收藏夹功能 如(添加文件夹.添加收藏.删除.右键菜单部分功能) 后续代码中对MTreeViewItem进行了扩展,增 ...

  7. vue项目安装sass步骤等遇到的问题

    1.安装sass依赖包 npm install --save-dev sass-loader 注释(可能会出现问题:sass-loader版本过高导致,可以将其package.json中的版本改为7. ...

  8. apk签名、包名

    //通过各手机管理软件,如如360.豌豆荚等查看 //使用命令行,可以查看到permission.packagename.title.versionCode等 aapt dump badging ~/ ...

  9. Python-tkinter-window

    示例代码讲解 1.加载tkinter模块 2.创建一个窗口 3.设置窗口的主题 4.开始窗口的事件循环 import tkinter 2 win = tkinter.Tk() 3 win.title( ...

  10. 8、Spring教程之静态代理/动态代理

    为什么要学习代理模式,因为AOP的底层机制就是动态代理! 代理模式: 静态代理 动态代理 学习aop之前 , 我们要先了解一下代理模式! 静态代理 静态代理角色分析 抽象角色 : 一般使用接口或者抽象 ...