按照顺序来。

Median Sum

大意:

给你一个集合,求其所有非空子集的权值的中位数。

某集合的权值即为其元素之和。

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 }
AC代码

相关文章:

  • 2021-11-18
  • 2022-12-23
  • 2021-10-28
  • 2021-07-15
  • 2022-01-10
  • 2021-08-26
  • 2022-12-23
猜你喜欢
  • 2021-04-02
  • 2022-12-23
  • 2021-09-29
  • 2021-12-15
  • 2021-11-26
  • 2021-10-18
  • 2021-12-01
相关资源
相似解决方案