AGC020C Median Sum

原题链接:https://beta.atcoder.jp/contests/agc020/tasks/agc020_c

Median Sum

Problem Statement

You are given N N integers A1,A2,,AN.

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

Let the list of these sums in non-decreasing order be S1,S2,,S2N1 S 1 , S 2 , ⋯ , S 2 N − 1 .

Find the median of this list, S2N1 S 2 N − 1 .

Constraints

1N2000 1 ≤ N ≤ 2000
1Ai2000 1 ≤ A i ≤ 2000
All input values are integers.

Input

Input is given from Standard Input in the following format:

N N

A1 A2  AN

Output

Print the median of the sorted list of the sums of all non-empty subsequences of
A 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 S 4 = 2 .

Sample Input 2

1
58

Sample Output 2

58

In this case, S=(58) S = ( 58 ) .

题目大意

输入一个数组 A1,A2,,AN A 1 , A 2 , ⋯ , A N

考虑所有 2N1 2 N − 1 个非空子集,每个子集的权值是包含的所有元素之和。问这 2N1 2 N − 1 个非空子集的权值的中位数是什么?

题解

虽然题目不让我考虑空集,但我偏要考虑。。。

加上空集的话, 2N 2 N 个集合刚好可以两两匹配互为补集,这样 2N 2 N 个子集的中位数就是 sum2 s u m 2 ,去掉空集以后,中位数就是大于等于 sum2 s u m 2 的第一个子集。

题解
#include<bits/stdc++.h>
using namespace std;
const int M=2005;
int n,x,sum;
bitset<M*M>dp;
void in(){scanf("%d",&n);}
void ac()
{
    dp[0]=1;
    for(int i=1;i<=n;++i)scanf("%d",&x),dp|=dp<<x,sum+=x;
    for(int i=sum+1>>1;i<=sum;++i)if(dp[i])printf("%d",i),exit(0);
}
int main(){in();ac();}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ShadyPi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值