B. Array Stabilization

cf地址:https://codeforces.com/problemset/problem/1054/B
Initially Ildar has an empty array. He performs n steps. On each step he takes a subset of integers already added to the array and appends the mex of this subset to the array.

The mex of an multiset of integers is the smallest non-negative integer not presented in the multiset. For example, the mex of the multiset [0,2,3] is 1, while the mex of the multiset [1,2,1] is 0.

More formally, on the step m, when Ildar already has an array a1,a2,…,am−1, he chooses some subset of indices 1≤i1<i2<…<ik<m (possibly, empty), where 0≤k<m, and appends the mex(ai1,ai2,…aik) to the end of the array.

After performing all the steps Ildar thinks that he might have made a mistake somewhere. He asks you to determine for a given array a1,a2,…,an the minimum step t such that he has definitely made a mistake on at least one of the steps 1,2,…,t, or determine that he could have obtained this array without mistakes.

Input
The first line contains a single integer n (1≤n≤100000) — the number of steps Ildar made.

The second line contains n integers a1,a2,…,an (0≤ai≤109) — the array Ildar obtained.

Output
If Ildar could have chosen the subsets on each step in such a way that the resulting array is a1,a2,…,an, print −1.

Otherwise print a single integer t — the smallest index of a step such that a mistake was made on at least one step among steps 1,2,…,t.

在这里插入图片描述
Note
In the first example it is possible that Ildar made no mistakes. Here is the process he could have followed.

1-st step. The initial array is empty. He can choose an empty subset and obtain 0, because the mex of an empty set is 0. Appending this value to the end he gets the array [0].
2-nd step. The current array is [0]. He can choose a subset [0] and obtain an integer 1, because mex(0)=1. Appending this value to the end he gets the array [0,1].
3-rd step. The current array is [0,1]. He can choose a subset [0,1] and obtain an integer 2, because mex(0,1)=2. Appending this value to the end he gets the array [0,1,2].
4-th step. The current array is [0,1,2]. He can choose a subset [0] and obtain an integer 1, because mex(0)=1. Appending this value to the end he gets the array [0,1,2,1].
Thus, he can get the array without mistakes, so the answer is −1.

In the second example he has definitely made a mistake on the very first step, because he could not have obtained anything different from 0.

In the third example he could have obtained [0,1,2] without mistakes, but 239 is definitely wrong.

题意:先排序,然后选择去掉第一个还是最后一个(比大小)

#include<iostream>
#include <algorithm>
using namespace std;

int main()
{
	long int n;
	int i, temp, m1, m2;
	long int a[100013];
	temp = 0;
	cin >> n;
	for (i = 0; i < n; i++)
		cin >> a[i];
	sort(a, a + n);
	if (a[1] - a[0] > a[n - 1] - a[n - 2])
		cout << a[n - 1] - a[1] << endl;
	else
		cout << a[n - 2] - a[0] << endl;
	//system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值