bnu 34982 Beautiful Garden

Beautiful Garden

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

2014 ACM-ICPC Beijing Invitational
Programming Contest

题解及代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
map<long long,int>Map;

int main () {

    int cas,n;
    long long s[45];
    scanf("%d",&cas);
    for(int ca=1;ca<=cas;ca++)
    {
        scanf("%d",&n);
        Map.clear();
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&s[i]);
            if(Map.find(s[i])==Map.end()) Map[s[i]]=1;
            else Map[s[i]]++;
        }
        if(n==2)
        {
            printf("Case #%d: 0\n",ca);
            continue;
        }

        int ans=50;
        for(int i=1;i<=n;i++)
        {
            ans=min(ans,n-Map[s[i]]);
            for(int j=i+1;j<=n;j++)
            {
                long long dis=(s[i]>s[j])?s[i]-s[j]:s[j]-s[i];
                if(dis==0) continue;
                for(int k=0;k<=n-2;k++)
                {
                    if(dis%(k+1)) continue;
                    long long  di=dis/(k+1);
                    long long c=min(s[i],s[j]);
                    int cnt=0;
                    for(int x=0;x<=k+1;x++)
                    {
                        if(Map.find(c)!=Map.end()) cnt++;
                        c+=di;
                    }
                    ans=min(ans,n-cnt);
                }
            }
        }
        printf("Case #%d: %d\n",ca,ans);
    }
    return 0;
}
/*
这道题,因为n比较小而且至少有两棵树是不用动的,我们直接暴力枚举起点以及其后另外一点,
然后枚举两棵树之间树的数目,算好间距,模拟即可。

比赛的时候还在纠结,如果选定的起点在最优的情况下不是起点怎么办?后来发现自己多虑了。
假设我们选定的起点是i,另外一点是j(j>i),如果存在上述最优的情况起点在i左侧假设为k(k<i),
那么我们在进行暴力时,以k为起点,j为另一点的情况,已经计算过这种情况了。
所以这样讲,那我们每次枚举i的时候就不需要考虑左侧的树了,每次都是把左侧的树搬到右边来。
还有就是这道题一个比较坑的点:就是一个点上可以种很多树,稍微处理一下就可以了。

转载请注明出处,谢谢。
*/

bnu 34982 Beautiful Garden,布布扣,bubuko.com

时间: 2024-10-16 06:12:15

bnu 34982 Beautiful Garden的相关文章

bnu 34982 Beautiful Garden(暴力)

题目链接:bnu 34982 Beautiful Garden 题目大意:给定一个长度为n的序列,问说最少移动多少点,使得序列成等差序列,点的位置能够为小数. 解题思路:算是纯暴力吧.枚举等差的起始和中间一点,由于要求定中间一点的位置.所以这一步是o(n3);然后用o(n)的算法确定说须要移动几个来保证序列等差. #include <cstdio> #include <cstring> #include <vector> #include <algorithm&g

BNUOJ 34982 Beautiful Garden

BNUOJ 34982 Beautiful Garden 题目地址:BNUOJ 34982 题意: 看错题意纠结了好久... 在坐标轴上有一些树,现在要重新排列这些树,使得相邻的树之间间距相等. 刚开始瞄了一眼以为是求最短的移动距离...后来发现是求最少去移动的树的数量. 分析: 刚开始想错了,以为任意取两棵树,作为相邻的两棵树就行了,吃了好多个wa后,发现这个有问题,因为考虑里面三棵树为最终序列中的三个,那么就有可能判断不出来. 于是想了新的方法,枚举两棵树后,再枚举中间有几棵树,在两棵树中间

北京邀请赛 B. Beautiful Garden

题意:给你坐标和n个点,求最少移动的点使得n个点成等差数列 思路:既然要成等差数列,那么最起码有两个点是不动的,然后枚举这两个点中间的点的个数,最近水的要死,看了队友的代码做的 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cstdlib> #include <cmath> using namespace

2014 SummerTrain Beautiful Garden

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

2014 BNU 邀请赛B题(枚举)

Beautiful Garden 题意:x轴上放了一些树,现在要移动一些树使得所有树都等间距,问最少要移动多少棵 思路:枚举,枚举第一棵树,和另一棵树,以及中间有多少树,这样就能知道等差数列的首项和公差,然后再循环一边计算出答案,保存最小值 代码: #include <stdio.h> #include <string.h> #include <algorithm> #include <math.h> using namespace std; #define

hdu 5977 Garden of Eden

Garden of Eden Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 581    Accepted Submission(s): 164 Problem Description When God made the first man, he put him on a beautiful garden, the Garden

hdu-5977 Garden of Eden(树分治)

题目链接: Garden of Eden Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 210    Accepted Submission(s): 75 Problem Description When God made the first man, he put him on a beautiful garden, the G

2014北京邀请赛(部分题解)

马上要去比赛了. 今天做了一下2014北京邀请赛,出了两道题目,感觉很水啊... 首先H题: H. Happy Reversal Time Limit: 1000ms Case Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class name: Main Submit Status PID: 34988 Font Size:  +   - Elfness is studying

The best and the worst

原文 Joe Sanders has the most beautiful garden in our town. Nearly everybody enters for "The Nicest Garden Competition" each year, but Joe wins every time. Bill Frith's garden is larger than Joe's. Bill works harder than Joe and grows more flowers