Mex(Minieye杯第十五届华中科技大学程序设计邀请赛现场同步赛)

链接:https://ac.nowcoder.com/acm/contest/700/J
来源:牛客网
 

Recently MINIEYE's engineer M is working on neural network model training and he has found that if the output of the network is S = (S1, S2, …, Sn), then we can use mex(S) to predict the total training time of the neural network. Define mex(S) as:

 

Here S' ≤ S means S' is a subsequence of S, ∑S' represents the sum of all elements in S'. Please note that S' can be empty, and in that case ∑S' is 0.

M needs your help to calculate mex(S).

 

输入描述:

The first line contains a single integer n(1 ≤ n ≤ 105).

The second line contains n non-negative integers Si(0 ≤ Si < 231).

输出描述:

Print mex(S) in a single line.

示例1

输入

3
1 2 5

输出

4

说明

S'=(), ∑S'=0

S'=(1), ∑S'=1

S'=(2), ∑S'=2

S'=(1,2), ∑S'=3

S'=(5), ∑S'=5

There
is no way for ∑S'=4, hence 4 is the answer.

题意: 找出这n个数不能组成的数的最小值。

思路:每一个数满足的要求大于等于必须是前面所有数的和加一。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#define eps 0.0000000001
#define mem(a) memset(a,0,sizeof(a))
#define MAX 1e9
#define inf 99999999999999999
#define mod 1000000007
using namespace std;
typedef long long ll;
//priority_queue<int,vector<int>,greater<int> > q;
#define mod 1000000007
const int M=1e5+10;
ll a[M];
int main()
{
    ll n,i,j;
    cin>>n;
    for(i=0; i<n; i++)
        cin>>a[i];
    sort(a,a+n);
    ll ans=0;
    if(a[0]==1)
       ans=a[0];
    for(i=1;i<n;i++)
    {
        if(a[i]<=ans+1)
        {
            ans+=a[i];
        }
        else
            break;
    }
    cout<<ans+1<<endl;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值