给定一个长度为n的序列,能够进行两种操做,求使其变为最佳序列的最小花费
操做一:排序,此操做没有花费
操做二:对任意一个数无限制的进行加一或者减一操做,每次花费为1个单元
最佳序列定义:a[i] = c^i,数组下标从0开始,即首项为1的等比数列ios
求出c的范围,暴力枚举便可。
根据题目给出的数据范围,可知这个数组的最大和为1e14,然而等比数列的增加是很是快的,所以很快便会打到上界,因此最大公比为pow(1e14, 1.0 / n)c++
#include<iostream> #include<stdio.h> #include<algorithm> #include<queue> #include<map> #include<string> #include<math.h> using namespace std; #define ll long long const ll inf = 1e15; const int N = 1e5 + 15; int a[N]; int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); int c = pow(inf, 1.0 / n); ll ans = inf; for (int i = 1; i <= c; i++) { ll num = llabs(1 - a[0]); ll q = 1; for (int j = 1; j < n; j++) { q *= i; num += llabs(a[j] - q); if (num > ans) break; } ans = min(ans, num); } cout << ans << endl; return 0; }