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的更多相关文章

  1. zoj 2835 Magic Square(set)

    Magic Square Time Limit: 2 Seconds      Memory Limit: 65536 KB In recreational mathematics, a magic ...

  2. ZOJ题目分类

    ZOJ题目分类初学者题: 1001 1037 1048 1049 1051 1067 1115 1151 1201 1205 1216 1240 1241 1242 1251 1292 1331 13 ...

  3. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  4. ZOJ 3122 Sudoku

    Sudoku Time Limit:10000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status ...

  5. 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 ...

  6. ZOJ 3890 Wumpus

    Wumpus Time Limit: 2 Seconds      Memory Limit: 65536 KB One day Leon finds a very classic game call ...

  7. 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 ...

  8. POJ 3076 / ZOJ 3122 Sudoku(DLX)

    Description A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells ...

  9. ZOJ - 3890 Wumpus(BFS基础题)

    Wumpus Time Limit: 2 Seconds      Memory Limit: 65536 KB One day Leon finds a very classic game call ...

随机推荐

  1. Go 入门 - 方法和接口

    方法和接口 方法的接受者 Go中没有类,取而代之的是在结构体上定义的方法 为了将方法(函数)绑定在某一类结构体上,我们在定义函数(方法)时引入"接受者"的概念. 方法接受者在它自己 ...

  2. 蓝桥杯 算法训练 K好数

    参考:https://blog.csdn.net/jjmjeffrey/article/details/69298110 https://www.cnblogs.com/TWS-YIFEI/p/634 ...

  3. 两步搞定一台电脑同时开启多个tomcat

    1. 修改tomcat中的某些参数,为了避免启动tomcat时出现冲突,编辑bin/startup.bat, 在文件第一行添加如下两行(必须第一行才有效) SET JAVA_HOME=C:\webso ...

  4. BZOJ1085_骑士精神_KEY

    题目传送门 乍一看好像是搜索题,但搜索明显会超时. 此处采用IDA*的方法求解. IDA*算法就是基于迭代加深的A*算法. code: /******************************* ...

  5. EnterpriseDB公司的 Postgres Solution Pack (一)

    下载地址: http://www.enterprisedb.com/products-services-training/products/postgres-plus-solution-pack/do ...

  6. 北京Uber优步司机奖励政策(12月28日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  7. Android AOSP 编译sdk

    首先你要有AOSP 工程. 然后执行下面的命令编译sdk. //运行下面的命令得到编译环境 source build/envsetup.sh lunch sdk make sdk 最后文件输出在哪个位 ...

  8. Git学习系列 (一)

    打算花一个半月的时间学完Git.宏观上有更深的认识. 参考: Pro Git(中文版) 一.历史 本地版本控制系统 最原始的做法.复制整个项目目录的方式来保存不同的版本,或许还会改名加上备份时间以示区 ...

  9. DSP5509的ADC实验

    1. 本次使用esay5509开发板,具体做这个板子叫做大道科技. 2. 5509有2个ADC的输入引脚,就是2个采集通道 3. 看下ADC的寄存器 4. 看下代码中怎么引用ADC的寄存器的,这种写法 ...

  10. vim中project多标签和多窗口的使用

    1.打开多个窗口 打开多个窗口的命令以下几个: 横向切割窗口 :new+窗口名(保存后就是文件名) :split+窗口名,也可以简写为:sp+窗口名 纵向切割窗口名 :vsplit+窗口名,也可以简写 ...