Atcoder 3857 Median Sum
Problem Statement
You are given N integers A1, A2, ..., 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 S1, S2, ..., 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的更多相关文章
- AtCoder3857:Median Sum (Bitset优化背包&&对称性求中位数)
Median Sum You are given N integers A1, A2, ..., AN. Consider the sums of all non-empty subsequences ...
- 2018.08.10 atcoder Median Sum(01背包)
传送门 题意简述:输入一个数组an" role="presentation" style="position: relative;">anan. ...
- AtCoder - 4351 Median of Medians(二分+线段树求顺序对)
D - Median of Medians Time limit : 2sec / Memory limit : 1024MB Score : 700 pointsProblem Statement ...
- 【ATcoder】Xor Sum 2
题目大意:给定一个 N 个点的序列,求有多少个区间满足\(\oplus_{i=l}^ra[i]=\sum\limits_{i=l}^ra[i]\). 题解: 小结论:\(a\oplus b=a+b\r ...
- [AT3857]Median Sum
题目大意:给定$n$个数,第$i$个数为$a_i$,记这$n$个数的所有非空子集的和分别为$s_1,s_2,\dots,s_{2^n-1}$:求$s$的中位数. 题解:假设考虑的是所有子集,包括空子集 ...
- AGC020C Median Sum
高端操作qaq 又双叒叕读错题了= = 然后重新读题发现不会做了 于是瞅了一波题解 我靠要不要这么暴力呜呜呜 直接bitset O(n^3/w)QAQ 就是f[i]表示i是否能被搞出来 然后我们先不看 ...
- AtCoder ABC 129E Sum Equals Xor
题目链接:https://atcoder.jp/contests/abc129/tasks/abc129_e 题目大意 给定一个二进制表示的数 L,问有多少对自然数 (a, b) 满足 $a + b ...
- atcoder题目合集(持续更新中)
Choosing Points 数学 Integers on a Tree 构造 Leftmost Ball 计数dp+组合数学 Painting Graphs with AtCoDeer tarja ...
- 【AtCoder】AGC020
A - Move and Win 题解 看两个人相遇的时候谁先手即可,相遇之后第一个移动的人必输 代码 #include <bits/stdc++.h> #define fi first ...
随机推荐
- shell脚本,用awk实现替换文件里面的内容。
文件是这样,有ID和具体信息,ID行以@开头,后面的信息有空格,把第一个空格后的全部内容替换为空格前的字符. 用AWK来实现. @AA10 P 7 #YYYYYYYYYYYYYYYYYYZZZZZZZ ...
- 【倍增】7.11fusion
非常奇妙的倍增题 题目描述 知名科学家小A在2118年在计算机上实现了模拟聚变的过程.我们将她研究的过程简化.核子共有26种,可以用a到z共26个字母表示.核子聚变的过程可以用一个字符串描述.按照顺序 ...
- struct 区别 在C 和C++ 中
C语言中: Struct是用户自定义数据类型(UDT). C++语言中: Struct是抽象数据类型(ADT),支持成员函数的定义. 在C++中,struct的成员的默认访问说 ...
- datetime模块,random模块
6.10自我总结 1.datetime模块(用于修改日期) import datetime print(datetime.datetime.now(),type(datetime.datetime.n ...
- python动态添加属性和方法
---恢复内容开始--- python动态添加属性: class Person(object): def __init__(self,newName,newAge): self.name = newN ...
- 1、初学探讨PYTHON的itchat和wxpy两库
最近好奇学习了python,觉得简单明了,但是最头疼的就是调整空格和调试吧,的确调试不如C#使用visual studio 方便,都是使用print()来调试.也许因为我是菜鸟,如果大家还有更好的方法 ...
- web开发框架Flask学习一
flask框架 用Python做Web开发的三大框架特点 Django 主要特点是大而全,集成了很多的组件,例如:Admin Form Model等,不管你用不用的到,他都会为 你提供,通常用于大型W ...
- 【03】github的markdown语法
[03]github的markdown语法 https://guides.github.com/features/mastering-markdown/(下图)(魔芋:已录入) http://ma ...
- Eclipse如何创建模拟器
Eclipse如何创建模拟器下载地址:http://developer.android.com/sdk/index.html#downloadJDK安装包: 1, 打开安卓模拟器控制台(windows ...
- RN import ** from ** 用法
1.import React, { Component } from 'react': 导入‘react’文件里export的一个默认的组件,将其命名为React以及Component这个非默认组件 ...