ZOJ 1909 I-Square
https://vjudge.net/contest/67836#problem/I
Given a set of sticks of various lengths, is it possible to join them end-to-end to form a square?
Input
The first line of input contains N, the number of test cases. Each test case begins with an integer 4 <= M <= 20, the number of sticks. M integers follow; each gives the length of a stick - an integer between 1 and 10,000.
Output
For each case, output a line containing "yes" if is is possible to form a square; otherwise output "no".
Sample Input
3
4 1 1 1 1
5 10 20 30 40 50
8 1 7 2 6 4 4 3 5
Sample Output
yes
no
yes
代码:
- #include <bits/stdc++.h>
- using namespace std;
- int n, ave, maxx, sum;
- int a[50];
- bool flag = false;
- int vis[50];
- void dfs(int num, int len, int start) {
- if(flag)
- return;
- if(num == 4) {
- flag = true;
- return ;
- }
- if(len == ave) {
- dfs(num + 1, 0, 1);
- if(flag)
- return ;
- }
- for(int i = start; i <= n; i ++) {
- if(vis[i] == 0 && len + a[i] <= ave) {
- vis[i] = 1;
- dfs(num, len + a[i], i + 1);
- vis[i] = 0;
- if(flag)
- return ;
- }
- }
- }
- int main() {
- int T;
- scanf("%d", &T);
- while(T --) {
- scanf("%d", &n);
- maxx = 0, sum = 0;
- for(int i = 1; i <= n; i ++) {
- scanf("%d", &a[i]);
- sum += a[i];
- }
- sort(a + 1, a + 1 + n);
- maxx = a[n];
- ave = sum / 4;
- if(sum % 4 != 0 || maxx > ave) {
- printf("no\n");
- continue;
- }
- memset(vis, 0, sizeof(vis));
- flag = false;
- dfs(0, 0, 1);
- if(flag)
- printf("yes\n");
- else
- printf("no\n");
- }
- return 0;
- }
写这个专题的时候想到暑假被搜索支配的恐惧 写不出来或者很紧张的时候就很喜欢听《最佳歌手》
ZOJ 1909 I-Square的更多相关文章
- zoj 2835 Magic Square(set)
Magic Square Time Limit: 2 Seconds Memory Limit: 65536 KB In recreational mathematics, a magic ...
- ZOJ题目分类
ZOJ题目分类初学者题: 1001 1037 1048 1049 1051 1067 1115 1151 1201 1205 1216 1240 1241 1242 1251 1292 1331 13 ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- ZOJ 3122 Sudoku
Sudoku Time Limit:10000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit Status ...
- POJ 2296 Map Labeler / ZOJ 2493 Map Labeler / HIT 2369 Map Labeler / UVAlive 2973 Map Labeler(2-sat 二分)
POJ 2296 Map Labeler / ZOJ 2493 Map Labeler / HIT 2369 Map Labeler / UVAlive 2973 Map Labeler(2-sat ...
- ZOJ 3890 Wumpus
Wumpus Time Limit: 2 Seconds Memory Limit: 65536 KB One day Leon finds a very classic game call ...
- POJ 1502 MPI Maelstrom / UVA 432 MPI Maelstrom / SCU 1068 MPI Maelstrom / UVALive 5398 MPI Maelstrom /ZOJ 1291 MPI Maelstrom (最短路径)
POJ 1502 MPI Maelstrom / UVA 432 MPI Maelstrom / SCU 1068 MPI Maelstrom / UVALive 5398 MPI Maelstrom ...
- POJ 3076 / ZOJ 3122 Sudoku(DLX)
Description A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells ...
- ZOJ - 3890 Wumpus(BFS基础题)
Wumpus Time Limit: 2 Seconds Memory Limit: 65536 KB One day Leon finds a very classic game call ...
随机推荐
- 【一】调通单机版的thrift-python版本
开发步骤说明 [任务1]调通单机版的thrift-python版本 [任务1]调通单机版的thrift-python版本 安装thrift 创建thrift模块文件并编译 开发python版的clie ...
- django_orm 基本操作
单表操作 增的操作: 一种方式:表名.objects.create(name='xxoo') 第二种方式:表名(name='xxoo') obj=表名(name='xxoo') obj.save() ...
- Python递归二分法
# lst = [4, 56, 178, 253, 625, 1475, 2580, 3574, 15963] # 时间复杂度. n# # 让用户输入一个数n. 判断这个n是否出现在lst中# n = ...
- 关于条件约束问题的无偏差统计——一个偏差控制型生成器(Unbiased Statistics of a Constraint Satisfaction Problem – a Controlled-Bias Generator——by Denis Berthier)
论文地址:https://hal.archives-ouvertes.fr/hal-00641955 Unbiased Statistics of a Constraint Satisfaction ...
- flash读写学习笔记与spi接口及简单测试验证(三)
FPGA中的视频图像资源,以及想要永久存储的程序都是要存储在flash中,flash是FPGA一个不可缺少的部分,flash的种类有很多,根据winbond公司的128Mbit Qual SPI接口的 ...
- Redis的n种妙用,不仅仅是缓存
redis是键值对的数据库,常用的五种数据类型为字符串类型(string),散列类型(hash),列表类型(list),集合类型(set),有序集合类型(zset) Redis用作缓存,主要两个用途: ...
- Android零碎知识点
1.android:foreground="?attr/selectableItemBackground" ###设置水波纹效果 2.android:contentDescri ...
- 成都Uber优步司机奖励政策(2月23日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- struts2-01:作用域传值
方式一.使用ServletActionContext(耦合度高,不建议使用) public String login(){ ServletActionContext.getRequest().getS ...
- spring源码-事件&监听3.6
一.spring中的发布与监听模式,是我们最常用的一种观察者模式.spring在其中做了很多优化,目的就是让用户更好的使用事件与监听的过程. 二.常用的事件与监听中涉及到的接口和类为:Applicat ...