赞
踩
https://codeforces.com/contest/1931/problem/C
这是一个算法问题,需要找到一种操作方法,使得将数组中的所有元素变为相等所需的最小代价。对于每个测试用例,我们需要输出使数组中所有元素相等所需的最小代价。
首先,我们可以遍历数组,找到数组中的最小值和最大值。然后我们尝试将其他元素都变为最小值或最大值,计算出每种情况下的代价,最后取其中的最小值作为最少需要花费的代价。
以下是具体的步骤:
- int n;
- int a[N];
- void work()
- {
- cin>>n;
- for(int i=1;i<=n;i++)
- cin>>a[i];
-
- int l=1,r=n;
- while(a[l]==a[l+1])l++;
- while(a[r]==a[r-1])r--;
-
- if(l>=r)cout<<0<<endl;
- else if(a[1]!=a[n])cout<<min(n-l,r-1)<<endl;
- else cout<<(r-1)-(l+1)+1<<endl;
- }
-
- signed main()

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。