Codeforces Round #666 (Div. 2) ------ Power Sequence

題目

在這裏插入圖片描述

題意:

給定一個長度爲n的序列,能夠進行兩種操做,求使其變爲最佳序列的最小花費
操做一:排序,此操做沒有花費
操做二:對任意一個數無限制的進行加一或者減一操做,每次花費爲1個單元
最佳序列定義:a[i] = c^i,數組下標從0開始,即首項爲1的等比數列ios

題解:

求出c的範圍,暴力枚舉便可。
根據題目給出的數據範圍,可知這個數組的最大和爲1e14,然而等比數列的增加是很是快的,所以很快便會打到上界,因此最大公比爲pow(1e14, 1.0 / n)c++

AC代碼:

#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;
}
相關文章
相關標籤/搜索