题目
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.
Paste_Image.pngWrite 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
YES
分析
给你一堆点,让你看能不能找到一条竖线使所有点左右对称。思路很简单,开一个20000的vector存点然后对每一行找对称点,看看是不是在一条竖线上,额我写的有点取巧,其实是有样例可以卡掉我的代码,但是竟然过了,,,
所以我又写了个正确的代码,,
ac代码
//ac代码1,错误代码
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
using namespace std;
const int maxn = 2e4 + 10;
const int mid = 1e4 + 5;
vector <int> dot[maxn];
int n;
void init(){
for(int i = 0; i < maxn; ++i){
dot[i].clear();
}
scanf("%d", &n);
for(int i = 0; i < n; ++i){
int t1, t2;
scanf("%d%d", &t1, &t2);
dot[t2 + mid].push_back(t1);
}
}
void solve(){
bool text = 1;
double t;
for(int i = 0; i < maxn; ++i){
if(dot[i].size() == 0) continue;
double sum = 0;
for(int j = 0; j < dot[i].size(); ++j){
sum += dot[i][j];
}
sum /= dot[i].size();
if(text){
t = sum;
text = 0;
}
else {
if(t != sum){
printf("NO\n");
return;
}
}
}
printf("YES\n");
}
int main(){
int t;
scanf("%d", &t);
while(t--){
init();
solve();
}
return 0;
}
//ac代码2,正确(暂时没发现什么错误
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
using namespace std;
const int maxn = 2e4 + 10;
const int mid = 1e4 + 5;
vector <int> dot[maxn];
int n;
void init(){
for(int i = 0; i < maxn; ++i){
dot[i].clear();
}
scanf("%d", &n);
for(int i = 0; i < n; ++i){
int t1, t2;
scanf("%d%d", &t1, &t2);
dot[t2 + mid].push_back(t1);
}
}
void solve(){
bool text = 1;
double t;
for(int i = 0; i < maxn; ++i){
if(dot[i].size() == 0) continue;
double sum = 0;
for(int j = 0; j < dot[i].size(); ++j){
sum += dot[i][j];
}
sum /= dot[i].size();
if(text){
t = sum;
text = 0;
}
else {
if(t != sum){
printf("NO\n");
return;
}
}
}
for(int i = 0; i < maxn; ++i){
for(int j = 0; j < dot[i].size(); ++j){
if(dot[i][j] == t){
continue;
}
else{
int temp = t * 2 - dot[i][j];
for(int k = 0; ; ++k){
if(k == dot[i].size()){
printf("NO\n");
return;
}
else if(dot[i][k] == temp) break;
}
}
}
}
printf("YES\n");
}
int main(){
int t;
scanf("%d", &t);
while(t--){
init();
solve();
}
return 0;
}
网友评论