暴力枚举-规模估算

Power Sequence - CodeForces - 1397B

我的代码:

#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<cmath>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

//暴力枚举-规模估算
const int maxn = 100000 + 100;
int aim[maxn];
int lenth;

int main(){
    scanf("%d",&lenth);
    for(int i=0;i<lenth;i++){
        scanf("%d",&aim[i]);
    }
    sort(aim,aim+lenth);//注意先排序
    //开始枚举
    ll ans = 1e14;
    for(ll i=1;i<=1e5;i++){
        ll cur = 1;
        ll sum = 0;
        int k = 0;
        while(k<lenth && sum<ans){
            sum += fabs(aim[k]-cur);
            cur *= i;
            k++;
        }
        if(k==lenth){
            ans = min(ans,sum);
        }
    }
    cout<<ans<<endl;
    return 0;
}

其中1e14 与 1e5 是估算的最大的可能的结果以及最大的大致可能的公比

OK

posted @ 2020-09-29 15:45  SavenNeer  阅读(109)  评论(0编辑  收藏  举报