题解 | #年轮广场#
年轮广场
https://ac.nowcoder.com/acm/problem/13583
本题只需要了解循环数组怎么确定最小距离
#include <bits/stdc++.h>
using namespace std;
/*
暴力求解,遍历每一个位置,选择到所有人距离最大值最小的位置
*/
const int MAXN = 1000 + 5;
int arr[MAXN];
const int INF = 0x7FFFFFFF;
int main() {
int n, m;
while (cin >> n >> m) {
for (int i = 0; i < m; ++i) {
cin >> arr[i];
}
int minNum = INF;
for (int i = 1; i <= n; ++i) {
int maxNum = 0;
for (int j = 0; j < m; ++j) {
// 唯一难点:如何表示两点间的距离
int dist = abs(arr[j] - i);
dist = min(n - dist, dist);
maxNum = max(maxNum, dist);
}
minNum = min(minNum, maxNum);
}
cout << minNum << endl;
}
return 0;
}