Sticks HDU - 1455 (未完成)

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. 
InputThe 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. 
OutputThe 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

 

复制代码
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
 
int n,cnt,sum;//设置全局变量,n表示木棍的数量,sum表示n个木棍的总长度,cnt=sum/i;
 
struct node
{
  int lenth; // 木棍的长度
  int mark; //标记这根木棍是否被用过;
}stick[66];
 
int cmp(node a,node b) //按长度从大到小排序
{
    return a.lenth>b.lenth;
}
 
//len为当前木棒的长度,count统计当前长度木棒的数量,
int dfs(int len,int count,int l,int pos)
{
   if(len==sum) return 1; //递归出口
   if(count==cnt)   return 1;
 
   for(int i=pos;i<n;i++)
   {
      if(stick[i].mark)continue; //如果木棍被标记过,跳过去
      if(len==(stick[i].lenth+l))
      {
          stick[i].mark=1;
          if(dfs(len,count+1,0,0))  return 1;
          stick[i].mark=0;
          return 0;
      }
      else if(len>(stick[i].lenth+l))
      {
          stick[i].mark=1;
          l+=stick[i].lenth;
          if(dfs(len,count,l,i+1))
             return 1;
          l-=stick[i].lenth;  //如果不能拼接,那么就恢复
          stick[i].mark=0;
          if(l==0) return 0;
          while(stick[i].lenth==stick[i+1].lenth)i++;  //如果不剪支跑一遍要140MS,加上剪支跑一遍只要31MS,ORZ
      }
   }
  return 0;
}
 
int main()
{
        while(cin>>n&&n)
        {
           cnt=sum=0;
           for(int i=0;i<n;i++)
           {
               cin>>stick[i].lenth;
               sum+=stick[i].lenth;
               stick[i].mark=0;
           }
           sort(stick,stick+n,cmp);
           for(int i=stick[0].lenth;i<=sum;i++)
           {
              if(sum%i)continue;
              cnt=sum/i;  
              if(dfs(i,0,0,0))
              {
                 printf("%d\n",i);
                 break;
              }
           }
 
        }
        return 0;
}
复制代码

 

posted @   莫莫君不恋爱  阅读(115)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起