87pts 最后一个点TLE
  • 板块P1120 小木棍
  • 楼主_Oxygen_
  • 当前回复2
  • 已保存回复2
  • 发布时间2023/10/17 10:38
  • 上次更新2023/11/2 13:19:37
查看原帖
87pts 最后一个点TLE
965233
_Oxygen_楼主2023/10/17 10:38

思路在这里

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

constexpr int N = 100010;
// dfs(u, cur, start) u表示当前已经拼到了第u根木棍, cur表示已拼当前的总长, start表示起始下标
int n, sticks[N], sum, length;
bool st[N];

bool dfs(int u, int cur, int start){
	if (u * length == sum)
		return true;
		
	if (cur == length)
		return dfs(u + 1, 0, 0);
		
	for (int i = start; i < n; i ++){
		if (st[i]) continue; // 如果木棒已经用过了
		int l = sticks[i];
		
		if (cur + l > length)
			continue;
		
		st[i] = true;
		if (dfs(u, cur + l, i + 1))
			return true;
		st[i] = false;
		
		if (!cur) return false;
		
		if (cur + l == length) return false;
		
		// 相同长度的
		int j = i;
		while (j < n && sticks[j] == l)
			j ++;
		i = j - 1;
	}
	
	return false;
}

int main(){
		cin >> n;
		sum = 0, length = 0;
		for (int i = 0; i < n; i ++){
			int l;
			cin >> l;
			sticks[i] = l;
			
			
			sum += l;
			length = max(l, length);
		}
		
		sort(sticks, sticks + n);
		reverse(sticks, sticks + n);
		memset(st, false, sizeof st);
			
		while (length){
			if (sum % length == 0 && dfs(0, 0, 0)){
				cout << length << endl;
				break;
			}
			length ++;
		}
	
	return 0;
}
2023/10/17 10:38
加载中...