zoukankan      html  css  js  c++  java
  • codeforces_A. Salem and Sticks_数组/暴力

    A. Salem and Sticks
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Salem gave you nn sticks with integer positive lengths a1,a2,,ana1,a2,…,an.

    For every stick, you can change its length to any other positive integer length (that is, either shrink or stretch it). The cost of changing the stick's length from aa to bb is |ab||a−b|, where |x||x| means the absolute value of xx.

    A stick length aiai is called almost good for some integer tt if |ait|1|ai−t|≤1.

    Salem asks you to change the lengths of some sticks (possibly all or none), such that all sticks' lengths are almost good for some positive integer tt and the total cost of changing is minimum possible. The value of tt is not fixed in advance and you can choose it as any positive integer.

    As an answer, print the value of tt and the minimum cost. If there are multiple optimal choices for tt, print any of them.

    Input

    The first line contains a single integer nn (1n10001≤n≤1000) — the number of sticks.

    The second line contains nn integers aiai (1ai1001≤ai≤100) — the lengths of the sticks.

    Output

    Print the value of tt and the minimum possible cost. If there are multiple optimal choices for tt, print any of them.

    Examples
    input
    Copy
    3
    10 1 4
    
    output
    Copy
    3 7
    
    input
    Copy
    5
    1 1 2 2 3
    
    output
    Copy
    2 0
    
    Note

    In the first example, we can change 11 into 22 and 1010 into 44 with cost |12|+|104|=1+6=7|1−2|+|10−4|=1+6=7 and the resulting lengths [2,4,4][2,4,4]are almost good for t=3t=3.

    In the second example, the sticks lengths are already almost good for t=2t=2, so we don't have to do anything.

     1 #include <bits/stdc++.h>
     2 #include <cstdio>
     3 
     4 using namespace std;
     5 
     6 int n;
     7 int a[1005];
     8 int sum=0;
     9 
    10 int cal(int k){
    11     int res=0;
    12     for(int i=0;i<n;i++){
    13         if(abs(a[i]-k)>1){
    14             res+=(abs(a[i]-k)-1);
    15         }
    16     }
    17     return res;
    18 }
    19 
    20 int main()
    21 {
    22     int minn=99999999;
    23     int maxx=0;
    24     scanf("%d",&n);
    25     for(int i=0;i<n;i++){
    26         scanf("%d",&a[i]);
    27         sum+=a[i];
    28         minn=a[i]<minn?a[i]:minn;
    29         maxx=a[i]>maxx?a[i]:maxx;
    30     }
    31     int r1=1;
    32     int cost=99999999;
    33     int now=0;
    34     for(int i=minn;i<=maxx;i++){
    35         now=cal(i);
    36         if(now<cost){
    37             r1=i;
    38             cost=now;
    39         }
    40     }
    41     printf("%d %d
    ",r1,cost);
    42     /*
    43     int r1=sum/n;
    44     int r2=r1+1;
    45     int _r1=cal(r1,a);
    46     int _r2=cal(r2,a);
    47 
    48     if(_r1<_r2){
    49         printf("%d %d
    ",r1,_r1);
    50     }else{
    51         printf("%d %d
    ",r2,_r2);
    52     }
    53     */
    54 
    55     return 0;
    56 }
    View Code
  • 相关阅读:
    【Tools__技巧】软件技巧整理归纳
    【JS__框架】主页面F5刷新iframe框架页面
    为类型增加选择属性
    单元测试mock当前时间
    silverlight属性改变事件通知
    修改博客园推荐人数增加W效果
    Enum扩展特性,代替中文属性
    Python爬虫-萌妹子图片
    吐槽下魅族
    使用openXML 不用插件导出excel
  • 原文地址:https://www.cnblogs.com/TWS-YIFEI/p/10301891.html
Copyright © 2011-2022 走看看