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. (4)JSTL的SQL标签库

    jstl的SQL标签库 SQL tag Library中的标签用来提供在 JSP 页面中可以与数据库进行交互的功能Database access标签库有以下6组标签来进行工作: <sql:set ...

  2. shell脚本,awk替换{}里面的内容

    如何将oxo{axbxc}oxo{dxexf}oxo里面的{}里面的x 替换为; 用awk实现 [root@localhost 09-30]# echo 'oxo{axbxc}oxo{dxexf}ox ...

  3. java数字金额转化为中文金额

    public static String digitUppercase(double n){String fraction[] = {"角", "分"};Str ...

  4. vue axios 请求本地接口端口不一致出现跨域设置代理

    首先在config下面的index.js,设置跨域代理 在axios请求的时候     用'/api/' 替代baseURL 最重要的就是设置完必须重新 npm run dev 否则不生效

  5. springboot 修炼之路

    网上无意中发现一份关于springboot的教程说明,说的很详细,大家可以参考.具体地址:http://www.spring4all.com/article/246

  6. GIMP暗黑诱惑,部分彩色效果制作

    在一些图形处理中经常会用到高逼格的部分彩色,其他部分黑白的效果,今天我就简单记录一下如何操作. 1.选区,先选择要突出的选区,可以用多种方法,钢笔,套绳,小剪刀等等: 2.把选择的区域稍稍调整亮一点: ...

  7. centos 7 中文乱码的解决办法

    @@首先查看系统的操作版本,我的版本是centos 7.2 的. @@查看系统是否有安装中文语言包,一般我们在安装的时候系统都会默认的为我们安装上去的. locale -a | grep " ...

  8. linux定时任务执行php任务

    首先用命令检查服务是否在运行 systemctl status crond.service 如果服务器上没有装有crontab ,则可以执行 yum install vixie-cron yum in ...

  9. tensorboard以时间命名每一个文件夹

    tensorboard 有一个良好的命名习惯以时间命名每一个文件夹,例如**20190523_081232** ''' from datetiome import datetime dir = os. ...

  10. JQuery基本事件函数

    1,click单击事件 2,blur失去光标事件,focus获得光标事件 3,JQuery.on()函数:为html元素绑定事件,如下代码: $("div").on("c ...