Beautiful campus
Time Limit: 1000 MS | memory limit: 65535 KB
Difficulty: 3
Description
The beautiful campus needs to be maintained by everyone. It is of course the responsibility of South engineering students!
There are n trees in front of the family planning system, arranged in one row in front of the family planning system building, they are on a straight line, the teachers in the department think they want to make them look beautiful, they think that beauty means that the distance between all trees is equal. Now we give the positions of all trees, so that you can find the minimum number of moving trees to make them look beautiful.
Input
Input Multiple groups of data. First, a number of N (1 <= n <= 40) indicates n trees, and then n indicates n numbers, indicates the position of the number XI (-1000000000 ≤ Xi ≤ 1000000000 ).
Output
Output result
Sample Input
4
1 3 6 7
Sample output
1
Source
Yougth
Uploaded
TC _ Yang zhuangliang
Solution: enumerate the adjacent distances ..
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 42;18 int n,d[maxn];19 int main() {20 while(~scanf("%d",&n)){21 for(int i = 1; i <= n; i++)22 scanf("%d",d+i);23 int ans = INF;24 sort(d+1,d+n+1);25 for(int i = 1; i < n; i++){26 int tmp = 0,ds = d[i+1] - d[i];27 for(int j = i - 1; j > 0; j--)28 if(d[i] - d[j] == ds*(j - i)) continue;29 else tmp++;30 for(int j = i + 1; j <= n; j++)31 if(d[j] - d[i+1] == ds*(j - i - 1)) continue;32 else tmp++;33 ans = min(ans,tmp);34 }35 printf("%d\n",ans);36 }37 return 0;38 }
View code
It seems that this is not the case. The data is too weak.
Nyist 1083 beautiful campus