年轮广场
题号:NC13583
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld
题目描述
在云塘校区,有一个很适合晒太阳的地方————年轮广场
年轮广场可以看成n个位置顺时针围成一个环。
这天,天气非常好,Mathon带着他的小伙伴们出来晒太阳。他们分别坐在A[i]位置上,每个位置上保证最多只有1个小伙伴。现在Mathon想让大家集合玩狼人杀,所以想选择一个位置集合,之后所有的人顺时针或逆时针移动到那里去,每移动两个相邻的位置需要1个单位时间,小伙伴们都很有素质所以不会插近路踩草坪,只会沿着位置走。
Mathon想越快集合越好,于是他在群里发了QQ消息告诉大家集合位置,假设所有人都立刻接到了消息,然后都以最优的方法往集合位置移动。
要把所有人集合到一个位置最少需要多少时间?
输入描述:
多组输入 每组数据第一行输入n,m(1<=n,m<=1000),n表示年轮广场上位置的个数,m表示Mathon以及他的小伙伴总人数 第二行m个数,第i个A[i](1<=A[i]<=n)表示第i个人的位置。
输出描述:
每组数据输出一行,表示所需要的最少时间。
示例1
输入
5 3 1 4 5 3 2 1 3
输出
1 1
说明
第一组数据选择5为集合地点,第二组数据选择2为集合地点。
//#include <bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<list>
#include<set>
#include<iomanip>
#include<cstring>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<cassert>
#include<sstream>
#include<algorithm>
using namespace std;
const int mod=1e9+7;
typedef long long ll;
#define ls (p<<1)
#define rs (p<<1|1)
#define mid (l+r)/2
#define over(i,s,t) for(register long long i=s;i<=t;++i)
#define lver(i,t,s) for(register long long i=t;i>=s;--i)
const int MAXN = 305;
const int INF = 0x3f3f3f3f;
const int N=5e4+7;
const int maxn=1e5+5;
const double EPS=1e-10;
const double Pi=3.1415926535897;
//inline double max(double a,double b){
// return a>b?a:b;
//}
//inline double min(double a,double b){
// return a<b?a:b;
//}
int xd[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int yd[8] = {1, 0, -1, 0, -1, 1, -1, 1};
//void Fire(){
// queue<node> p;
// p.push({fx,fy,0});
// memset(fire, -1, sizeof(fire));
// fire[fx][fy]=0;
// while(!p.empty()){
// node temp=p.front();
// p.pop();
// for(int i=0;i<8;i++){
// int x=temp.x+xd[i];
// int y=temp.y+yd[i];
// if(x<0||x>=n||y<0||y>=m||fire[x][y]!=-1){
// continue;
// }
// fire[x][y]=temp.val+1;
// p.push({x,y,temp.val+1});
// }
// }
//}
//int bfs(){
// queue<node> p;
// memset(vis, 0, sizeof(vis));
// p.push({sx,sy,0});
// while (!p.empty()) {
// node temp=p.front();
// vis[temp.x][temp.y]=1;
// p.pop();
// for(int i=0;i<4;i++){
// int x=temp.x+xd[i];
// int y=temp.y+yd[i];
// if(x<0||x>=n||y<0||y>=m) continue;
// if(x==ex&&y==ey&&temp.val+1<=fire[x][y]) return temp.val+1;
// if(vis[x][y]||temp.val+1>=fire[x][y]||a[x][y]=='#') continue;
// p.push({x,y,temp.val+1});
// }
// }
// return -1;
//}
//一维哈希
//int n;
//string s;
//int bas=131;
//typedef unsigned long long ull;
//const ull mod1=100001651;
//ull a[100010];
//ull Hash(string s){
// ll ans=0;
// for(int i=0;i<s.size();i++){
// ans*=bas;
// ans+=int(s[i]);
// ans%=mod1;
// }
// return ans;
//}
//二维哈希
//using lom=unsigned long long ;
//const lom bas1=131,bas2=233;
//const int M=505;
//int n,m;
//char a[M][M];
//lom _hash[M][M];
//lom p1[M],p2[M];
//
//
//void init(){
// p1[0]=1;
// p2[0]=1;
// for(int i=1;i<=505;i++){
// p1[i]=p1[i-1]*bas1;
// p2[i]=p2[i-1]*bas2;
//
// }
//}
//void Hash(){
// _hash[0][0]=_hash[0][1]=_hash[1][0]=0;
// for(int i=1;i<=n;i++){ //前缀和
// for(int j=1;j<=m;j++){
// _hash[i][j]=_hash[i][j-1]*bas1+a[i][j]-'a';
// }
// }
// for(int i=1;i<=n;i++){ //二维前缀和
// for(int j=1;j<=m;j++){
// _hash[i][j]+=_hash[i-1][j]*bas2;
// }
// }
//
//}
int n,m;
int a[100010];
int main(){
while(cin>>n>>m){
for(int i=1;i<=m;i++){
cin>>a[i];
}
int ans=1000000;
int p;
for(int i=1;i<=n;i++){
p=-1;
for(int j=1;j<=m;j++){
if(a[j]>i){
p=max(p,min(a[j]-i,n+i-a[j]));
}
else {
p=max(p,min(i-a[j],a[j]+n-i));
}
}
ans=min(p,ans);
}
cout<<ans<<endl;
}
}