codeforce978C-Almost Arithmetic Progression+暴力,枚举前两个数字的情况

传送门:http://codeforces.com/contest/978/problem/D

题意:求变为等差数列,最小要改动的数字个数;

思路:暴力,这道题只用枚举前面两个数字的情况就ok,反思自己在看到这道题的时候各种找规律。

复杂的是3*3*n,我看到群里说的复杂度,内心才恍然大悟。

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

#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back

typedef long long ll;

/*-----------------show time----------------*/

const int maxn = 1e5+9;
int n;
ll a[maxn];
ll abb(ll x)
{
    return x>0?x:-x;
}
int main(){
    scanf("%d",&n);
    for(int i=1; i<=n; ++i)
    {
        scanf("%lld", &a[i]);
    }
    int ans = n+1;
    for(int u = -1; u<=1; u++)
    {
        for(int v=-1; v<=1; v++)
        {
            ll tmp = a[2]+v - (a[1] + u);
            int flag = 1,cnt = 0;
            if(u!=0)cnt++;
            if(v!=0)cnt++;
            for(int i=3; i<=n; i++)
            {
                if(abb(a[i] - (a[1]+u + (i-1)*tmp))>1)
                {
                    flag = 0;
                    break;
                }
                else if(a[i] - (a[1]+u + (i-1)*tmp)!=0){
                    cnt++;
                }
            }
            if(flag)ans = min(ans,cnt);
        }
    }
    if(ans==n+1)puts("-1");
    else printf("%d\n",ans);
    return 0;
}

 

posted @ 2018-05-13 22:54  ckxkexing  阅读(452)  评论(0编辑  收藏  举报