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 }
  • 相关阅读:
    在smarty模板中截取指定长度的字符串
    HTMl中Meta标签详解以及meta property=og标签含义
    seo标题关键字描述字数限制Title,keywords,description长度最长多长 ?
    网站title,meta,description如何设置,长度大小多少合适!
    php如何开启gd2扩展
    PHP生成带logo图像二维码的两种方法
    [uart]1.Linux中tty框架与uart框架之间的调用关系剖析
    TCP/IP四层模型和OSI七层模型的概念
    [platform]新旧内核的device设备注册对比
    [platform]linux platform device/driver(三)--Platform Device和Platform_driver注册过程之代码对比
  • 原文地址:https://www.cnblogs.com/lovychen/p/4478172.html
Copyright © 2011-2022 走看看