BNUOJ 34982 Beautiful Garden

Beautiful Garden

Time Limit: 15000ms
Case Time Limit: 8000ms
Memory Limit: 65536KB
64-bit integer IO format: %lld      Java class name: Main
There are n trees planted in lxhgww's garden. You can assume that these trees are planted along the X-axis, and the coordinate of ith tree is xi.
But in recent days, lxhgww wants to move some of the trees to make them look more beautiful. lxhgww will recognize the trees as beautiful if and only if the distance between any adjacent trees is the same.
Now, lxhgww wants to know what is the minimum number of trees he need to move.
Just to be clear, after moving, there should still be n trees in the X-axis.
 
 

Input

The first line of the input contains a single integer T, which is the number of test cases.
For each case,
  • The first line contains an integers number n (1 ≤ n ≤ 40), representing the number of trees lxhgww planted;
  • The second line contains n integers numbers, the ith number represents xi. (-1000000000 ≤ xi ≤ 1000000000)
 

Output

For each case, first output the case number as "Case #x: ", and x is the case number. Then output a single number, representing the minimum number of trees lxhgww needs to move.

 

Sample Input

1
4
1 3 6 7

Sample Output

Case #1: 1

Source

 
解题:枚举公差,以任意两点的差作为公差,较小的作为首项,算出后面的n-1个元素,看看这n-1个元素有多少个在原来的数列里面
 
那么剩下的即为要移动的 
 
然后 在首项前面新增一个替代当前首项,最后面的删除一个 看看有多少个在原数列。。。
 
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 50;
 4 int d[maxn],n;
 5 map<int,int>mp;
 6 int main() {
 7     int kase,m;
 8     scanf("%d",&kase);
 9     for(int cs = 1; cs <= kase; ++cs) {
10         scanf("%d",&n);
11         mp.clear();
12         for(int i = 0; i < n; ++i) {
13             scanf("%d",d+i);
14             mp[d[i]]++;
15         }
16         if(n <= 2) {
17             printf("Case #%d: %d\n",cs,0);
18             continue;
19         }
20         sort(d,d+n);
21         int ret = 50;
22         for(int i = 0; i < n; ++i) {
23             for(int j = i + 1; j < n; ++j) {
24                 int ds = d[j] - d[i],cnt = n-1;
25                 if(!ds) {
26                     ret = min(ret,n - mp[d[j]]);
27                     continue;
28                 }
29                 int now = d[i];
30                 for(int k = 1; k < n; ++k) {
31                     now += ds;
32                     if(mp[now]) cnt--;
33                 }
34                 int pre = d[i];
35                 ret = min(ret,cnt);
36                 for(int k = 1; k < n; ++k) {
37                     pre -= ds;
38                     cnt += (mp[now]?1:0) - (mp[pre]?1:0);
39                     now -= ds;
40                     ret = min(ret,cnt);
41                 }
42             }
43         }
44         printf("Case #%d: %d\n",cs,ret);
45     }
46     return 0;
47 }
View Code

 

posted @ 2015-07-24 15:52  狂徒归来  阅读(231)  评论(0编辑  收藏  举报