zoukankan      html  css  js  c++  java
  • hduoj 1455 && uva 243 E

    http://acm.hdu.edu.cn/showproblem.php?pid=1455

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=243

     

     uva开头描述:

    307 - Sticks

    Time limit: 3.000 seconds

    hduoj 开头描述:

    E - Sticks
    Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu
     
     
    题目描述:
     

    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 file contains blocks of 2 lines. The first line contains the number of sticks parts after cutting. 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


    分析:
    uva会TLE , hduoj AC



    AC代码:
     1 #include<algorithm>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<iostream>
     5 using namespace std;
     6 
     7 const int N = 1e4+10;
     8 int len[N],sum,L,T;
     9 int used[N];
    10 
    11 int cmp(const void *a,const void *b)
    12 {
    13      return *(int *)b-*(int *)a;
    14 }
    15 
    16 bool DFS(int m,int left)
    17 //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 
    46 int main()
    47 {
    48     while(scanf("%d",&T) && T)
    49     {   sum  = 0 ;
    50         for(int i=0;i<T;i++)
    51         {
    52             scanf("%d",&len[i]);
    53             sum = sum + len[i];
    54         }
    55         //sort(len,len+T,cmp);          sort  超时
    56         qsort(len,T,sizeof(int),cmp);       //从大到小排序
    57         for(L = len[0];L<=sum/2;L++)
    58         {
    59             if(sum%L)
    60                 continue;
    61             memset(used,0,sizeof(used));
    62             if(DFS(T,L))
    63             {
    64                 printf("%d
    ",L);
    65                 break;
    66             }
    67         }
    68         if(L>sum/2)
    69             printf("%d
    ",sum);
    70     }
    71     return 0;
    72 }
  • 相关阅读:
    线性筛素数
    m个苹果放入n个盘子问题
    幸运的袋子
    [HNOI2013]消毒
    [SDOI2016]数字配对
    [SCOI2015]小凸玩矩阵
    [JLOI2008]将军
    [HEOI2016/TJOI2016]游戏
    [洛谷4329/COCI2006-2007#1] Bond
    [BZOJ1324]Exca王者之剑
  • 原文地址:https://www.cnblogs.com/jeff-wgc/p/4478480.html
Copyright © 2011-2022 走看看