• E. Arithmetic Operations 题解(分类讨论)


    题目链接

    题目思路

    看一眼数据范围和时间限制,很容易想到复杂度为\(O(n\sqrt n)\)

    有点不太会写,要采用按照公差大小分类讨论

    假如公差小于\(\sqrt {10^5}\)

    那么直接暴力计算\(a[i]=a[i]-(i-1)*d\) 判断哪个数出现的次数最多即可

    假如公差大于\(\sqrt{10^5}\)

    那么以\(a[i]\)为起点,长度最多\(\sqrt{10^5}\)

    代码

    #include<bits/stdc++.h>
    #define fi first
    #define se second
    #define debug cout<<"I AM HERE"<<endl;
    using namespace std;
    typedef long long ll;
    const int maxn=1e8+5,inf=0x3f3f3f3f,mod=1e9+7;
    const double eps=1e-6;
    int n;
    int a[100000+5];
    int c=320,yi=5e7;
    int cnt[maxn];
    signed main(){
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
        }
        int ma=0;
        for(int i=-c;i<=c;i++){
            for(int j=1;j<=n;j++){
                cnt[a[j]+(j-1)*i+yi]++;
                ma=max(ma,cnt[a[j]+(j-1)*i+yi]);
            }
            for(int j=1;j<=n;j++){
                cnt[a[j]+(j-1)*i+yi]=0;
            }
        }
        for(int i=1;i<=n;i++){
            for(int j=i+1;j<=min(n,i+c);j++){
                if((a[j]-a[i])%(j-i)!=0) continue;
                cnt[(a[j]-a[i])/(j-i)+yi]++;
                ma=max(ma,cnt[(a[j]-a[i])/(j-i)+yi]+1);
            }
             for(int j=i+1;j<=min(n,i+c);j++){
                if((a[j]-a[i])%(j-i)!=0) continue;
                cnt[(a[j]-a[i])/(j-i)+yi]=0;
            }
        }
        printf("%d\n",n-ma);
        return 0;
    }
    
    
    
  • 相关阅读:
    如何注册一个ens域名
    storj
    Polygon
    拜占庭容错
    10秒钟完成metamask上任意公链主网添加
    Logistic Regression
    Normal Equations
    神经网络学习笔记 lecture3:The backpropagation learning proccedure
    Three Types of Learning
    Exercise: Logistic Regression and Newton's Method
  • 原文地址:https://www.cnblogs.com/hunxuewangzi/p/16033605.html
Copyright © 2020-2023  润新知