Problem Statement

You are given N integers A1A2, ..., AN.

Consider the sums of all non-empty subsequences of A. There are 2N−1 such sums, an odd number.

Let the list of these sums in non-decreasing order be S1S2, ..., S2N−1.

Find the median of this list, S2N−1.

Constraints

  • 1≤N≤2000
  • 1≤Ai≤2000
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A1 A2 AN

Output

Print the median of the sorted list of the sums of all non-empty subsequences of A.

Sample Input 1

3
1 2 1

Sample Output 1

2

In this case, S=(1,1,2,2,3,3,4). Its median is S4=2.

Sample Input 2

1
58

Sample Output 2

58

In this case, S=(58).

我们不妨把空集考虑进来,那么最后的答案就是 第 2^(N-1)+1 小的sum。

显然可以直接O(N^3)dp,最后可以算出和为每个数的集合有多少个。

然后考虑一下怎么优化这个算法。

我们可以发现一个集合和它的补集的和总是 所有数的和,这样我们的dp数组肯定是对称的,即最后 f[0] = f[sum of a[]] ....

所以我们如果要求中位数的话,只需要知道出现过的和的中位数就行了,暴力bitset维护,常数除以了一个32.

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=2000;
int a[maxn+5],n,now;
int num[maxn*maxn+5],T;
bitset<maxn*maxn+maxn> S;
int main(){
scanf("%d",&n),S[0]=1;
for(int i=1;i<=n;i++){
scanf("%d",&now);
S|=S<<now;
}
for(int i=1;i<=maxn*maxn;i++) if(S[i]) num[++T]=i;
printf("%d\n",num[(T+1)>>1]);
return 0;
}

  

Atcoder 3857 Median Sum的更多相关文章

  1. AtCoder3857:Median Sum (Bitset优化背包&&对称性求中位数)

    Median Sum You are given N integers A1, A2, ..., AN. Consider the sums of all non-empty subsequences ...

  2. 2018.08.10 atcoder Median Sum(01背包)

    传送门 题意简述:输入一个数组an" role="presentation" style="position: relative;">anan. ...

  3. AtCoder - 4351 Median of Medians(二分+线段树求顺序对)

    D - Median of Medians Time limit : 2sec / Memory limit : 1024MB Score : 700 pointsProblem Statement ...

  4. 【ATcoder】Xor Sum 2

    题目大意:给定一个 N 个点的序列,求有多少个区间满足\(\oplus_{i=l}^ra[i]=\sum\limits_{i=l}^ra[i]\). 题解: 小结论:\(a\oplus b=a+b\r ...

  5. [AT3857]Median Sum

    题目大意:给定$n$个数,第$i$个数为$a_i$,记这$n$个数的所有非空子集的和分别为$s_1,s_2,\dots,s_{2^n-1}$:求$s$的中位数. 题解:假设考虑的是所有子集,包括空子集 ...

  6. AGC020C Median Sum

    高端操作qaq 又双叒叕读错题了= = 然后重新读题发现不会做了 于是瞅了一波题解 我靠要不要这么暴力呜呜呜 直接bitset O(n^3/w)QAQ 就是f[i]表示i是否能被搞出来 然后我们先不看 ...

  7. AtCoder ABC 129E Sum Equals Xor

    题目链接:https://atcoder.jp/contests/abc129/tasks/abc129_e 题目大意 给定一个二进制表示的数 L,问有多少对自然数 (a, b) 满足 $a + b ...

  8. atcoder题目合集(持续更新中)

    Choosing Points 数学 Integers on a Tree 构造 Leftmost Ball 计数dp+组合数学 Painting Graphs with AtCoDeer tarja ...

  9. 【AtCoder】AGC020

    A - Move and Win 题解 看两个人相遇的时候谁先手即可,相遇之后第一个移动的人必输 代码 #include <bits/stdc++.h> #define fi first ...

随机推荐

  1. Bootstrap图片支持响应式

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  2. tableview和searchbar的适配

    iOS7中,如果用UITableViewStyleGrouped的话,里面的 cell会比原来的拉长了,这样做应该是为了统一和UITableViewStylePlain风格时cell的大小一致,所以改 ...

  3. 【树状数组 离散化】bzoj1573: [Usaco2009 Open]牛绣花cowemb

    解方程题! Description Bessie学会了刺绣这种精细的工作.牛们在一片半径为d(1 <= d <= 50000)的圆形布上绣花. 它们一共绣了N (2 <= N < ...

  4. python面向对象编程(OOP)

    python作为一种解释性语言,其主要的编程方式就是面向对象,而且python的框架django也是主要面向对象的编程. 类(class)和对象(object) 类(class)是用来描述具有相同属性 ...

  5. axure笔记--内置变量

    部件变量: This:当前变量名称 Target:目标变量的名称 x,y表示组件左上角的位置 name:获取当前组件标签命名 Top:获取组件上边界到x轴的距离 bottom:获取组件下边界到x轴的距 ...

  6. kafka异常问题汇总

    1.报错:: java.util.concurrent.ExecutionException: org.apache.kafka.common.errors.NotLeaderForPartition ...

  7. Python9-继承1-day24(大年初一)

    #面向对象编程:'''思想:角色的抽象,创建类,创建角色,面对对象的关键字class 类名: 静态属性 = ‘aaa' def __init__(self):pass 类名.静态属性 ———存储在类的 ...

  8. LeetCode(9)Palindrome Number

    题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...

  9. 小白用shiro(2)

    本文来自网易云社区 作者:王飞 以上的配置走完以后就可以用,下面讲讲个人需求,以及踩过的坑: 1.如何修改cookie的名称,默认名称"rememberMe"太丑了有木有? 首先丢 ...

  10. 设置Putty 字体 颜色 全屏

    效果 1.   字体 2.全屏 3. 颜色 Window->Colours->Default Foreground->Modify设置(我喜欢绿色设置:R:0 G:255 B:0) ...