[贪心]Codeforces Equal Rectangles

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

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int amn=1e4+5;
 4 int cnt[amn];
 5 set<int> s;
 6 set<int>::iterator it;
 7 set<int>::iterator rit;
 8 int main(){
 9     int q,n,in;
10     cin>>q;
11     while(q--){
12         memset(cnt,0,sizeof cnt);
13         cin>>n;
14         s.clear();
15         for(int i=1;i<=4*n;i++){
16             cin>>in;
17             s.insert(in);
18             cnt[in]++;
19         }
20         it=s.begin(),rit=s.end();rit--;
21         int a=*it,b=*rit;
22         int need=(a)*(b),num=0;
23         bool valid=1;
24         if(cnt[a]&&cnt[b]&&cnt[a]%2==0&&cnt[b]%2==0){
25                 cnt[a]-=2;cnt[b]-=2;
26                 num++;
27                 if(cnt[a]==0)
28                     s.erase(it++);
29                 else if(cnt[a]<0){
30                         valid=0;
31                         break;
32                 }
33                 if(a!=b&&cnt[b]==0)
34                     s.erase(rit--);
35                 else if(cnt[b]<0){
36                         valid=0;
37                         break;
38                 }
39             while(it!=s.end()&&s.size()){
40                 a=*it,b=*rit;
41                 int now=(a)*(b);
42                 if(now==need&&num<n){
43                     if(cnt[a]&&cnt[b]&&cnt[a]%2==0&&cnt[b]%2==0){
44                         cnt[a]-=2;cnt[b]-=2;
45                         num++;
46                         if(cnt[a]==0)
47                             s.erase(it++);
48                         else if(cnt[a]<0){
49                                 valid=0;
50                                 break;
51                         }
52                         if(a!=b&&cnt[b]==0)
53                             s.erase(rit--);
54                         else if(cnt[b]<0){
55                                 valid=0;
56                                 break;
57                         }
58                     }
59                     else{
60                         valid=0;
61                         break;
62                     }
63                 }
64                 else{
65                     valid=0;
66                     break;
67                 }
68             }
69         }
70         else{
71             valid=0;
72         }
73         if(valid&&num==n)cout<<"YES\n";
74         else cout<<"NO\n";
75     }
76 }
77 /***
78 给你4*n个棍子,问能否用这些棍子组成n个面积相同的矩形
79 给n个棍子按大小排序,并统计其数量,第一次用最大和最小的棍子组成矩形,每次也是取出最大和最小的棍子组成矩形来判断是否和原矩形同面积,注意如果棍子用完了就要删掉这种棍子,如果棍子不够或多了就是不合法的要跳出循环
80 ***/

 

全部评论

相关推荐

来个大佬救一下,为上投了都是石沉大海了,没实习经历的话怕秋招直接进不了面。什么实习这么难找,基本
心态爆炸了:现在正式的岗位都少,实习基本不咋招的,除了大厂,中小企业其实没那么多岗位需求,就算是有,大多都是招一两个廉价劳动力,同时,他们也会希望你一来就能干活的,没时间培训你,就让你了解公司的项目,你了解完就可以开始干活。再者是,很多低质量的实习其实用处没有那么大的。我去年也是找实习找到破防,最后去了一家深圳的小公司实习,工作对我来说很简单,甚至不如我在学校做的项目,秋招的时候,这段实习经历也并没有帮上什么忙,投递简历,依旧非常低的回复率。低回复率是常态,尤其是找实习,找不到,那就把重心放在优化自己的简历和项目,多看八股文,锻炼自己的面试能力,多看别人的面经,自己模拟面试,等秋招的时候,只要有那么寥寥几次,好好抓住那几次机会。
点赞 评论 收藏
分享
06-12 16:23
已编辑
小米_软件开发(准入职员工)
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
昨天 11:29
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务