Problem - 1203B - Codeforces
codeforces.com
두 변이 쌍으로 존재하는 지 확인
모든 사각형이 하나의 크기로 정의되는 지 확인
- 제목
Equal Rectangles
- 조건
time limit per test : 2 second
memory limit per test : 256 megabytes
input : standard input
output : standard output
- 문제
You are given 4N sticks, the length of the i-th stick is aiai.
You have to create N rectangles, each rectangle will consist of exactly 4 sticks from the given set. The rectangle consists of 4 sides, opposite sides should have equal length and all angles in it should be right. Note that each stick can be used in only one rectangle. Each stick should be used as a side, you cannot break the stick or use it not to the full length.
You want to all rectangles to have equal area. The area of the rectangle with sides a and b is a⋅b.
Your task is to say if it is possible to create exactly N rectangles of equal area or not.
You have to answer q independent queries.
- 입력
The first line of the input contains one integer qq (1≤q≤500) — the number of queries. Then q queries follow.
The first line of the query contains one integer nn (1≤N≤100) — the number of rectangles.
The second line of the query contains 4N integers a1,a2,…,a4n (1≤ai≤10^4), where ai is the length of the i-th stick.
- 출력
For each query print the answer to it. If it is impossible to create exactly nn rectangles of equal area using given sticks, print "NO". Otherwise print "YES".
예제 입력 1 | 5 1 1 1 10 10 2 10 5 2 10 1 1 2 5 2 10 5 1 10 5 1 1 1 2 1 1 1 1 1 1 1 1 1 10000 10000 10000 10000 |
예제 출력 1 | YES YES NO YES YES |
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
bool check;
int test_case, size, * arr, * tmp, size_tmp, sum;
cin >> test_case;
for (int n = 0; n < test_case; n++) {
cin >> size;
size = size * 4;
check = true;
arr = new int[size];
for (int m = 0; m < size; m++)
cin >> arr[m];
sort(arr, arr + size);
for (int m = 0; m < size; m += 2) {
if (arr[m] != arr[m + 1]) {
check = false;
}
}
if (check == false) {
cout << "NO" << endl;
continue;
}
if (size / 4 == 1){
cout << "YES" << endl;
continue;
}
size_tmp = size / 2;
tmp = new int[size_tmp];
for (int m = 0, i = 0; m < size; m += 2, i++) {
tmp[i] = arr[m];
}
sum = tmp[0] * tmp[size_tmp - 1];
for (int m = 1; m < size_tmp / 2; m++) {
if (sum != tmp[m] * tmp[size_tmp - 1 - m]) {
check = false;
break;
}
}
if (check == false) {
cout << "NO" << endl;
continue;
}
cout << "YES" << endl;
}
return 0;
}