P15453 [JOI 2026 SemiFinal] 衣服 / Clothes & 开这题才发现自己不会写搜索
ImmatureDreamer · · 题解
::::info[无解情况]{open}
当
发现用
::::info[证明]{open}
对于有解情况,为达到
故答案最多为
检查一种方案是否合法时,可以使用背包 DP。但是 bitset 实在太好用啦,我就偷个懒啦。
::::success[AC 代码]
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 85;
int n, ans, a[N], b[10];
bitset<N> bs, tp;
bool check(){
bs.reset();
bs[0] = 1;
for(int i = 1; i <= ans; ++ i)
bs |= (bs << b[i]);
return (bs & tp) == tp;
}
void dfs(int p, int l){
if(l > ans){
if(!check())
return;
cout << "Yes\n" << ans << "\n";
for(int i = 1; i <= ans; ++ i)
cout << b[i] << " ";
exit(0);
}
for(int i = p; i <= a[n]; ++ i)
b[l] = i, dfs(i + 1, l + 1);
}
signed main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n;
for(int i = 1; i <= n; ++ i)
cin >> a[i], a[i] = 23 - a[i];
sort(a + 1, a + n + 1);
if(a[1] < 0)
return cout << "No", 0;
for(int i = 1; i <= n; ++ i)
tp[a[i]] = 1;
for(;;++ ans)
dfs(1, 1);
cout << "I AK IOI";
return 0;
}
::::
::::info[代码中 bitset 操作解释]{open}
for(int i = 1; i <= ans; ++ i)
bs |= (bs << b[i]);
即在原可组合方案基础上,加上所有能得到的值加
return (bs & tp) == tp;
| 判断是否满足: |
|---|