[贪心]Codeforces Equal Rectangles
2 seconds
256 megabytes
standard input
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.
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.
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".
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
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的更多相关文章
- Codeforces Round #579 (Div. 3) B Equal Rectangles、C. Common Divisors
B Equal Rectangles 题意: 给你4*n个数,让你判断能不能用这个4*n个数为边凑成n个矩形,使的每个矩形面积相等 题解: 原本是想着用二分来找出来那个最终的面积,但是仔细想一想,那个 ...
- CF #579 (Div. 3) B.Equal Rectangles
B.Equal Rectangles time limit per test2 seconds memory limit per test256 megabytes inputstandard inp ...
- B. Equal Rectangles
B. Equal Rectangles 给定4*N个数,是否能构成N个矩形 面积均相等 每次取两个大的,两个小的 #include<bits/stdc++.h> using namespa ...
- 贪心 Codeforces Round #288 (Div. 2) B. Anton and currency you all know
题目传送门 /* 题意:从前面找一个数字和末尾数字调换使得变成偶数且为最大 贪心:考虑两种情况:1. 有偶数且比末尾数字大(flag标记):2. 有偶数但都比末尾数字小(x位置标记) 仿照别人写的,再 ...
- 贪心 Codeforces Round #301 (Div. 2) B. School Marks
题目传送门 /* 贪心:首先要注意,y是中位数的要求:先把其他的都设置为1,那么最多有(n-1)/2个比y小的,cnt记录比y小的个数 num1是输出的1的个数,numy是除此之外的数都为y,此时的n ...
- 线段树+dp+贪心 Codeforces Round #353 (Div. 2) E
http://codeforces.com/contest/675/problem/E 题目大意:有n个车站,每个车站只能买一张票,这张票能从i+1到a[i].定义p[i][j]为从i到j所需要买的最 ...
- 贪心 Codeforces Round #297 (Div. 2) C. Ilya and Sticks
题目传送门 /* 题意:给n个棍子,组成的矩形面积和最大,每根棍子可以-1 贪心:排序后,相邻的进行比较,若可以读入x[p++],然后两两相乘相加就可以了 */ #include <cstdio ...
- 贪心 Codeforces Round #304 (Div. 2) B. Soldier and Badges
题目传送门 /* 题意:问最少增加多少值使变成递增序列 贪心:排序后,每一个值改为前一个值+1,有可能a[i-1] = a[i] + 1,所以要 >= */ #include <cstdi ...
- 贪心 Codeforces Round #303 (Div. 2) B. Equidistant String
题目传送门 /* 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 */ #include <cstdio> #includ ...
随机推荐
- js之构造函数的理解
在JavaScript中,创建对象的方式包括两种:对象字面量和使用new表达式.对象字面量是一种灵活方便的书写方式,例如: 1 2 3 4 5 6 var o1 = { p:”I’m in ...
- JobScheduler 和 JobService
使用AlarmManager.IntentService和PendingIntent相互配合,创走周期性的后台任务,实现一个完全可用的后台服务还需要手动执行以下操作. 计划一个周期性任务 ...
- iPhone6爆炸真是小概率事件吗?
前不久,央视新闻报道,根据上海市消费者权益保护委员会统计,2016年9月到11月,共接到8名消费者投诉,反映其苹果手机在正常使用或者正常充电的情况下突然爆炸.此外,苹果手机还被投诉存在自动关机等问题, ...
- 自制一个可编辑QueryString的类URLModifier
有些情况下,需要 新增/删除/替换 url中的部分Querystring中的参数,而.net自带的Uri类只能解析,不能编辑,,并且如果是Relative类型的链接,转成Uri类型之后,很多参数又不能 ...
- [置顶]
利用Python 提醒实验室同学值日(自动发送邮件)
前言: 在实验室里一直存在着一个问题,就是老是有人忘记提醒下一个人值日,然后值日就被迫中断了.毕竟良好的 卫生环境需要大家一起来维护的!没办法只能想出一些小对策了. 解决思路: 首先,我 ...
- 201771010105—达拉草 实验一 软件工程准备—<软件工程构建之法—初步了解和认识>
项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...
- 优秀的Spring Cloud开源项目整理推荐
无论是对于初学者,还是有一定工作经验的程序员来讲,Spring Cloud开源项目都是一笔宝贵的财富.下面给大家整理了十个优秀的开源项目,分别是spring-cloud-examples.spaasc ...
- 从头认识js-基本概念(关键字,保留字,数据类型)
语法 ECMAScript的语法大量借鉴了C及其他类C语言(如Java和Perl)的语法.因此,熟悉这些语言的开发人员在接受ECMSAScript更加宽松的语法时,一定会有一种轻松自在的感觉. 区分大 ...
- Python 绘图 - Bokeh 柱状图小试(Stacked Bar)
背景 在 Bokeh 初探之后,学习使用它来做个图 目标 做一个柱状图,支持多个 y 数据源,即有堆叠效果的柱状图 stacked bar 实现 单数据源 简单的柱状图 参考 Handling Cat ...
- git add的各种情况分类
· git add -A 提交所有变化 · git add -u 提交被修改(modified)和被删除(deleted)文件,不包括新文件(new) · git add . 提交新文件( ...