题目描述
There is a circular pond with a perimeter of K meters, and N houses around them.
The i-th house is built at a distance of Ai meters from the northmost point of the pond, measured clockwise around the pond.
When traveling between these houses, you can only go around the pond.
Find the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.
Constraints
·2≤K≤106
·2≤N≤2×105
·0≤A1<…<AN<K
·All values in input are integers.
输入
Input is given from Standard Input in the following format:
K N
A1 A2 … AN
输出
Print the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.
样例输入
【样例1】
20 3
5 10 15
【样例2】
20 3
0 5 15
样例输出
【样例1】
10
【样例2】
10
提示
样例1解释
If you start at the 1-st house and go to the 2-nd and 3-rd houses in this order, the total distance traveled will be 10.
样例2解释
If you start at the 2-nd house and go to the 1-st and 3-rd houses in this order, the total distance traveled will be 10.
AC代码
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
ll k,n,t,tmp;
vector<ll> arr;
int main()
{
ios::sync_with_stdio(false);
cin >> k >> n;
for(ll i = 0; i < n; i++)
{
cin >> tmp;
arr.push_back(tmp%k);
}
for(ll i = 1; i < n-1; i++)
{
tmp = max(abs(arr[i] - arr[i-1]),abs(arr[i+1] - arr[i]));
t = max(t,tmp);
}
tmp = max(abs(arr[1] - arr[0]),k - abs(arr[0] - arr[n-1]));
t = max(t,tmp);
tmp = max(abs(arr[n-1] - arr[n-2]),k - abs(arr[0] - arr[n-1]));
t = max(t,tmp);
cout << k - t << endl;
return 0;
}