https://codeforces.com/contest/1397/problem/B
Let's call a list of positive integers a0,a1,...,an−1a0,a1,...,an−1 a power sequence if there is a positive integer cc, so that for every 0≤i≤n−10≤i≤n−1 then ai=ciai=ci.
Given a list of nn positive integers a0,a1,...,an−1a0,a1,...,an−1, you are allowed to:
- Reorder the list (i.e. pick a permutation pp of {0,1,...,n−1}{0,1,...,n−1} and change aiai to apiapi), then
- Do the following operation any number of times: pick an index ii and change aiai to ai−1ai−1 or ai+1ai+1 (i.e. increment or decrement aiai by 11) with a cost of 11.
Find the minimum cost to transform a0,a1,...,an−1a0,a1,...,an−1 into a power sequence.
Input
The first line contains an integer nn (3≤n≤1053≤n≤105).
The second line contains nn integers a0,a1,...,an−1a0,a1,...,an−1 (1≤ai≤1091≤ai≤109).
Output
Print the minimum cost to transform a0,a1,...,an−1a0,a1,...,an−1 into a power sequence.
Examples
input
Copy
3 1 3 2
output
Copy
1
input
Copy
3 1000000000 1000000000 1000000000
output
Copy
1999982505
Note
In the first example, we first reorder {1,3,2}{1,3,2} into {1,2,3}{1,2,3}, then increment a2a2 to 44 with cost 11 to get a power sequence {1,2,4}{1,2,4}.
思路:直觉上这题的费用状态是类似一个开口向上的二次函数,感觉是个三分的类型。然后发现这题最多的花费上限的c=1的时候, ans(max)=∑(a[i]−1).因为c范围在1-无穷,根据函数图像随着c变得很大后面的ans肯定大于c=1的函数值。
所以直接枚举c,当出现大于c=1的函数值的时候break;
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5+100;
typedef long long LL;
const LL inf=1e18;
LL a[maxn];
int main(void)
{
cin.tie(0);std::ios::sync_with_stdio(false);
LL n;cin>>n;
for(LL i=0;i<n;i++) cin>>a[i];
sort(a,a+n);
LL ans=inf;
for(LL c=1;c<=1000000; c++)
{
LL cur=1;
LL now=0;
bool flag=1;
for(LL i=0;i<n;i++)
{
if(cur>=1000000000000){flag=0;break;}
now+=fabs(a[i]-cur);
cur*=c;
}
if(flag) ans=min(ans,now);
}
cout<<ans<<endl;
return 0;
}