[51nod 1056]最长等差数列V2
来源:互联网 发布:2006 mac mini 接口 编辑:程序博客网 时间:2024/05/12 01:07
题目描述
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1056
随机大法
方法很暴力。
随机一个中间项,然后枚举公差,之后左右拓展,能否拓展用hash判。
随机个一定次数即可。
也可以枚举首项,那么可以加玄学优化就是可行性优化。
我比较水枚举了所有首项。
#include<cstdio>#include<algorithm>#define fo(i,a,b) for(i=a;i<=b;i++)using namespace std;typedef long long ll;const int maxn=50000+10,maxd=10000000;struct dong{ int x,wz;} b[maxn];int a[maxn],ha[10000000+10];int i,j,k,l,r,n,m,d,ca,ans,mx,mi,num;ll t;bool cmp(dong a,dong b){ return a.x<b.x||a.x==b.x&&a.wz<b.wz;}int hash(int x){ int k=x%maxd; if (!k) k=maxd; while (ha[k]!=0&&ha[k]!=x) k=k%maxd+1; return k;}int find(int x){ if (ha[hash(x)]==x) return 1;else return 0;}int main(){ //freopen("data.txt","r",stdin); scanf("%d",&n); mi=1e9; fo(i,1,n) scanf("%d",&a[i]),mx=max(mx,a[i]),mi=min(mi,a[i]); sort(a+1,a+n+1); a[0]=a[1]-1; l=0; fo(i,1,n){ if (a[i]==a[i-1]) l++; else{ num=max(num,l); l=1; } } ans=199; num=max(num,l); if (num>ans) ans=num; fo(i,1,n) ha[hash(a[i])]=a[i]; /*fo(i,1,n){ b[i].x=a[i]; b[i].wz=i; }*/ //sort(b+1,b+n+1,cmp); fo(i,1,n) fo(j,i+1,n){ d=a[j]-a[i]; //if (d<0) continue; t=(ll)a[i]+(ll)d*ans; if (t<mi||t>mx) continue; k=a[j];l=2; while (1){ if (!find(k+d)) break; l++; k+=d; } if (l>ans) ans=l; num=max(num,l); } if (ans<200) printf("No Solution\n");else printf("%d\n",ans);}
0 0
- [51nod 1056]最长等差数列V2
- 51nod 1056 最长等差数列 V2
- 51nod 1056 最长等差数列 V2
- 51 nod 1056 最长等差数列 V2
- 【51nod 1055 & 1056】最长等差数列及V2 题解
- 1056 最长等差数列 V2
- 51nod 1055:最长等差数列
- 51nod-1055 最长等差数列
- 51Nod-1055-最长等差数列
- 51nod 1055 最长等差数列
- 51nod 1055 最长等差数列
- 51nod 1055 最长等差数列
- 51nod-1055-最长等差数列
- 51nod 1055 最长等差数列
- 51nod 1055 最长等差数列
- 51Nod 1055 最长等差数列
- 51Nod-1054-最长等差数列
- 51nod 1055最长等差数列 dp
- File.IO流,异常总结
- DECODE AND NULL
- 直接插入排序算法
- 顺序表应用5:有序顺序表归并
- CentOS7.3定制Gnome3外观
- [51nod 1056]最长等差数列V2
- Linux 操作知识
- 30 Python 深拷贝和浅拷贝
- 集合框架_Map集合的遍历之键值对对象找键和值
- Python join()函数
- HTTPS和HTTP的区别
- 顺序表应用6:有序顺序表查询
- Python SQLite基本使用
- Apache common Csv读写文件