http://acm.hrbust.edu.cn/contests/index.php?act=showproblem&cid=250&p=F
F.背包 | |||||
| |||||
Description | |||||
DS最近刚学会了背包。比如,给一个序列,问是否存在一个子集满足元素和为 X , DS 会用一种方法:
xiaodao 觉得 DS 非常聪明,不过她想难为一下DS。 她给了DS一个序列,序列中有 N(N ≤107)个正整数,满足1≤A1 ,Ai≤Ai+1 ,Ai≤109 | |||||
Input | |||||
第一行是一个整数 T 代表数据组数,以下是 T 组数据。 | |||||
Output | |||||
对于每组数据,输出一个整数 Y 代表本组数据的最小不可构造数。 | |||||
Sample Input | |||||
3 3 1 2 4 2 2 100000 4 1 2 3 4 | |||||
Sample Output | |||||
8 1 11 | |||||
Hint | |||||
对于第一组样例
对于第三组样例
|
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define N 10000005
int n , a[N];
void work()
{
int i , sum = 0;
for (i = 0 ; i < n ; ++ i)
scanf("%d",&a[i]);
for (i = 0 ; i < n ; ++ i)
{
if (sum + 1 < a[i])//sum及其之前的数都能再构,若这个条件不成立,则sum~~sum+a[i]又都能表示了。若成了,则sum+a[i]+1这个数肯定是最小的不能表示的
break;
sum += a[i];
}
printf("%d\n" , sum + 1);
}
int main()
{
int t;
scanf("%d",&t);
while (t--)
{
scanf("%d",&n);
work();
}
return 0;
}