• hdu 1518 Square


    Square

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 17297    Accepted Submission(s): 5419


    Problem Description
    Given a set of sticks of various lengths, is it possible to join them end-to-end to form a square?
     
    Input
    The first line of input contains N, the number of test cases. Each test case begins with an integer 4 <= M <= 20, the number of sticks. M integers follow; each gives the length of a stick - an integer between 1 and 10,000.
     
    Output
    For each case, output a line containing "yes" if is is possible to form a square; otherwise output "no".
     
    Sample Input
    3 4 1 1 1 1 5 10 20 30 40 50 8 1 7 2 6 4 4 3 5
     
    Sample Output
    yes no yes
     
    //剪枝很重要
    #include <iostream>
    #include <algorithm>
    using namespace std;
    long long a[25],n,s;
    bool v[25];
    int m;
    bool cmp(long long x,long long y)
    {
        return x>y;
    }
    int dfs(long long h, int w, int k)  
    {  
        if(h == s)  
        {  
            w = 1;  
            h = 0;
            k++;  
            if(k == 4)  
                return 1;  
        }  
      
        for(int i=w; i<=m; i++)  
        {  
            if(!v[i])  
            {  
               v[i] = 1;  
                if((h+a[i] <= s ) && dfs(h+a[i], i+1, k))  //有一根无法和其他的拼就直接跳出来
                    //而不是一根根选
                    return 1;  
               v[i] = 0;  
            }  
        }  
        return 0;  
    }  
    int main()
    {
        scanf("%lld",&n);
        while(n--)
        {
            scanf("%d",&m);
            s=0;
            int i;
            for(i=1;i<=m;i++)
            {
                scanf("%lld",&a[i]);
                v[i]=0;
                s=s+a[i];
            }
            if(s%4!=0) {printf("no
    ");continue;}
            s=s/4;
            for(i=1;i<=m;i++)
            {
                if(a[i]>s) {printf("no
    ");break;}
            }
            if(i!=m+1) continue;
            sort(a+1,a+m+1,cmp);
            if(a[0]+a[m-1]>s) {printf("no
    ");break;}    
            if(dfs(0,1,0)) printf("yes
    ");
            else printf("no
    ");
        }
        return 0;
    }
  • 相关阅读:
    [CQOI2009][BZOJ1303] 中位数图
    [JSOI2008][BZOJ1016] 最小生成树计数
    [SCOI2005][BZOJ1083] 繁忙的都市
    [HAOI2006][BZOJ2428] 均分数据
    [HAOI2008][BZOJ1054] 移动玩具
    [ZJOI2006][BZOJ1003] 物流运输trans
    AW241 楼兰图腾
    AW239 奇偶游戏
    AW244 谜一样的牛
    AW278 数字组合(0/1背包)
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/8411115.html
Copyright © 2020-2023  润新知