前排提醒: 我就是个菜鸟,水平很低.
UVA1595
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.
输入格式:
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.
输出格式:
Print exactly one line for each test case. The line should contain ‘YES’ if the figure is left-right symmetric, and ‘NO’, otherwise.
题目大意:
给出笛卡尔坐标系上的N个点,判断是否存在一条竖线,使所有点左右对称。
如图,左侧可以,右侧不可以。
输入样例:
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
输出样例:
YES
NO
YES
思路:
用map<int, vector<int>>
储存所有数据。其中一个Y对应多个X。也就是说,map中的每一个元素都代表一横行。每行所有点按照x值升序排列。先随意取一行,计算最左与最右元素之和,记为mid
。
然后逐行判断最左与最右元素和是否等于mid
,再判断次左与次右元素和是否等于mid
,以此类推。
代码:
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
map<int, vector<int>> mp;
while (n--)
{
int x, y;
cin >> x >> y;
mp[y].push_back(x);
}
bool isdefine = false;
int mid;
bool isright = true;
for (auto v : mp)
{
sort(v.second.begin(), v.second.end());
if (!isdefine) {
mid = v.second[0] + v.second[v.second.size() - 1];
isdefine = true;
}
for (int i = 0; i < v.second.size(); i++)
{
if (v.second[i] + v.second[v.second.size() - 1 - i] != mid) {
isright = false;
goto endloop;
}
}
}
endloop:;
if (isright)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
}
收获:
这破题做半天才做上来,让我认清了我自己,我是个废物。