B.Equal Rectangles
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given 4𝑛 sticks, the length of the 𝑖-th stick is 𝑎𝑖.
You have to create 𝑛 rectangles, each rectangle will consist of exactly 4 sticks from the given set. The rectangle consists of four 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 𝑎 and 𝑏 is 𝑎⋅𝑏.
Your task is to say if it is possible to create exactly 𝑛 rectangles of equal area or not.
You have to answer 𝑞 independent queries.
Input
The first line of the input contains one integer 𝑞 (1≤𝑞≤500) — the number of queries. Then 𝑞 queries follow.
The first line of the query contains one integer 𝑛 (1≤𝑛≤100) — the number of rectangles.
The second line of the query contains 4𝑛 integers 𝑎1,𝑎2,…,𝑎4𝑛 (1≤𝑎𝑖≤104), where 𝑎𝑖 is the length of the 𝑖-th stick.
Output
For each query print the answer to it. If it is impossible to create exactly 𝑛 rectangles of equal area using given sticks, print "NO". Otherwise print "YES".
input
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
output
YES YES NO YES YES
题意就是给你4*n个小木棒,问你能不能拼成n个面积一样大的矩形。
首先肯定判断一下每种长度木棒的数量是不是2的倍数,然后除2(因为两根一样的视为一组即可)。然后我就随手搞了个map(因为可以很方便的记录数量顺便排序),让最大的和最小的组合,反复组合...搞一搞应该就可以了!
话说WA了一发在break和continue上...我反思...
#include <bits/stdc++.h> using namespace std; int a[502]; int main() { int T,n; scanf("%d",&T); while(T--) { scanf("%d",&n); map<int,int>mp; vector<int> v; for(int i=0;i<4*n;i++){ scanf("%d",&a[i]); mp[a[i]]++; } int flag=0; for(auto i: mp){ // cout<<"EAWF "<<i.first<<" "<<i.second<<endl; if(i.second%2!=0){ flag=1; break; }else { int tmp=i.second; tmp/=2; while(tmp--){ v.push_back(i.first); } } } if(flag){ cout<<"NO"<<endl; continue; } if(n==1){ cout<<"YES"<<endl; continue; } int tid = v.size(); int ans=v[0]*v[tid-1]; for(int i=0;i<tid/2;i++){ // cout<<i<<" "<<v.size()-1-i<<endl; if(v[i]*v[tid-1-i]!=ans){ flag=1; break; } } if(flag){ cout<<"NO"<<endl; }else{ cout<<"YES"<<endl; } } return 0; }