B. Equal Rectangles ( Codeforces Round #579 )

You are given 4n4n sticks, the length of the ii-th stick is aiai.

You have to create nn rectangles, each rectangle will consist of exactly 44 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 aa and bb is aba⋅b.

Your task is to say if it is possible to create exactly nn rectangles of equal area or not.

You have to answer qq independent queries.

Input

The first line of the input contains one integer qq (1q5001≤q≤500) — the number of queries. Then qq queries follow.

The first line of the query contains one integer nn (1n1001≤n≤100) — the number of rectangles.

The second line of the query contains 4n4n integers a1,a2,,a4na1,a2,…,a4n (1ai1041≤ai≤104), where aiai is the length of the ii-th stick.

Output

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".

Example
input
Copy
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
Copy
YES
YES
NO
YES
YES

题意:q组输入,每组输入给出一个整数n,然后输入4n个整数。判断这些数字能否构成n个面积相同的矩形。

分析:要构成N个面积相等的矩形,矩形的面积显然应该是数据中的最小值乘最大值。然后我们对这些数排序,每次从首尾分别取两个数字判断是否构成矩形,面积是否相等即可。

 



#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
typedef long long ll;
using namespace std;
const int INT=1e6+5;
#define lson rt<<1, l, m  
#define rson rt<<1|1, m+1, r
#define read(x) scanf("%d",&x)
#define lread(x) scanf("%lld",&x);
#define pt(x) printf("%d\n",(x))
#define cn cin>>
#define ct cout<<
#define en <<endl
#define rep(j,k) for (int i = (int)(j); i <= (int)(k); i++)
#define mem(s,t) memset(s,t,sizeof(s))
#define re return 0;
#define TLE std::ios::sync_with_stdio(false);
ll a[100000+5],b[10000+5];
priority_queue<ll>q1,q2;
int main()
{
    TLE;
    int t,n,m,mx,flag;
    cn t;
    while(t--)
    {
        map<int,int>mp;
        mx=-INT;
        cn n;
        mem(b,0);
        rep(1,4*n)
        {
            cn a[i];
            b[ a[i] ]++;
        }
        m=0;flag = 0;
        sort(a+1,a+4*n+1);
        mx=a[1]*a[4*n];
        if(n==1 && b[a[1]]==4) 
            ct "YES" en;
        else
        {
            rep(1,4*n-1)
            {
                if(a[i]!=a[i+1] || a[i]*a[4*n-i+1]!=mx)
                {
                    flag = 1;
                    break;                    
                }
                i++;
            }
            if(flag)     ct "NO" en;
            else     ct "YES" en;
        }
    }
    re ;
}
View Code

 

转载于:https://www.cnblogs.com/Shallow-dream/p/11415914.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值