AtCoder3857:Median Sum (Bitset优化背包&&对称性求中位数)
Median Sum
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).
题意:给定N个数,有2^N-1种非空组合,求这些组合的和排序后的中位数。
思路:由对称性,知道中位数略大于Sum/2,所以我们从(Sum+1)/2后面所有的可能中,选择最近的一个。(证明见下面。)
具体实现:需要得到背包结果有哪些可能性,这个只需要Bitset加速一下即可得到。
证明:见前辈写的,很清晰,只要利用对称性即可。ZZZZone
#include<bitset>
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
bitset<maxn*maxn+>s;
int main()
{
int N,x,ans;
while(~scanf("%d",&N)){
s.reset(); ans=;
s[]=;
for(int i=;i<=N;i++){
scanf("%d",&x);
s|=(s<<x);
ans+=x;
}
for(int i=(ans+)/;;i++){
if(s[i]){
printf("%d\n",i);
break;
}
}
}
return ;
}
AtCoder3857:Median Sum (Bitset优化背包&&对称性求中位数)的更多相关文章
- HDU5890:Eighty seven(Bitset优化背包)
Mr. Fib is a mathematics teacher of a primary school. In the next lesson, he is planning to teach ch ...
- 洛谷 P5527 - [Ynoi2012] NOIP2016 人生巅峰(抽屉原理+bitset 优化背包)
洛谷题面传送门 一道挺有意思的题,想到了某一步就很简单,想不到就很毒瘤( 首先看到这样的设问我们显然可以想到背包,具体来说题目等价于对于每个满足 \(i\in[l,r]\) 的 \(a_i\) 赋上一 ...
- 2018.08.10 atcoder Median Sum(01背包)
传送门 题意简述:输入一个数组an" role="presentation" style="position: relative;">anan. ...
- HDU - 6268: Master of Subgraph (分治+bitset优化背包)
题意:T组样例,给次给出一个N节点的点权树,以及M,问连通块的点权和sum的情况,输出sum=1到M,用0或者1表示. 思路:背包,N^2,由于是无向的连通块,所以可以用分治优化到NlgN. 然后背包 ...
- bitset优化背包
题目:https://agc020.contest.atcoder.jp/tasks/agc020_c 回忆下一题,是零一背包,主要的做法就是凑出最接近sum/2的价值,然后发现现在的背包的容量是20 ...
- HDU 5313 bitset优化背包
题目大意: 添加尽可能少的边,最后使图形成二分图 一开始将图区分成一个个联通分量,根据二分图染色,计算出每个联通分量的黑色点和白色点的个数 希望添加的边最少,那么合并的时候,希望黑白块尽可能平均,这无 ...
- hdu 5890 Eighty seven 暴力+bitset优化背包
Eighty seven Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others) P ...
- CCPC 2016 杭州 E. Master of Subgraph(点分治+bitset优化DP)
题目链接:http://acm.hdu.edu.cn/downloads/CCPC2018-Hangzhou-ProblemSet.pdf 题意:给定一棵有 n 个结点的树和一个数 m,对于 i ∈ ...
- HDU 5808 Price List Strike Back bitset优化的背包。。水过去了
http://acm.hdu.edu.cn/showproblem.php?pid=5808 用bitset<120>dp,表示dp[0] = true,表示0出现过,dp[100] = ...
随机推荐
- 数据库数据导出CSV文件,浏览器下载
直接上代码: def download(request): # 从数据库查询数据 data_list = Info.objects.all() # 定义返回对象 response = HttpResp ...
- hdu 4849
简单题,公式计算+最短路.注意点:注意1 取模,2 数组开到n*n+n. #include<iostream> #include<queue> using namespace ...
- hg下拉和上传代码
1.从代码仓库克隆源代码:$ mkdir bzrobot_ws$ cd bzrobot_ws$ hg clone http://192.168.15.88/hg/bzrobot_src src$ ca ...
- Scrum软件开发
Scrum 什么是Scrum Scrum是迭代式增量软件开发过程,通常用于敏捷软件开发.Scrum包括了一系列实践和预定义角色的过程骨架.Scrum中的主要角色包括同项目经理类似的Scrum主管角色负 ...
- cocos2d-x-3.6 引擎概述
cocos2d-x是一个游戏开发引擎,从公布到如今也有五六年了,一路看它慢慢壮大.它是如今应用最多的开源2d引擎,没有之中的一个,据说已经占据90%的市场,所以.对于想从事游戏开发的童鞋来说还是有必要 ...
- SolidEdge如何在零件上写字 如何绘制文字
在草图状态下,插入-文字轮廓 可以按这两个按钮调节文字的大小和位置 之后你可以通过长出或除料把文字凸起或者凹下去 如果你要制作路径文字(比如环形文字),则先绘制一条圆或一段圆弧,并设为构造 ...
- Intel Edison —— 控制GPIO接口,网页显示传感器数值
前言 原创文章,转载引用务必注明链接. 因为是使用Typora(markdown)写好然后复制到论坛的,推荐直接访问我的网站以获得更好地阅读体验. Intel XDK IoT 开发环境很久之前就上手了 ...
- CMS - 认识目录
Tips:如果网页图片(文字)看不清,请按CTRL+鼠标滚轮 一个完整的小程序模板目录结构如下! 本章节给出的建议: 1.推荐使用flex布局 2.其它图片路径建议引入网络路径(tabBar不支持网络 ...
- HDU 2009 整除的尾数 题解
Problem Description 一个整数,仅仅知道前几位,不知道末二位.被还有一个整数除尽了.那么该数的末二位该是什么呢? Input 输入数据有若干组,每组数据包括二个整数a,b(0&l ...
- 生成可重集的排序 (白书P184)
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> us ...