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

相关推荐

不愿透露姓名的神秘牛友
07-01 17:13
想去,但是听说加班强度实在难崩,所以拒绝了,现在有点心梗对面hr感觉也是实习生,打电话的时候怪紧张的,但是感觉人很好嘞
水中水之下水道的鼠鼠:哥们这不先去体验一下,不行再跑呗,大不了混个实习经历(有更好的转正offer就当我没说)
点赞 评论 收藏
分享
06-07 17:17
嘉兴学院 教师
心爱的idea:你孩
点赞 评论 收藏
分享
后来123321:别着急,我学院本大二,投了1100份,两个面试,其中一个还是我去线下招聘会投的简历,有时候这东西也得看运气
无实习如何秋招上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务