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

题目

题意:

给定一个长度为n的序列,可以进行两种操作,求使其变为最佳序列的最小花费
操作一:排序,此操作没有花费
操作二:对任意一个数无限制的进行加一或者减一操作,每次花费为1个单元
最佳序列定义:a[i] = c^i,数组下标从0开始,即首项为1的等比数列

题解:

求出c的范围,暴力枚举即可。
根据题目给出的数据范围,可知这个数组的最大和为1e14,然而等比数列的增长是非常快的,因此很快便会打到上界,所以最大公比为pow(1e14, 1.0 / n)

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;
}
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务