神仙难做的Minieye杯第十五届华中科技大学程序设计邀请赛现场同步赛 J Mex

16 篇文章 0 订阅
4 篇文章 0 订阅

链接: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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值