1595 - Symmetry

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 ( 1N1,
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.

The following shows sample input and output for three test cases.

Sample Input

  1. 3
  2. 5
  3. -2 5
  4. 0 0
  5. 6 5
  6. 4 0
  7. 2 3
  8. 4
  9. 2 3
  10. 0 4
  11. 4 0
  12. 0 0
  13. 4
  14. 5 14
  15. 6 10
  16. 5 10
  17. 6 14

Sample Output

  1. YES
  2. NO
  3. YES

题解:刘汝佳白书135页练习题5.6;本题能够考虑暴力求解,也能够用set的查找来进行;

code:

#include <iostream>

#include <set>

#include <string>

using namespace std;

typedef pair<int,int> point;//定义类型;(不能够用预处理)

set<point> se;

int main()

{

    int cas;

    int n;

    int x,y;

    cin>>cas;

    while(cas--)

    {

        se.clear();

        cin>>n;

        int sum=0;

        for(int i=0; i<n; i++)

        {

            cin>>x>>y;

            sum+=x;//将全部横坐标加和;

            se.insert(point(x*n,y));//(备注1)

        }

        /*
        set<point> ::iterator it;

        for(it=se.begin(); it!=se.end(); ++it)

        {

            point p=*it;

            cout<<p.first<<"  "<<p.second<<endl;

        }
        */

        set<point> ::iterator it;

        bool flag = true;

        for(it=se.begin(); it!=se.end(); ++it)

        {

            point p=*it;

            if(se.find(point(2*sum-p.first,p.second))==se.end())

            {

                flag=false;

                break;

            }

        }

        flag?

cout<<"YES"<<endl: cout<<"NO"<<endl;

    }

    return 0;

}

备注1:

x*n的意思,就是存储的时候将横坐标扩大n倍,由于对称轴肯定是竖线,所以如果对称的话全部的横坐标加和再与n想除得出的应该就是答案的x坐标;之所以不是将sum/n。是由于考虑到精度的问题。除的话会出现double类型;换而言之,这个存储方式就是将全部横坐标扩大了n倍;

uvaoj-1595:symmetry的更多相关文章

  1. uva 1595 - Symmetry

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

  2. uva 1595 Symmetry“结构体”

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

  3. 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 ...

  4. 【UVA】1595 Symmetry(模拟)

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

  5. UVa 1595 Symmetry (set && math)

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

  6. Uva 3226 Symmetry

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

  7. UVa 1595 (水题) Symmetry

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

  8. Symmetry UVA - 1595

      The figure shown on the left is left-right symmetric as it is possible to fold the sheet of paper ...

  9. ACM/ICPC 之 数论-素数筛选法 与 "打表"思路(POJ 1595)

    何为"打表"呢,说得简单点就是: 有时候与其重复运行同样的算法得出答案,还不如直接用算法把这组数据所有可能的答案都枚举出来存到一个足够大的容器中去-例如数组(打表),然后再输入数据 ...

随机推荐

  1. 今日SGU 5.14

    //SGU 131 还没完全想清楚 留坑 SGU 259 题意:一个机器处理n个任务,每个任务有时间t和传送时间l 收获:贪心 #include<bits/stdc++.h> #defin ...

  2. Swift学习笔记(12)--数组和字典的复制

    Swift中,数组Array和字典Dictionary是用结构来实现的,但是数组与字典和其它结构在进行赋值或者作为参数传递给函数的时候有一些不同. 并且数组和字典的这些操作,又与Foundation中 ...

  3. IDEA 热启动,每次更改代码后不用重启服务

    1.ctrl+Shift+Alt+/,选择Registry 2.勾选 compiler.automake.allow.when.app.running(可能不按首字母排序,可以多找找) 3.Setti ...

  4. vim 常用插件功能跟配置

    在之前的公司,一直是使用别人配置好的vim 环境,他当时配置的功能很强大,查看源码的时候,非常的方便.至少我一直都是用它来看源码,从来没有使用过source insight.现在换了工作,但之前养成的 ...

  5. reactor官方文档译文(2)Reactor-core模块

    You should never do your asynchronous work alone. — Jon Brisbin 完成Reactor 1后写到 You should never do y ...

  6. Exercise : Softmax Regression

    Step 0: Initialize constants and parameters Step 1: Load data Step 2: Implement softmaxCost Implemen ...

  7. LBP 特征

    LBP(Local Binary Pattern,局部二值模式)是一种用来描述图像局部纹理特征的算子:它具有旋转不变性和灰度不变性等显著的优点.用于纹理特征提取.而且,提取的特征是图像的局部的纹理特征 ...

  8. C语言-常量指针与指针常量

    最近倪健问我一个问题,他说:什么是常指针?什么是指向常变量的指针?请举例说明 我查阅资料后这么回答他了, 指针常量(常指针):int * const p : 指针是一个常量,也就是说它始终指向那个地址 ...

  9. tortoisegit--无法commit

    tortoisegit--无法提交,好几次使用tortoisegit,commit的时候出现卡死的情况,导致无法提交代码. 分析错误以为是: 电脑的性功能有关,但重启好几次电脑一人无济于事,打开任务管 ...

  10. php 根据html table生成excel文件

    <?php /* *处理Excel导出 *@param $datas array 设置表格数据 *@param $titlename string 设置head *@param $title s ...