Symmetry UVA - 1595
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的更多相关文章
- UVa 1595 (水题) Symmetry
颓废的一个下午,一直在切水题,(ˉ▽ ̄-) 首先如果这些点是对称的话,那么它们的对称轴就是x = m,m是横坐标的平均值. 把这些点放到一个集合里,然后扫描每个点,计算出它关于x = m的对称点,看这 ...
- uva 1595 - Symmetry
思路:首先,如果这些点对称,那么它们的对称轴是x = m(m是所有点横坐标的平均值): 把这些点放到一个集合里,然后扫描每个点,计算出它关于x = m的对称点,看这个点是否在集合里面. 如果有 ...
- uva 1595 Symmetry“结构体”
给出平面上N(N<=1000)个点.问是否可以找到一条竖线,使得所有点左右对称,如图所示: 则左边的图形有对称轴,右边没有. Sample Input 3 5 -2 5 0 0 6 5 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 ...
- UVa 1595 Symmetry (set && math)
题意:给出n个在直角坐标系上的点,问你能不能找出一条竖轴(即垂直于x的轴)使得所有的点根据这条轴对称,能则输出YES,否则输出NO 分析:首先需要找到对称轴的值,将所有n个点的x轴的值加起来然后去除以 ...
- UVa第五章STL应用 习题((解题报告))具体!
例题5--9 数据库 Database UVa 1592 #include<iostream> #include<stdio.h> #include<string.h&g ...
- 【UVA】1595 Symmetry(模拟)
题目 题目 分析 理清思路,上模拟. 代码 #include <bits/stdc++.h> using namespace std; const int maxn=100 ...
- Uva 3226 Symmetry
题目给出一些点的坐标(横坐标,纵坐标),没有重叠的点,求是否存在一条竖线(平行于y轴的线),使线两边的点左右对称. 我的思路:对于相同的纵坐标的点,即y值相同的点,可以将x的总和计算出,然后除以点的数 ...
- UVA 10585 Center of symmetry
题意:给出一个点集,问这个集合有没有中心点使点集对称,这个点可以是点集中的点也可以不是点集的点. 解法:一开始我枚举每两个点连线的中点……结果T了orz当时也不知道怎么想的…… 将点按横坐标排序,如果 ...
随机推荐
- 小白养成记——Java比较器Comparable和Comparator
一.使用情景 1. 调用Arrays.sort()方法或Collections.sort()方法对自定义类的对象排序 以Arrays.sort()为例.假定有如下自定义的Person类 1 publ ...
- Gc root 定义
常说的GC(Garbage Collector) roots,特指的是垃圾收集器(Garbage Collector)的对象,GC会收集那些不是GC roots且没有被GC roots引用的对象.一个 ...
- CentOS 7.7上配置mysql
转载:https://www.cnblogs.com/VinsonYang/p/12333570.html 首先登陆到阿里云,进行远程连接,在这里我使用的是Xshell 6进行连接的. 参照https ...
- 最新版大数据平台安装部署指南,HDP-2.6.5.0,ambari-2.6.2.0
一.服务器环境配置 1 系统要求 名称 地址 操作系统 root密码 Master1 10.1.0.30 Centos 7.7 Root@bidsum1 Master2 10.1.0.105 Cent ...
- 146. LRU 缓存机制 + 哈希表 + 自定义双向链表
146. LRU 缓存机制 LeetCode-146 题目描述 题解分析 java代码 package com.walegarrett.interview; /** * @Author WaleGar ...
- HDOJ-1024(动态规划+滚动数组)
Max Sum Plus Plus HDOJ-1024 动态转移方程:dp[i][j]=max(dp[i][j-1]+a[j],max(dp[i-1][k])+a[j]) (0<k<j) ...
- AJAX 相关参数详细说明
最近ajax的使用十分频繁,对其许多参数还不是很了解,特此总结. 通用写法 1 $.ajax({ 2 url: "http://www.hzhuti.com", //请求的url地 ...
- C语言之三字棋的简单实现及扩展
C语言之三字棋的简单实现及扩展 在我们学习完数组之后,我们完全可以利用数组相关知识来写一个微小型的游戏,比如说今天所说的--三子棋. 大纲: 文件组成 实现 完整代码展示 扩展 即: 一.文件 ...
- 推荐!!! Markdown图标索引网站
作者:三十三重天 博客: http://www.zhouhuibo.club 我们在观察别人的文章时候时,总能看到很多有趣的图标,像是这样
- ES6学习笔记(4)- 解构
一.解构的意义 二.对象解构 三.数组解构