zoukankan      html  css  js  c++  java
  • Codeforces-Salem and Sticks(枚举+思维)

    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 |a−b||a−b|, where |x||x| means the absolute value of xx.

    A stick length aiai is called almost good for some integer tt if |ai−t|≤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 (1≤n≤10001≤n≤1000) — the number of sticks.

    The second line contains nn integers aiai (1≤ai≤1001≤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

    3
    10 1 4
    

    Output

    3 7
    

    Input

    5
    1 1 2 2 3
    

    Output

    2 0
    

    Note

    In the first example, we can change 11 into 22 and 1010 into 44 with cost |1−2|+|10−4|=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-100的数,因为a[i]在1-100的范围内

    代码:

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    
    using namespace std;
    
    int a[1005];
    int main()
    {
    	int n;
    	cin>>n;
    	for(int t=0;t<n;t++)
    	{
    		scanf("%d",&a[t]);
    	}
    	int maxn=9999999;
    	int k;
    	for(int t=1;t<=100;t++)
    	{
    		int sum=0;
    		for(int j=0;j<n;j++)
    		{
    			
    		     sum+=min(fabs(a[j]-t+1),min(fabs(a[j]-t),fabs(a[j]-t-1)));
    		}
    		if(sum<maxn)
    		{
    			maxn=sum;
    			k=t;
    		}
    	}
    	cout<<k<<" "<<maxn<<endl;
    	return 0;
    }
  • 相关阅读:
    解决WordPress不能发邮件,WordPress 无法发送邮件
    WordPress 显示 Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 2097160 bytes)解决办法
    怎么优雅的取消重复请求ajax
    手拉手搭建一个脚手架
    数据库隔离级别RC与RR区别——MVCC、ReadView
    整理一下下一步的计划
    减肥
    EBS: Shift + F6: 当复制上行记录
    Oracle 表值函数之多表关联使用
    EBS: 序号授权 GRANT
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/10781838.html
Copyright © 2011-2022 走看看