北大poj-1011
木棒
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 136132 | Accepted: 32036 |
Description
Input
Output
Sample Input
9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0
Sample Output
6
5
Source
#include <stdio.h>
#include <stdlib.h>
#include <string.h> #define MAX_NUM 64
#define FAILURE 1
#define SUCCESS 0 int gwStickNum = ;
int gwtotalLen;
int gwData[MAX_NUM];
int gwSelect[MAX_NUM];
int gwGroup; void sort(int *a, int left, int right)
{
if(left >= right)
{
return;
}
int i = left;
int j = right;
int key = a[left];
while(i < j)
{
while(i<j && key>=a[j])
{
j--;
}
a[i] = a[j];
while(i<j && key<=a[i])
{
i++;
}
a[j] = a[i];
}
a[i] = key;
sort(a, left, i-);
sort(a, i+, right);
} int match(int goal, int sum, int nowGroup)
{
int i = ;
if(sum > goal) return FAILURE;
if(sum == goal)
{
nowGroup++;
if(nowGroup == gwGroup) return SUCCESS;
sum = ;
return match(goal, sum, nowGroup);
}
for(i=; i<gwStickNum; i++)
{
if(gwSelect[i] == ) continue;
sum += gwData[i];
gwSelect[i] = ;
if(SUCCESS == match(goal, sum, nowGroup)) return SUCCESS;
gwSelect[i] = ;
sum -= gwData[i];
if(sum == ) return FAILURE;
while(gwData[i] == gwData[i+] && i < gwStickNum)
{
i++;
}
}
return FAILURE;
} int searchFactors(int start)
{
int i = ;
for(i=start+; i<=gwtotalLen; i++)
{
if(gwtotalLen % i ==) return i;
}
return ;
} int calc()
{
int sum = gwData[];
int Len = ;
gwSelect[] = ;
int start = gwData[]-;
while(start < gwtotalLen)
{
Len = searchFactors(start);
gwGroup = gwtotalLen / Len;
if(SUCCESS == match(Len, sum, )) return Len;
start = Len;
memset(gwSelect, , sizeof(gwSelect));
gwSelect[] = ;
sum = gwData[];
}
return ;
} int getTatolLen(int *a, int arryLen)
{
int i = ;
int sum = ;
for(i=; i<arryLen; i++)
{
sum += a[i];
}
return sum;
} int main(void)
{
int i = ;
int result = ;
while(scanf("%d", &gwStickNum), gwStickNum != )
{
for(i=; i<gwStickNum; i++)
{
scanf("%d", &gwData[i]);
} gwtotalLen = getTatolLen(gwData, gwStickNum);
sort(gwData, , gwStickNum-);
result = calc(); printf("%d\n", result);
memset(gwSelect, , sizeof(gwSelect));
} return ;
}
测试数据:
9
15 3 2 8 8 4 11 8 1
0 20
------------------------------------------------------
27
15 3 2 4 11 1 8 8 8 15 3 2 4 11 1 8 8 8 15 3 2 4 11 1 8 8 8
0 20
------------------------------------------------------
9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0 6
5
------------------------------------------------------
46
40 37 32 10 47 4 42 56 61 23 59 36 27 16 16 37 26 19 14 29 31 58 51 32 63 28 11 25 12 15 39 42 46 43 11 19 53 17 39 21 45 44 8 23 51 55
58
57 6 44 4 16 35 54 9 32 23 43 55 46 41 8 41 55 44 31 59 57 58 59 29 53 30 3 39 52 17 32 45 8 40 34 18 20 11 32 33 14 41 31 25 4 42 54 9 29 37 47 29 34 20 47 56 61 5
26
3 64 18 49 4 40 18 61 50 36 17 49 8 17 62 11 24 8 36 59 34 26 28 7 37 26
0 89
89
99
北大poj-1011的更多相关文章
- 北大POJ题库使用指南
原文地址:北大POJ题库使用指南 北大ACM题分类主流算法: 1.搜索 //回溯 2.DP(动态规划)//记忆化搜索 3.贪心 4.图论 //最短路径.最小生成树.网络流 5.数论 //组合数学(排列 ...
- DFS(剪枝) POJ 1011 Sticks
题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...
- OpenJudge 2817:木棒 / Poj 1011 Sticks
1.链接地址: http://bailian.openjudge.cn/practice/2817/ http://poj.org/problem?id=1011 2.题目: 总时间限制: 1000m ...
- POJ 1011 - Sticks DFS+剪枝
POJ 1011 - Sticks 题意: 一把等长的木段被随机砍成 n 条小木条 已知他们各自的长度,问原来这些木段可能的最小长度是多少 分析: 1. 该长度必能被总长整除 ...
- 搜索+剪枝——POJ 1011 Sticks
搜索+剪枝--POJ 1011 Sticks 博客分类: 算法 非常经典的搜索题目,第一次做还是暑假集训的时候,前天又把它翻了出来 本来是想找点手感的,不想在原先思路的基础上,竟把它做出来了而且还是0 ...
- POJ 1011 Sticks 【DFS 剪枝】
题目链接:http://poj.org/problem?id=1011 Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissio ...
- poj 1011
http://poj.org/problem?id=1011 这是一道POJ的搜索的题目,最开始确实难以理解,但做过一些搜索的题目后,也没那么难了. 大概题意就是,现在有N根木头,要拼成若干根木头,并 ...
- POJ 1011 Sticks dfs,剪枝 难度:2
http://poj.org/problem?id=1011 要把所给的集合分成几个集合,每个集合相加之和ans相等,且ans最小,因为这个和ans只在[1,64*50]内,所以可以用dfs一试 首先 ...
- poj 1011 搜索减枝
题目链接:http://poj.org/problem?id=1011 #include<cstdio> #include<cstring> #include<algor ...
- poj 1011 Sticks (DFS+剪枝)
Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 127771 Accepted: 29926 Descrip ...
随机推荐
- (01-02) odoo8.0_Ubuntu14.04_nginx反代理设置
作者:陈伟明联系 : QQ 942923305 | 微信 toby942923305E-mail: cwm.win@hotmail.com============================== ...
- JavaScript判断、循环、Map、Set
本文是笔者在看廖雪峰老师JavaScript教程时的个人总结 一些判断条件 JavaScript把null.undefined.0.NaN和空字符串''视为 ...
- java.lang.NumberFormatException: For input string: "1608020001 " 错误
错误: java.lang.NumberFormatException: For input string: "1608020001 " at java.lang.Numbe ...
- Shell中的空格和引号
空格对于linux的shell是一种很典型的分隔符,所以给变量赋值的时候中间不能够有空格.而单引号和双引号,都是为了解决中间有空格的问题.比如string1=this is a string,这样执行 ...
- 安装Windows 10后PDF补丁丁等程序界面变得模糊的解决办法
对于使用高分辨率屏幕且屏幕缩放比例在 100%以上的用户,升级到 Windows 10 后将发现许多程序的界面,例如QQ.电脑管家.Windows本身的服务管理程序等等,都变得非常模糊,<PDF ...
- Jquery弹窗效果
1.主页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...
- spider_text
__author__ = 'sus'import urllibimport urllib2import re def getPage(url): #获取网页 request = urll ...
- 【资讯】天啦鲁,这十余款创客设计居然由FPGA搞定 [转]
按理说‘高大上’的FPGA,多出现在航天航空(如火星探测器).通信(如基站.数据中心).测试测量等高端应用场景.但麦迪却也发现,近期,在很多创客的作品内部都有FPGA的影子.这或许也从侧面看出,打从总 ...
- My Game --文件读取数据
My Game --线段数据 中说到背景的绘制由贝赛尔曲线生成线段,用 DrawNode 画多边形,同时一张背景有两座山,一座山有两条以上贝赛尔曲线保存,用了嵌套的数据类:Bezier,LineLay ...
- JSTL跳出<c:forEach>循环
<c:forEach items="${consultPager.dataList }" var="consult"> <tr> < ...