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 }
  • 相关阅读:
    NumPy笔记:运算符(exp,sqrt,square)
    NumPy笔记:常用操作
    php字符操作
    laravel如何实现批量插入
    php中@符号的作用
    laravel如何实现数据的批量插入
    如何在laravel框架中使用阿里云的oss
    laravle如何设置mysql数据表前缀
    thinkphp视图中如何调用并且向控制器传参数
    thinkphp如何隐藏入口文件index.php
  • 原文地址:https://www.cnblogs.com/lovychen/p/4478172.html
Copyright © 2011-2022 走看看