[贪心]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 ...
随机推荐
- 【深入理解Java虚拟机 】类加载器的命名空间以及类的卸载
类加载器的命名空间 每个类加载器又有一个命名空间,由其以及其父加载器组成 类加载器的命名空间的作用和影响 每个类加载器又有一个命名空间,由其以及其父加载器组成 在每个类加载器自己的命名空间中不能出现相 ...
- sql05
1.Ado.net Ado.net是一组由微软提供的使用C#操作数据库的类库 2.连接 首先引入: using System.Data.SqlClient; 需要使用连接字符串进行连接 using S ...
- 前端每日实战:74# 视频演示如何用纯 CSS 创作一台 MacBook Pro
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/MXNNyR 可交互视频 此视频是可 ...
- python笔记26
一.今日内容 python中的方法 python中的方法+正则表达式的新内容 #分组 #分组命名 #引用分组 #爬虫的小例子 1.转义符 如:\n--->\\n--->print('\\n ...
- python框架Django实战商城项目之工程搭建
项目说明 该电商项目类似于京东商城,主要模块有验证.用户.第三方登录.首页广告.商品.购物车.订单.支付以及后台管理系统. 项目开发模式采用前后端不分离的模式,为了提高搜索引擎排名,页面整体刷新采用j ...
- 关于java性能优化细节方面的建议
在Javva程序中,性能问题的大部分原因并不在于Java语言,而是程序本身,养成一个良好的编码习惯非常重要,能够显著地提升程序性能.下面来聊聊该方面的建议: 1.尽量在合适的场合使用单例: 所谓单例, ...
- python之嵌套 闭包 装饰器 global、nonlocal关键字
嵌套: 在函数的内部定义函数闭包: 符合开放封闭原则:在不修改源代码与调用方式的情况下为函数添加新功能 # global 将局部变量变成全局变量 num = 100 def fn1(): globa ...
- python-面向对象小结
面向对象 小结 1:面向对象:就是一种编程思想 简称oop,指挥某某完成能完成的功能 2:面向对象与面向过程的优缺点 : 面向过程: 优点: 复杂问题简答化(一步一步解决), 流程化, 缺点:机械化 ...
- 搭建flutter开发
最近入坑flutter,dart还没开始学,搭环境就干了我一天半,不容易,记录一下, 我们先立个目标,这是我已经配好的,我是真的有强迫症,需要打四个对勾,真的不容易,我们一个一先说一下每一个都代表什么 ...
- 遍历tree
1.解决方法 filterData (arr) { var newArr = [] arr.map((item) => { var childrenArr if (item.children ! ...