• POJ 1011: Sticks


    #include <iostream>
    #include <string>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <deque>
    #include <map>
    #define range(i,a,b) for(int i=a;i<=b;++i)
    #define LL long long
    #define rerange(i,a,b) for(int i=a;i>=b;--i)
    #define fill(arr,tmp) memset(arr,tmp,sizeof(arr))
    using namespace std;
    int n,len,stick[65];
    bool flag,vis[65];
    void init(){}
    bool cmp(int a,int b){
        return a>b;
    }
    void dfs(int d,int now,int u){
        if(flag)return;
        if(!now){
            int pos=0;
            while(vis[pos])++pos;
            vis[pos]=true;
            dfs(d+1,stick[pos],pos+1);
            vis[pos]=false;
            return;
        }
        if(now==len){
            if(d==n)flag=true;
            else dfs(d,0,0);
            return;
        }
        range(i,u,n-1)
            if(!vis[i]&&now+stick[i]<=len){
                if(!vis[i-1]&&stick[i]==stick[i-1])continue;
                vis[i]=true;
                dfs(d+1,now+stick[i],i+1);
                vis[i]=false;
            }
    }
    void solve(){
        while(cin>>n,n){
            int sum=0;flag=false;
            range(i,0,n-1){
                cin>>stick[i];
                sum+=stick[i];
            }
            sort(stick,stick+n,cmp);
            for(len=stick[0];len<sum;++len)
                if(!(sum%len)) {
                    fill(vis, 0);
                    dfs(0, 0, 0);
                    if (flag)break;
                }
            cout<<len<<endl;
        }
    }
    int main() {
        init();
        solve();
        return 0;
    }
    View Code
  • 相关阅读:
    centos7/RHEL7安装LibreOffice
    CentOS7开机启动管理systemd简介及使用
    Vim使用技巧
    16_用LVM扩展xfs文件系统(当分区空间不够时)
    15_RHEL7挂载NTFS分区
    14_RHEL7安装mplayer
    polyfill-eventsource added missing EventSource to window ie浏览器 解决方案
    关于vue,webpack 中 “exports is not defined”报错
    2018 vue前端面试题
    Error: No PostCSS Config found in... 报错 踩坑记
  • 原文地址:https://www.cnblogs.com/Rhythm-/p/9338629.html
Copyright © 2020-2023  润新知