其他分享
首页 > 其他分享> > 洛谷 P1120 小木棍 [数据加强版]

洛谷 P1120 小木棍 [数据加强版]

作者:互联网

洛谷 P1120 小木棍 [数据加强版]

Description

Input

Output

Sample Input

9
5 2 1 5 2 1 5 2 1

Sample Output

6

题解:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define N 105
using namespace std;

int n, t, sum, val, len, cnt;
int a[N];
bool vis[N];

bool dfs(int tot, int cab, int pos)
{
    if(tot > cnt) return 1;
    if(cab == len) return dfs(tot + 1, 0, 1);
    int tag = 0;
    for(int i = pos; i <= n; i++)
        if(!vis[i] && cab + a[i] <= len && tag != a[i])
        {
            vis[i] = 1;
            if(dfs(tot, cab + a[i], i + 1)) return 1;
            tag = a[i];
            vis[i] = 0;
            if(!cab || cab + a[i] == len) return 0;
        }
    return 0;
}

int main()
{
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        cin >> a[++t];
        if(a[t] > 50) t--;
        else sum += a[t], val = max(val, a[t]);
    }
    n = t;
    sort(a + 1, a + 1 + n);
    reverse(a + 1, a + 1 + n);
    for(len = val; len <= sum; len++)
        if(sum % len == 0)
        {
            cnt = sum / len;
            if(dfs(1, 0, 1)) break;
            memset(vis, 0, sizeof(vis));
        }
    cout << len;
    return 0;
}

标签:洛谷,加强版,val,int,长度,len,木棍,P1120,include
来源: https://www.cnblogs.com/BigYellowDog/p/11525161.html