链接: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.
Meaning
give you N number , calculate the sum of each subsequence (子序列) , you should find the minimum k that sums can’t reach .
Idea
sort the N number
LL k = 0; judge the a[i] > k + 1,if YES , you should break ;
else you can let K plus the a[i];
K is the max number you can reach currently(当前)
last cout our ans “k + 1” !!!
it’s so amazing that I can’t ac this problem in short time .
let’s make example to understand .
5 number
1 2 3 4 5
let’s count the number you reach
it’s easy to find that K
0 1 3 6 10 15
it’s shocking that K can raise so quickly
let’s check the algorithm correctly
0 /1 / 2 3 / 3 4 5 / 4 5 6 7 6 8 9 10/ …
when we write ’ 5 ’ situation we can write the last answer , also the max answer.
15 , then , if we can reach 11 ~ 14 ?
of course !! because we have 1 2 3 4 . we can use max ans to minus these number.
you may say it may can’t reach 11 ~ 14 , because in this example we have 1 2 3 4 , if we don’t have any one of them , if we can also get 11 ~ 14 ?
the answer mast be YES , because we have get some subsequence’s sum is 1 2 3 4 !!
that ’ s so interested , isn’t it ?
then let’ s check the before situations . 0 you mast can get , then ‘1’ , if the first number is bigger than 1 , a[1] > 0 + 1 , you should break then cout k .
easy to know if a[i] > k + 1, you cant’t get the k + 1 , it’s we all know QAQ.
OK , I think if you have read there , you may know this problem . And I think my idea is not rigorous (严谨)
If you have some idea ,you can tell me what’s your idea.
AC code
#include<cstdio>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
ll a[1000005];
int main(){
int n;
ll k = 0;
scanf("%d",&n);
for(int i = 1;i <= n;i++)
cin >> a[i];
sort(a + 1,a + 1 + n);
for(int i = 1;i <= n;i++){
if(a[i] > k + 1) break;
k += a[i];
}
cout << k + 1 << endl;
return 0;
}