51nod1056 最长等差数列 V2

1056 最长等差数列 V2
基准时间限制:8 秒 空间限制:131072 KB
N个不同的正整数,从中选出一些数组成等差数列。 
 
例如:1 3 5 6 8 9 10 12 13 14
等差子数列包括(仅包括两项的不列举)
1 3 5
1 5 9 13
3 6 9 12
3 8 13
5 9 13
6 8 10 12 14
 
其中6 8 10 12 14最长,长度为5。
 
现在给出N个数,你来从中找出一个长度 >= 200 的等差数列,如果没有,输出No Solution,如果存在多个,输出最长的那个的长度。
 
Input
第1行:N,N为正整数的数量(1000 <= N <= 50000)。
第2 - N+1行:N个正整数。(2<= A[i] <= 10^9)
(注,真实数据中N >= 1000,输入范例并不符合这个条件,只是一个输入格式的描述)
Output
找出一个长度 >= 200 的等差数列,如果没有,输出No Solution,如果存在多个,输出最长的那个的长度。
Input示例
10
1
3
5
6
8
9
10
12
13
14
Output示例
No Solution
玄学优化题,,
 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<cstdlib>
 7 #include<vector>
 8 using namespace std;
 9 typedef long long ll;
10 typedef long double ld;
11 typedef pair<int,int> pr;
12 const double pi=acos(-1);
13 #define rep(i,a,n) for(int i=a;i<=n;i++)
14 #define per(i,n,a) for(int i=n;i>=a;i--)
15 #define Rep(i,u) for(int i=head[u];i;i=Next[i])
16 #define clr(a) memset(a,0,sizeof(a))
17 #define pb push_back
18 #define mp make_pair
19 #define fi first
20 #define sc second
21 #define pq priority_queue
22 #define pqb priority_queue <int, vector<int>, less<int> >
23 #define pqs priority_queue <int, vector<int>, greater<int> >
24 #define vec vector
25 ld eps=1e-9;
26 ll pp=1000000007;
27 ll mo(ll a,ll pp){if(a>=0 && a<pp)return a;a%=pp;if(a<0)a+=pp;return a;}
28 ll powmod(ll a,ll b,ll pp){ll ans=1;for(;b;b>>=1,a=mo(a*a,pp))if(b&1)ans=mo(ans*a,pp);return ans;}
29 void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
30 //void add(int x,int y,int z){ v[++e]=y; next[e]=head[x]; head[x]=e; cost[e]=z; }
31 int dx[5]={0,-1,1,0,0},dy[5]={0,0,0,-1,1};
32 ll read(){ ll ans=0; char last=' ',ch=getchar();
33 while(ch<'0' || ch>'9')last=ch,ch=getchar();
34 while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
35 if(last=='-')ans=-ans; return ans;
36 }
37 const unsigned int p=1795876373;
38 const int p_=7;
39 #define hash(i) (((unsigned int)i*p)>>p_)
40 int a[50005],ans=199;
41 bool f[40000000];
42 void work(int x,int d){
43     int now=1;
44     while (f[hash((1LL*x+1LL*now*d))]){
45         now++;
46     }
47     ans=max(now,ans);
48 }
49 int main(){
50     int n=read(),d,Max=0;
51     for (int i=1;i<=n;i++) a[i]=read(),f[hash(a[i])]=1,Max=max(Max,a[i]);
52     sort(a+1,a+n+1);
53     for (int i=1;i<n;i++)
54         for (int j=i+1;j<=n;j++){
55             d=a[j]-a[i];
56             if ((1LL*a[i]+1LL*ans*d)>1LL*Max) break;
57             if (f[hash((1LL*a[i]+1LL*ans*d))]) {
58                 work(a[i],d);
59             }
60         }
61     if (ans>=200) printf("%d",ans);
62     else puts("No Solution");
63     return 0;
64 } 
View Code
原文地址:https://www.cnblogs.com/SXia/p/7592029.html