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. PAT (Basic Level) Practise (中文)-1027. 打印沙漏(20)

    PAT (Basic Level) Practise (中文)-1027. 打印沙漏(20)  http://www.patest.cn/contests/pat-b-practise/1027 本题 ...

  2. ios多线程原理及runloop介绍

    一.线程概述 有些程序是一条直线,起点到终点:有些程序是一个圆,不断循环,直到将它切断.直线的如简单的Hello World,运行打印完,它的生命周期便结束了,像昙花一现那样:圆如操作系统,一直运行直 ...

  3. vim中使用pydiction对python代码进行补全

    在配置完vim插件YouCompleteMe之后,在vim编写python的时候按tab键会报错,E121: Undefined variable: g:pydiction_location Pydi ...

  4. 【cpu】CPU版本认识

    此图应该是摘选自<鸟哥的linux私房菜>

  5. LeetCode(91) Decode Ways

    题目 A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A ...

  6. luogu2146 [NOI2015]软件包管理器

    安装就把根节点到它全设为 1 删除就把以它为根的子树全设为 0 记得标记初始化为-1,因为标记是 0 的情况也是要处理的. #include <iostream> #include < ...

  7. python基础-range和xrange的区别

    range(start,stop,step): 1.用于循环时使用,可以给定开始,结束,和步长 例如: >>> for i in range(2,10,2):... print i, ...

  8. git删除本地所有的更改

    删除本地所有为暂存的修改: git checkout -f 如果有修改以及加入暂存区的话 那么 使用如下命令: git reset --hard git clean -xdf

  9. 自定义Title

    xml: <jp.co.view.TitleLayout android:id="@+id/titleLayout" android:layout_width="m ...

  10. Win7开启SNMP服务

    通过SNMP监控Windows主机需要在被监控的服务器上安装简单网络管理协议(SNMP)的Windows组件,以Windows 7系统为例: 首先,在控制面板中找到“卸载程序”: 在弹出的窗口中单击“ ...