按照顺序来。
大意:
给你一个集合,求其所有非空子集的权值的中位数。
某集合的权值即为其元素之和。
1 <= n <= 2000
解:
集合配对,每个集合都配对它的补集。
最大的那个没有配对,所以求(原集合的权值 + 1) >> 1,不小于这个的第一个即为所求。
用bitset实现可行性背包。
1 #include <cstdio> 2 #include <bitset> 3 4 const int N = 2010; 5 6 std::bitset<N * N> bt; 7 8 int a[N]; 9 10 int main() { 11 int n, tot = 0; 12 scanf("%d", &n); 13 for(int i = 1; i <= n; i++) { 14 scanf("%d", &a[i]); 15 } 16 for(int i = 1; i <= n; i++) { 17 bt |= (bt << a[i]); 18 bt[a[i]] = 1; 19 tot += a[i]; 20 } 21 tot = (tot + 1) >> 1; 22 while(bt[tot] == 0) { 23 tot++; 24 } 25 printf("%d", tot); 26 return 0; 27 }