注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

东月之神

在单纯的观念里面,生命就容易变得比较深刻!

 
 
 

日志

 
 
关于我

别驻足,梦想要不停追逐,别认输,熬过黑暗才有日出,要记住,成功就在下一步,路很苦,汗水是最美的书!

网易考拉推荐

sticks   

2009-11-22 21:54:01|  分类: ACM |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
Problem Description
George took sticks of the same length and cut them randomly until all parts became at most 50 units long. Now he wants to return sticks to the original state, but he forgot how many sticks he had originally and how long they were originally. Please help him and design a program which computes the smallest possible original length of those sticks. All lengths expressed in units are integers greater than zero.
 


 

Input
The input contains blocks of 2 lines. The first line contains the number of sticks parts after cutting, there are at most 64 sticks. The second line contains the lengths of those parts separated by the space. The last line of the file contains zero.
 


 

Output
The output file contains the smallest possible length of original sticks, one per line.
 


 

Sample Input
9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0
 


 

Sample Output
6
5
 
以前能过北大的代码始终过不了自己学校的OJ,还有过不了HDU的OJ,发现数据变态,后来一加修改居然过了!!

代码如下:

 

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace
std;

int
len[64], n, minlen, get;
bool
b[64];

int
cmp(const void *a, const void *b)
{

     return
*(int *)a < *(int *)b ? 1 : -1;
}


bool
dfs(int nowlen, int nowget, int cnt)
{

     if
(cnt >= n) return false;
     if
(get == nowget) return true;
     int
i;
     bool
f = false;
     if
(nowlen == 0) f = true;
     for
(i = cnt; i < n; i++)
     {

         if
(!b[i])
         {

                if
(len[i] + nowlen == minlen)
             {

                  b[i] = true;
                  if
(dfs(0, nowget+1, nowget))
                       return
true;
                  b[i] = false;
                  return
false;
             }

            else if
(len[i] + nowlen < minlen)
            {

                 b[i] = true;
                 if
(dfs(nowlen+len[i], nowget, i+1))
                      return
true;
                 b[i] = false;
                 if
(f) return false;  
                 while
(i + 1 < n && len[i] == len[i+1]) i++;
            }
          }
     }

    return
false;
}


int
main()
{

    int
i, tollen;
    while
(scanf("%d", &n), n)
    {

        tollen = 0;
        int
j = 0, p;
        for
(i = 0; i < n; i++)
        {

            scanf("%d", &p);
            if
(p <= 50)
               {

                     len[j] = p;
                  tollen += len[j];
                  j++;
            }
          }

          n = j;
          if
(n == 0)
         {

              printf("0\n");
              continue
;
         }

          qsort(len, n, sizeof(int), cmp);  
          for
(minlen = len[0];  ; minlen++)  
          {

               if
(tollen % minlen) continue; 
               memset(b, 0, sizeof(b));
               get = tollen / minlen;
               if
(dfs(0, 0, 0))
               {

                printf("%d\n", minlen);
                break
;
               }
          }
     }

     return
0;
}

  评论这张
 
阅读(837)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017