Equal Rectangles
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 a⋅ba⋅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 (1≤q≤5001≤q≤500) — the number of queries. Then qq queries follow.

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

The second line of the query contains 4n4n integers a1,a2,…,a4na1,a2,…,a4n (1≤ai≤1041≤ai≤104), where aiaiis 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

题意:

给你4*n个棍子,问能否用这些棍子组成n个面积相同的矩形

思路:

给n个棍子按大小排序,并统计其数量,第一次用最大和最小的棍子组成矩形,每次也是取出最大和最小的棍子组成矩形来判断是否和原矩形同面积,注意如果棍子用完了就要删掉这种棍子,如果棍子不够或多了就是不合法的要跳出循环

 #include<bits/stdc++.h>
using namespace std;
const int amn=1e4+;
int cnt[amn];
set<int> s;
set<int>::iterator it;
set<int>::iterator rit;
int main(){
int q,n,in;
cin>>q;
while(q--){
memset(cnt,,sizeof cnt);
cin>>n;
s.clear();
for(int i=;i<=*n;i++){
cin>>in;
s.insert(in);
cnt[in]++;
}
it=s.begin(),rit=s.end();rit--;
int a=*it,b=*rit;
int need=(a)*(b),num=;
bool valid=;
if(cnt[a]&&cnt[b]&&cnt[a]%==&&cnt[b]%==){
cnt[a]-=;cnt[b]-=;
num++;
if(cnt[a]==)
s.erase(it++);
else if(cnt[a]<){
valid=;
break;
}
if(a!=b&&cnt[b]==)
s.erase(rit--);
else if(cnt[b]<){
valid=;
break;
}
while(it!=s.end()&&s.size()){
a=*it,b=*rit;
int now=(a)*(b);
if(now==need&&num<n){
if(cnt[a]&&cnt[b]&&cnt[a]%==&&cnt[b]%==){
cnt[a]-=;cnt[b]-=;
num++;
if(cnt[a]==)
s.erase(it++);
else if(cnt[a]<){
valid=;
break;
}
if(a!=b&&cnt[b]==)
s.erase(rit--);
else if(cnt[b]<){
valid=;
break;
}
}
else{
valid=;
break;
}
}
else{
valid=;
break;
}
}
}
else{
valid=;
}
if(valid&&num==n)cout<<"YES\n";
else cout<<"NO\n";
}
}
/***
给你4*n个棍子,问能否用这些棍子组成n个面积相同的矩形
给n个棍子按大小排序,并统计其数量,第一次用最大和最小的棍子组成矩形,每次也是取出最大和最小的棍子组成矩形来判断是否和原矩形同面积,注意如果棍子用完了就要删掉这种棍子,如果棍子不够或多了就是不合法的要跳出循环
***/

[贪心]Codeforces Equal Rectangles的更多相关文章

  1. Codeforces Round #579 (Div. 3) B Equal Rectangles、C. Common Divisors

    B Equal Rectangles 题意: 给你4*n个数,让你判断能不能用这个4*n个数为边凑成n个矩形,使的每个矩形面积相等 题解: 原本是想着用二分来找出来那个最终的面积,但是仔细想一想,那个 ...

  2. CF #579 (Div. 3) B.Equal Rectangles

    B.Equal Rectangles time limit per test2 seconds memory limit per test256 megabytes inputstandard inp ...

  3. B. Equal Rectangles

    B. Equal Rectangles 给定4*N个数,是否能构成N个矩形 面积均相等 每次取两个大的,两个小的 #include<bits/stdc++.h> using namespa ...

  4. 贪心 Codeforces Round #288 (Div. 2) B. Anton and currency you all know

    题目传送门 /* 题意:从前面找一个数字和末尾数字调换使得变成偶数且为最大 贪心:考虑两种情况:1. 有偶数且比末尾数字大(flag标记):2. 有偶数但都比末尾数字小(x位置标记) 仿照别人写的,再 ...

  5. 贪心 Codeforces Round #301 (Div. 2) B. School Marks

    题目传送门 /* 贪心:首先要注意,y是中位数的要求:先把其他的都设置为1,那么最多有(n-1)/2个比y小的,cnt记录比y小的个数 num1是输出的1的个数,numy是除此之外的数都为y,此时的n ...

  6. 线段树+dp+贪心 Codeforces Round #353 (Div. 2) E

    http://codeforces.com/contest/675/problem/E 题目大意:有n个车站,每个车站只能买一张票,这张票能从i+1到a[i].定义p[i][j]为从i到j所需要买的最 ...

  7. 贪心 Codeforces Round #297 (Div. 2) C. Ilya and Sticks

    题目传送门 /* 题意:给n个棍子,组成的矩形面积和最大,每根棍子可以-1 贪心:排序后,相邻的进行比较,若可以读入x[p++],然后两两相乘相加就可以了 */ #include <cstdio ...

  8. 贪心 Codeforces Round #304 (Div. 2) B. Soldier and Badges

    题目传送门 /* 题意:问最少增加多少值使变成递增序列 贪心:排序后,每一个值改为前一个值+1,有可能a[i-1] = a[i] + 1,所以要 >= */ #include <cstdi ...

  9. 贪心 Codeforces Round #303 (Div. 2) B. Equidistant String

    题目传送门 /* 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 */ #include <cstdio> #includ ...

随机推荐

  1. 多个计数器在Vuex中的状态

    安装 安装vue-cli npm i -g vue-cli 生成目录 vue init webpack 启动开发环境 npm run dev 启动命令 npm install -g vue-cli v ...

  2. Errors running builder JavaScript Validator

    问题: 解决方法: 方法一. 选择对应项目—-右键Properties—-Builders—-取消“JavaScript Validator”的勾就OK了 方法二. 找到“.project”文件,找到 ...

  3. Swift iOS实现把PCM语音转成MP3格式

    最近折腾了swift的语音录制识别和转码,这块还是比较坑的,由于语音识别的准确度实测大概也就80%左右,所以还是需要上传录音文件啊.首先是用讯飞语音SDK实现语音录制和识别(语音听写),第一个坑是讯飞 ...

  4. JavaScript的封装和继承

    提到JavaScript"面向对象编程",主要就是封装和继承,这里主要依据阮一峰及其他博客的系列文章做个总结. 继承机制的设计思想 所有实例对象需要共享的属性和方法,都放在这个对象 ...

  5. Spark ML机器学习库评估指标示例

    本文主要对 Spark ML库下模型评估指标的讲解,以下代码均以Jupyter Notebook进行讲解,Spark版本为2.4.5.模型评估指标位于包org.apache.spark.ml.eval ...

  6. 从头认识js-HTML中使用JavaScript

    <script>元素 在HTML页面中插入Javascript的主要办法就是使用<script>元素,HTML4.01为<script>定义了下列6个属性. 1.a ...

  7. @开发者,快来申请你的工业级NXP内核物联网开发板

    米尔工业级NXP开发板试用活动(MYD-C8MMX) 不久前 米尔推出了新一代高性价比核心板之王 MYC-C8MMX核心板及开发板 获得众多客户热烈反馈 这一次 我们给各位带来福利 i.MX8M mi ...

  8. jdbc Template 存储过程 返回多个结果 ,out 输出参数

    public ReportVo getReport() { //执行存储过程 ReportVo reportVo=jdbcTemplate.execute(new CallableStatementC ...

  9. 必备技能二、es6

    一.ES6模块 ES6 引入了模块化,其设计思想是在编译时就能确定模块的依赖关系,以及输入和输出的变量. ES6 的模块化分为导出(export) @与导入(import)两个模块. 特点 ES6 的 ...

  10. webpack的loader和plugin的区别

    [Loader]:用于对模块源码的转换,loader描述了webpack如何处理非javascript模块,并且在buld中引入这些依赖.loader可以将文件从不同的语言(如TypeScript)转 ...