链接: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;
}