习题5-6 对称轴 UVA1595

来源:互联网 发布:联通4g网络接入点设置 编辑:程序博客网 时间:2024/04/30 04:44
#include <iostream>
#include <vector>
#include <map>
using namespace std;
const int MAX=100000000;
//用一个vector到int的字典来存储所有的点,y值作为Key,相同y值的点被存入同一个vector里。
//判断时,先计算第一个key值下的所有数的平均值,然后将之后的Key值下的平均值与它比较,只要有一个不相同则输出NO。
//全相同则输出YES
int main()
{
    int N;
    cin>>N;
    while(N--)
    {
        map<int,vector<int>> points;
        int M;
        cin>>M;
        while(M--)
        {
            int x;
            cin>>x;
            int y;
            cin>>y;
            points[y].push_back(x);
        }
        bool istrue=1;
        bool first=1;
        double sum=0;
        double avr=0;
        for(map<int,vector<int>>::iterator it=points.begin();it!=points.end();it++)
        {
            if(first)
            {
                for(int i=0;i!=(*it).second.size();i++)
                {
                    sum+=(*it).second[i];
                }
                first=0;
                avr=sum/((*it).second.size());
            }
            else
            {
                double tempsum=0;
                double tempavr=0;
                for(int i=0;i!=(*it).second.size();i++)
                {
                    tempsum+=(*it).second[i];
                }
                tempavr=tempsum/((*it).second.size());
                if(tempavr!=avr)
                {
                    istrue=0;
                    break;
                }
            }
        }
        if(istrue==1)
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
}
                                             
0 0