• 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
  • 相关阅读:
    hdu.1226.超级密码(bfs)
    zstu.2512. Moving Tables(贪心)
    zoj.3868.GCD Expectation(数学推导>>容斥原理)
    acdream.LCM Challenge(数学推导)
    acdream.Triangles(数学推导)
    acdream.A Very Easy Triangle Counting Game(数学推导)
    acdream.Bet(数学推导)
    acdream.郭式树(数学推导)
    EntityFramework(转自wiki)
    石墨烯(转自wiki)
  • 原文地址:https://www.cnblogs.com/TWS-YIFEI/p/10301891.html
Copyright © 2020-2023  润新知