1041A. Heist

版权声明:大家一起学习,欢迎转载,转载请注明出处。若有问题,欢迎纠正! https://blog.csdn.net/memory_qianxiao/article/details/82729873

A. Heist

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

There was an electronic store heist last night.

All keyboards which were in the store yesterday were numbered in ascending order from some integer number xx. For example, if x=4x=4 and there were 33 keyboards in the store, then the devices had indices 44, 55 and 66, and if x=10x=10 and there were 77 of them then the keyboards had indices 1010, 1111, 1212, 1313, 1414, 1515 and 1616.

扫描二维码关注公众号,回复: 3709457 查看本文章

After the heist, only nn keyboards remain, and they have indices a1,a2,…,ana1,a2,…,an. Calculate the minimum possible number of keyboards that have been stolen. The staff remember neither xxnor the number of keyboards in the store before the heist.

Input

The first line contains single integer nn (1≤n≤1000)(1≤n≤1000) — the number of keyboards in the store that remained after the heist.

The second line contains nn distinct integers a1,a2,…,ana1,a2,…,an (1≤ai≤109)(1≤ai≤109) — the indices of the remaining keyboards. The integers aiai are given in arbitrary order and are pairwise distinct.

Output

Print the minimum possible number of keyboards that have been stolen if the staff remember neither xx nor the number of keyboards in the store before the heist.

Examples

input

Copy

4
10 13 12 8

output

Copy

2

input

Copy

5
7 5 6 4 8

output

Copy

0

Note

In the first example, if x=8x=8 then minimum number of stolen keyboards is equal to 22. The keyboards with indices 99 and 1111 were stolen during the heist.

In the second example, if x=4x=4 then nothing was stolen during the heist.

题意:找出最小数和最大数之间缺少的数的个数。

题解:模拟 排序后,直接从第二个以后开始遍历,判断相邻两个数之差是否等于1,不等于1表示缺的有物品。

c++:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,a[1010],cnt=0;
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>a[i];
    sort(a,a+n);
    for(int i=1;i<n;i++)
        if(a[i]!=a[i-1]+1) cnt+=a[i]-a[i-1]-1;
    cout<<cnt<<endl;
    return 0;
}

python:

n=int(input())
a=list(map(int,input().split()))
a.sort()
ans=0
for i in range(1,n):
    ans+=a[i]-a[i-1]-1
print(ans)

猜你喜欢

转载自blog.csdn.net/memory_qianxiao/article/details/82729873