zoukankan      html  css  js  c++  java
  • poj 1011 Sticks

    Sticks
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 126238   Accepted: 29477

    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 should 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
    uva上面提交超时,poj上可以过;
    AC代码:
     1 #include<algorithm>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<iostream>
     5 using namespace std;
     6 const int N = 1e4+10;
     7 int len[N],sum,L,T;
     8 int used[N];
     9 //bool cmp(int x,int y)
    10 //{
    11 //    if(x>y) return true;
    12 //}
    13 int cmp(const void *a,const void *b)
    14 {
    15     return *(int *)b-*(int *)a;
    16 }
    17 bool DFS(int m,int left)//m为剩余的木棒数,left为当前正在拼接的木棒和假定的木棒长度L还缺少的长度
    18 {
    19     if(m == 0 && left == 0)
    20         return true;
    21     if(left == 0)//一根刚刚拼完
    22         left = L;
    23     for(int i=0; i<T; i++)
    24     {
    25         if(!used[i] && len[i]<=left)
    26         {
    27             if(i>0)//如果前者已经出现过的不能用,则当前的也不能用
    28             {
    29                 if(!used[i-1] && len[i] == len[i-1])
    30                     continue;
    31             }
    32             used[i] = 1;
    33             if(DFS(m-1,left-len[i]))
    34             return true;
    35             else
    36             {
    37                 used[i] = 0;
    38                 if(len[i] == left || left == L)
    39                     return false;
    40             }
    41         }
    42     }
    43     return false;
    44 }
    45 int main()
    46 {
    47     while(scanf("%d",&T) && T)
    48     {   sum  = 0 ;
    49         for(int i=0;i<T;i++)
    50         {
    51             scanf("%d",&len[i]);
    52             sum = sum + len[i];
    53         }
    54         //sort(len,len+T,cmp);          sort  超时
    55         qsort(len,T,sizeof(int),cmp);       //从大到小排序
    56         for(L = len[0];L<=sum/2;L++)
    57         {
    58             if(sum%L) continue;
    59             memset(used,0,sizeof(used));
    60             if(DFS(T,L))
    61             {
    62                 printf("%d
    ",L);
    63                 break;
    64             }
    65         }
    66         if(L>sum/2) printf("%d
    ",sum);
    67     }
    68     return 0;
    69 }
  • 相关阅读:
    CSS命名规范
    dev 从表处理
    core直接获取报异常数据
    SQL根据指定节点ID获取所有父级节点和子级节点(转载)
    bootstrap的tree使用
    ef报错(因为相同类型的其他实体已具有相同的主键值)
    yii初体验(2)创建方法
    yii初体验(1)composer安装+环境检测
    layui数据表格中图片无法自适应问题
    tp5查询数据库排除某字段
  • 原文地址:https://www.cnblogs.com/lovychen/p/4478172.html
Copyright © 2011-2022 走看看