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. php源码建博客2--实现单入口MVC结构

    主要: MVC目录结构 数据库工具类制作 创建公共模型类和公共控制器类 --------------文件结构:-------------------------------------- blog├─ ...

  2. PHP连接MySql闪断自动重连的方法

    使用php作为后台运行程序(例如短信群发),在cli模式下执行php,php需要连接mysql循环执行数据库处理. 当mysql连接闪断时,之后循环的执行将会失败. 我们需要设计一个方法,当mysql ...

  3. Java异常链

    是什么 一种面向对象的编程技术,将捕获到的异常重新封装到一个新的异常中,并重新抛出. 有什么用 可以保留每一层的异常信息,用户查看异常的时候,能够从顶层异常信息看到底层异常信息. 怎么用 catch异 ...

  4. 第二节 双向链表的GO语言实现

    一.什么是双向链表 和单链表比较,双向链表的元素不但知道自己的下线,还知道自己的上线(越来越像传销组织了).小煤车开起来,图里面可以看出,每个车厢除了一个指向后面车厢的箭头外,还有一个指向前面车厢的箭 ...

  5. pygame---制作一只会转弯的小乌龟

    Pygame Pygame是跨平台Python模块,专为电子游戏设计,包含图像.声音.建立在SDL基础上,允许实时电子游戏研发而无需被低级语言(如机器语言和汇编语言)束缚. 包含图像.声音. 建立在S ...

  6. mybatis入门(一):jdbc的缺点

    mybatis的基础内容 1.mybatis的框架原理 2.mybatis开发dao两种方法: a.原始dao开发方法(程序需要编写dao接口和dao实现类) b.mybatis的mapper接口(相 ...

  7. 北京Uber优步司机奖励政策(1月17日)

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

  8. Filter配置多个url-pattern

    java开发中会用的Filter过滤器,有时候开发需要,在一个Filter中需要配置多个过滤地址,即<url-pattern>,下面就说一下一个Filter过滤器中多个<url-pa ...

  9. MySql 增加字段 删除字段 修改字段名称 修改字段类型

    //1.增加一个字段 alter table user add COLUMN new1 VARCHAR(20) DEFAULT NULL; //增加一个字段,默认为空 alter table user ...

  10. WSL跑linux服务程序

    前段时间折腾了一次WSL下的Apache,无奈遇到各种奇葩问题,总是解决不了,最终放弃,甚至得出了一个现在看来比较可笑的结论:WSL是不可能跑Linux服务程序的! 当时的思路想歪了,由于Apache ...