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个数使得它成为等差数列。
比赛的时候就一直卡这题,应该是姿势不对,那时候的做法的枚举任意两个数,以他们的差值作为等差数列的d
 
正解是看kuangbin写的,枚举任意两个数,较小的数作为等差数列的首项,然后枚举 x[i],x[j] 所可能构成的 d ,
然后再查一下有多少个x[k]不属于这个等差数列(就是 N-属于的个数)。
 
 
#include <iostream>
#include <cstdio>
#include <map>
#include <algorithm>
#include <cstring> using namespace std;
typedef long long LL;
const int N= ;
int n;
LL x[N];
map<LL,int>mp;
LL gcd(LL a,LL b){return b==?a:gcd(b,a%b);} void run()
{
scanf("%d",&n);
for(int i=;i<n;++i){
scanf("%lld",&x[i]);
if(mp.find( x[i] ) == mp.end() ) mp[x[i]]=;
else mp[x[i]]++;
}
if( n <= ){ puts(""); return ; }
int ans=n-;
for(int i=;i<n;++i){
for(int j=i+;j<n;++j){
LL d=abs(x[j]-x[i]);
if(!d){
ans=min(ans,n-mp[x[j]]);
continue;
}
for(int k=;k<=n-;++k){
LL g=gcd((LL)k,d);
LL c=min(x[i],x[j]);
LL dis=k/g;
LL d2=d/g;
int cnt=;
for(int z=; z < n; z += dis){ //dis~~
if(mp.find(c) != mp.end()) cnt++;
c += d2;
}
ans = min(ans,n-cnt);
}
}
}
printf("%d\n",ans);
} int main()
{
//freopen("in.txt","r",stdin);
int cas=,_;
scanf("%d",&_);
while(_--){
mp.clear();
printf("Case #%d: ",cas++);
run();
}
return ;
}
 

2014 SummerTrain Beautiful Garden的更多相关文章

  1. bnu 34982 Beautiful Garden(暴力)

    题目链接:bnu 34982 Beautiful Garden 题目大意:给定一个长度为n的序列,问说最少移动多少点,使得序列成等差序列,点的位置能够为小数. 解题思路:算是纯暴力吧.枚举等差的起始和 ...

  2. 牛客多校第四场 F Beautiful Garden

    链接:https://www.nowcoder.com/acm/contest/142/F来源:牛客网 题目描述 There's a beautiful garden whose size is n ...

  3. BNUOJ 34982 Beautiful Garden

    BNUOJ 34982 Beautiful Garden 题目地址:BNUOJ 34982 题意:  看错题意纠结了好久... 在坐标轴上有一些树,如今要又一次排列这些树,使得相邻的树之间间距相等.  ...

  4. (第四场)F Beautiful Garden

    题目: F Beautiful Garden 题目描述 There's a beautiful garden whose size is n x m in Chiaki's house. The ga ...

  5. ACM程序设计选修课——1057: Beautiful Garden(模拟+耐心调试)

    1057: Beautiful Garden Time Limit: 5 Sec  Memory Limit: 128 MB Submit: 25  Solved: 12 [Submit][Statu ...

  6. 牛客网暑期ACM多校训练营(第四场) F Beautiful Garden

    链接: https://www.nowcoder.com/acm/contest/142/F 题意: n x m的矩形,选个p x q的矩形去掉,两个矩形中⼼重合,去掉后的矩形上下左右对称 求(p, ...

  7. 北京邀请赛 B. Beautiful Garden

    题意:给你坐标和n个点,求最少移动的点使得n个点成等差数列 思路:既然要成等差数列,那么最起码有两个点是不动的,然后枚举这两个点中间的点的个数,近期水的要死,看了队友的代码做的 #include &l ...

  8. 2014 ACM/ICPC 北京邀请赛 部分 题解

    题目链接:http://acm.bnu.edu.cn/bnuoj/problem.php?search=2014+ACM-ICPC+Beijing+Invitational+Programming+C ...

  9. HDU5977 Garden of Eden(树的点分治)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5977 Description When God made the first man, he ...

随机推荐

  1. Linux性能优化从入门到实战:11 内存篇:内存泄漏的发现与定位

      用户空间内存包括多个不同的内存段,比如只读段.数据段.堆.栈以及文件映射段等.但会发生内存泄漏的内存段,只有堆和文件映射段中的共享内存.   内存泄漏的危害非常大,这些忘记释放的内存,不仅应用程序 ...

  2. 牛客ACM赛 B [小a的旅行计划 ]

    链接 B 小a的旅行计划 把\(n\)个数中选任意数分成\(a,b\)两个集合,集合无区别,要求不包含且有交,求方案数.\(n\leq 10^{13}\) 首先讨论\(a,b\)并集是否为全集: 若是 ...

  3. ORM大结局

    1.添加记录: # 一对多的添加方式: #pub_obj=Publish.objects.filter(name="橙子出版社").first() # book=Book.obje ...

  4. JDBC连接Hive数据库

    一.依赖 pom <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncodi ...

  5. hdu 6205: card card card【输入挂】

    题目链接 感谢 http://blog.csdn.net/txgang/article/details/77568491 以下供参考 getchar读入法 2683MS FastIO法 MX=1e2 ...

  6. hdu 6045: Is Derek lying? (2017 多校第二场 1001)【找规律】

    题目链接 可以暴力找一下规律 比如,假设N=7,两人有5题相同,2题不同,枚举X=0->15时,Y的"Not lying"的取值范围从而找出规律 #include<bi ...

  7. fiddler常见问题

    捕获https: tools>options https>decrypt https traffic :安装证书捕获客户端请求: tools>options connections& ...

  8. 怎么实现web端上传超大文件

    1.介绍enctype enctype 属性规定发送到服务器之前应该如何对表单数据进行编码. enctype作用是告知服务器请求正文的MIME类型(请求消息头content-type的作用一样) 1. ...

  9. Codeforces 845D - Two TVs(贪心)

    原题链接:http://codeforces.com/problemset/problem/845/D 题意:一个人在驾照考试中,路边有“限速XX”.“没有限速”.“可以超车”.“不能超车”路牌, 以 ...

  10. Curl命令、Elinks命令、lynx命令、Wget命令、lftp命令

    一.Curl命令 语法 curl(选项)(参数) 选项 -a/--append 上传文件时,附加到目标文件 -A/--user-agent <string> 设置用户代理发送给服务器 -a ...