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. kubernetes集群中的pause容器

    昨天晚上搭建好了k8s多主集群,启动了一个nginx的pod,然而每启动一个pod就伴随这一个pause容器,考虑到之前在做kubelet的systemd unit文件时有见到: 1 2 3 4 5 ...

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

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

  3. django之学习前的准备

    一.配置环境 Windows 10操作系统 Python安装配置教程参考:https://www.cnblogs.com/huangbiquan/p/7784533.html 安装Python虚拟环境 ...

  4. 基于Blazor写一个简单的五子棋游戏

    写这个五子棋游戏,其实主要目的是想尝试一下微软新作Blazor.Blazor对于那些搞.NET的程序员,又想做一些前端工作,真的挺友好,不用一句JS就可搞定前端交互,美哉.现在已经有很流行的前端框架, ...

  5. Murata村田研发向左,制造向右

    前言:Murata村田自1944 年在日本京都创立,是陶瓷无源电子元件.无线连接模块和电源转换技术产品设计和制造领域的全球领导者. Murata 一直在为社会的进步和电子行业的革命贡献自己的力量. 在 ...

  6. Python:turtle库的使用及图形绘制

    目录 一.绘制一个八边形 二.绘制一个八角图形 三.简述问题 四.循环程序设计 五.绘制一个自己喜欢的图形 一.绘制一个八边形 使用turtle库,绘制一个八边形 代码: from turtle im ...

  7. APM监控工具Pinpoint搭建

    一:Pinpoint简介与架构图示 Pinpoint是什么Pinpoint是开源在github上的一款使用JAVA语言编写的APM监控工具,用于大规模分布式系统监控.它对性能的影响较小,只增加约3%的 ...

  8. MySQL的字符集和乱码问题

    1.字符集知识 #概述 .字符集是一套文字符号及其编码.比较规则的集合,第一个计算机字符串ASC2 .mysql数据库字符集包括字符集(character)和 校对规则,其中字符集使用来定义mysql ...

  9. ajax+lazyload时lazyload失效问题及解决

    最近写公司的项目的时候遇到一个关于图片加载的问题,所做的页面是一个商城的商品列表页,其中需要显示商品图片,名称等信息,因为商品列表可能会很长,所以其中图片需要滑到可以显示的区域再进行加载. 首先我的图 ...

  10. Yuchuan_Linux_C编程之一 Vim编辑器的使用

    一.整体大纲 二.Vim 编辑器的使用 vi -- vim    vim是从vi发展过来的一款文本编辑器    vi a.txt    前提: 安装了vim软件 工作模式: 1. 命令模式 -- 打开 ...