北大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 ...
随机推荐
- org.springframework.jdbc.UncategorizedSQLException异常
用到了mysql的TRUNCATE函数,这个关键字对spring事务有影响,最后换成了ROUND函数
- jsexcel导出插件
ExcelTable.js /* * author:wenluanlai */ (function ($) { Date.prototype.Format = function (fmt) { var ...
- xampp 端口冲突
最近使用xampp ,提示端口有问题,使用xampp自带的 xampp control修改的端口之后还是不行. 如果是apache端口有问题就修改 xampp\apache\conf\ httpd.c ...
- JS学习笔记01
文章转载pigpigpig4587 的 1.Javascript是区分大小写的语言.也就是说.关键字.变量,函数和所有的标识符都必须采取一致的大小写形式.因为html不严格区分大小写,所以在html中 ...
- jquery.validate.js 表单验证简单用法
引入jquery.validate.js插件以及Jquery,在最后加上这个插件的方法名来引用.$('form').validate(); <!DOCTYPE html PUBLIC " ...
- META-INF文件夹是干啥的,META-INF文件夹的作用, META-INF文件夹能删吗
今天有人问到 META-INF文件夹是干啥的,META-INF文件夹的作用, META-INF文件夹能删吗,还有项目的META-INF下面一般会有个MANIFEST.MF 文件,都是干啥的. 百度搜了 ...
- int unsigned实验
create table t1(a int unsigned,b int unsigned); insert into t1 select 1,2; select 1-2 from t1; Error ...
- C——malloc & free(转载自bccn C语言论坛)
原帖及讨论:http://bbs.bccn.net/thread-82212-1-1.html 原文链接:http://www.bccn.net/Article/kfyy/cyy/jszl/20060 ...
- [CentOS] 指定命令别名:Alias & 软链接生成命令 ln -s
参考:CentOS里alias命令详解 每天一个linux命令(35):ln 命令 1. Alias命令 功能描述:我们在进行系统的管理工作一定会有一些我们经常固定使用,但又很长的命令.那我们可以给这 ...
- java 创建线程的三种方法Callable,Runnable,Thread比较及用法
转自:http://www.chinaitlab.com/Java/line/942440.html 编写多线程程序是为了实现多任务的并发执行,从而能够更好地与用户交互.一般有三种方法,Thread, ...