• 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
    ",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
    ",cs,ret);
    45     }
    46     return 0;
    47 }
    View Code
  • 相关阅读:
    3G来临,程序员你准备好了吗?
    如何从开发人员走向架构师
    中国营销六大怪
    什么是3G
    未来五年中国高端软件人才缺口20万
    项目需求分析的20条法则
    让好的销售习惯提升你的业绩
    有关精通时间管理的最佳理念
    李嘉诚:让员工忠诚的简单办法
    手机软件开发人才严重短缺
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4673728.html
Copyright © 2020-2023  润新知