zoukankan      html  css  js  c++  java
  • Codeforces Round #533 (Div. 2) 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.

    题意:给你一个含有N个数的数组,让你找一个数x,使x的数组求值消耗的能量尽量小。

    数组对于x的求值是把数组的每一个数削成| x-a[i] | <=1 ,并且a[i]数值变化1就要消耗1能量。

    思路:

    数据范围很小,直接暴力从1到max ( a[i] ) +2 试一试,找出那个最小的就行了,

    细节看代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <vector>
    #define sz(a) int(a.size())
    #define all(a) a.begin(), a.end()
    #define rep(i,x,n) for(int i=x;i<n;i++)
    #define repd(i,x,n) for(int i=x;i<=n;i++)
    #define pii pair<int,int>
    #define pll pair<long long ,long long>
    #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
    #define MS0(X) memset((X), 0, sizeof((X)))
    #define MSC0(X) memset((X), '', sizeof((X)))
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define eps 1e-6
    #define gg(x) getInt(&x)
    #define db(x) cout<<"==  "<<x<<"  =="<<endl;
    using namespace std;
    typedef long long ll;
    ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
    ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
    inline void getInt(int* p);
    const int maxn=1000010;
    const int inf=0x3f3f3f3f;
    /*** TEMPLATE CODE * * STARTS HERE ***/
    int n;
    int a[maxn];
    int ans=inf;
    int cnt=inf;
    int tt;
    int tcnt;
    int main()
    {
        gbtb;
        cin>>n;
        repd(i,1,n)
        {
            cin>>a[i];
        }
        repd(i,1,2000)
        {
            tt=i;
            tcnt=0;
            repd(j,1,n)
            {
                if(abs(tt-a[j])<=1)
                {
    
                }else
                {
                    tcnt+=abs(tt-a[j])-1;
                }
            }
            if(tcnt<cnt)
            {
                cnt=tcnt;
                ans=tt;
            }
        }
        cout<<ans<<" "<<cnt;
        return 0;
    }
    
    inline void getInt(int* p) {
        char ch;
        do {
            ch = getchar();
        } while (ch == ' ' || ch == '
    ');
        if (ch == '-') {
            *p = -(getchar() - '0');
            while ((ch = getchar()) >= '0' && ch <= '9') {
                *p = *p * 10 - ch + '0';
            }
        }
        else {
            *p = ch - '0';
            while ((ch = getchar()) >= '0' && ch <= '9') {
                *p = *p * 10 + ch - '0';
            }
        }
    }

    本博客为本人原创,如需转载,请必须声明博客的源地址。 本人博客地址为:www.cnblogs.com/qieqiemin/ 希望所写的文章对您有帮助。
  • 相关阅读:
    ASP.NET教程4
    ASP.NET教程11
    TreeView Demo
    System.Net.Dns.GetHostByAddress(string) 已经过时
    会员注册实例
    ASP.NET教程2
    多表关联与表值函数
    ASP.NET教程6
    BusinessFrameWork
    ASP.NET教程8
  • 原文地址:https://www.cnblogs.com/qieqiemin/p/10296542.html
Copyright © 2011-2022 走看看