北大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 ...
随机推荐
- Laravel RuntimeException inEncrypter.php line 43: The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths
php artisan key:generate 运行上面代码即可解决
- Linux下配置Node.js环境
1.下载代码 下载地址:https://nodejs.org/en/download/ 下载Linux Binaries (.tar.xz)版本,位数根据操作系统位数选择 2.复制代码包 用FTP上传 ...
- Unity3D 中的定时器
不算上 C# 自带的,目前知道两种,以下分别介绍. 1.每帧检查 定义一个时间变量 timer,每帧将此时间减去帧间隔时间 Time.deltaTime,如果小于或者等于零,说明定时器到了,执行相应功 ...
- 【译】Learn ES2015——箭头函数
原文:https://babeljs.io/learn-es2015/ 理解也许有偏差,欢迎指正 箭头函数 不同于函数,箭头函数和包裹它的代码拥有同一个this.如果一个箭头函数在一个function ...
- hexo deploy出错的解决方法
.ERROR Deployer not found: git 执行npm install hexo-deployer-git --save .$ hexo d INFO Deploying: git ...
- 搭建Hadoop2.5.2+Eclipse开发调试环境
一.简介 为了开发调试方便,本文介绍在Eclipse下搭建开发环境,连接和提交任务到Hadoop集群. 二.安装前准备: 1)Eclipse:Luna 4.4.1 2)eclipse插件:hadoop ...
- sphinx索引分析——文件格式和字典是double array trie 检索树,索引存储 – 多路归并排序,文档id压缩 – Variable Byte Coding
1 概述 这是基于开源的sphinx全文检索引擎的架构代码分析,本篇主要描述index索引服务的分析.当前分析的版本 sphinx-2.0.4 2 index 功能 3 文件表 4 索引文件结构 4. ...
- This TableLayout layout or its LinearLayout parent is possibly useless
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- html生成图片并保存到本地方法(Windows)
// 最近用到一个保存html为图片到本地的功能(保存到下载目录),记之,该功能IE使用Blob 存储数据,关于兼容性问题参见如下表格,其他浏览器使用a标签download属性新功能下载 Browse ...
- idea初使用之配置使用maven仓库
idea使用的理由已经无需多说.现在已经超过了eclipse.java开发种占有44%.第一次使用上手还是挺难的.跟用惯了myeclipse的我来说.对于project的概念深入人心.还理解不了它的M ...