呕~调吐了,实在是调不出来了
  • 板块P1120 小木棍
  • 楼主Blued
  • 当前回复0
  • 已保存回复0
  • 发布时间2023/7/11 16:20
  • 上次更新2023/11/3 10:30:55
查看原帖
呕~调吐了,实在是调不出来了
649751
Blued楼主2023/7/11 16:20

照着第一篇题解的思路打的,

照着第一篇题解调的快仨小时了。

#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N = 70;

int n;

int a[N];

int tot;

int vis[N];

int nex[N];

int i;

int len;

int l , r , mid;

inline int read ()
{
	int x = 0 , f = 1;char ch = getchar ();
	
	while (ch < '0' && ch > '9') {if (ch == '-') f *= -1;ch = getchar ();}
	while (ch >= '0' && ch <= '9') {x = (x << 3) + (x << 1) + ch - '0';ch = getchar ();}
	
	return x * f;
}

inline bool cmp (int x , int y) {return x > y;}

inline void dfs (int now , int la , int num) //now正在拼第几个,la使用的上一个木棍编号,num还剩多长没拼
{
	if (! num)
	{
		if (now == tot / len)
			printf ("%lld\n" , len) , exit (0);
		
		for (i = 1;i <= n;i ++)
			if (! vis[i])
				break;
		
		vis[i] = true;
		
		dfs (now + 1 , i , len - a[i]);
		
		vis[i] = false;
	}
	
	l = la + 1 , r = n;
	
	while (l < r)
	{
		mid = l + r >> 1;
		
		if (a[mid] <= num)
			r = mid;
		else
			l = mid + 1;
	}
	
	//	l = lower_bound (a + la + 1 , a + n + 1 , num) - a + 1;
	
	for (i = l;i <= n;i ++)
		if (! vis[i])
	{
		vis[i] = true;
		
		dfs (now , i , num - a[i]);
		
		vis[i] = false;
		
		i = nex[i];
	}
	
	return ;
}

main ()
{
	n = read ();
	
	for (int i = 1;i <= n;i ++)
		a[i] = read () , tot += a[i];
	
	sort (a + 1 , a + n + 1 , cmp);
	
	nex[n] = n;
	
	for (int i = n - 1;i;i --)
		if (a[i] == a[i + 1])
			nex[i] = nex[i + 1];
	else
		nex[i] = i;
	
	vis[1] = true;
	
	for (len = a[1];len <= tot / 2;len ++)
		if (tot % len)
			continue;
	else
		dfs (1 , 1 , len - a[1]);
	
	
	printf ("%lld\n" , tot);
}
2023/7/11 16:20
加载中...