题意:就是给你一个数列,让你找出可以组成等差数列的最多元素有多少个
正解:
题解:直接暴力,枚举d,然后二分查找,注意这里要枝剪,减去已经有的最大值就行了
#include <bits/stdc++.h> #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <cmath> //#define double long double #define int long long //#define endl '\n'; using namespace std; const int N=5000+7; const int INF = 0x3f3f3f3f; const int mod=998244353; int a[N]; int32_t main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int n; cin>>n; for(int i=1;i<=n;i++) { cin>>a[i]; } sort(a+1,a+1+n); int ans=2; int sum=0; for(int i=1;i<n-sum;i++) { for(int j=i+1;j<=n-sum;j++) { int d=a[j]-a[i]; if(d==0) continue; int t=2; ans=2; while (binary_search(a+1,a+1+n,a[i]+t*d)) { t++; ans++; } sum=max(ans,sum); } } cout<<sum<<endl; }
标签:const,int,long,暑假,tie,题记,include,define From: https://www.cnblogs.com/whatdo/p/17612557.html