社区讨论
33pts求救
P1120[CERC 1995] 小木棍参与者 3已保存回复 3
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 3 条
- 当前快照
- 1 份
- 快照标识符
- @lo29a0px
- 此快照首次捕获于
- 2023/10/23 10:05 2 年前
- 此快照最后确认于
- 2023/11/03 10:17 2 年前
CPP
#include <bits/stdc++.h>
using namespace std;
bool dfs(int tg,int idx,vector<int>& sticks,vector<bool>& vis){
if(tg == 0)
return idx == sticks.size();
for(int i = idx;i < sticks.size();i ++) {
if(sticks[i] > tg) return false;
if(!vis[i] && sticks[i] <= tg){
vis[i] = true;
if(dfs(tg - sticks[i],i + 1,sticks,vis))
return true;
vis[i] = false;
}
}
return false;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
std::cin >> n;
vector<int>sticks(n + 1);
for (int i = 1;i <= n;i ++)
std::cin >> sticks[i];
std::sort(sticks.begin(), sticks.end());
int total_length = 0;
for(int i = 1;i <= n;i ++)
total_length += sticks[i];
int min_length = total_length;
for(int length = 1; length <= total_length / 2; length++){
if(total_length % length == 0) {
vector<bool>vis(n + 1,false);
if(dfs(length,0,sticks,vis)){
min_length = length;
break;
}
}
}
std::cout << min_length << std::endl;
return 0;
}
回复
共 3 条回复,欢迎继续交流。
正在加载回复...