POJ1840 离散化+二分 OR hash_链接法 (简单题)
来源:互联网 发布:mysql设置最大连接数 编辑:程序博客网 时间:2024/06/07 13:22
1 题意
2 分析
Note:代码一的二分,如果用STL的lower_bound(),速度很慢、甚至超时;而二分自己写的话速度就很快。
代码一:离散化+二分 (1000ms+ / 5000ms)
代码二:hash (500ms+ / 5000ms)
3
代码一:
#include <iostream>#include <stdio.h>#include <algorithm>#include <string.h>#define LI(x) x*x*xusing namespace std;//-(a1x1+a2x2)=(a3x3+a4x4+a5x5)const int maxn=10010;int res_left[maxn];int number[5];int cur;void Ini(){ cur=0; for(int i=-50;i<=50;i++){ if(i==0) continue; for(int j=-50;j<=50;j++){ if(j==0) continue; res_left[cur++]=-1*(number[0]*LI(i)+number[1]*LI(j)); } } //cout<<"cur: "<<cur<<endl; sort(res_left,res_left+cur);}int Search_binary(int sum){ int left=0,right=cur-1,mid=0,res=0; while(left<=right){ mid=(left+right)>>1; if(res_left[mid]<sum){ left=mid+1;///!!! Wrong:left=mid } else if(sum<res_left[mid]){ right=mid-1;///!!! Wrong: right=mid } else if(sum==res_left[mid]){ res=1; int mid_left=mid-1; int mid_right=mid+1; while(sum==res_left[mid_left]&&mid_left>=0){ res++; mid_left--; } while(sum==res_left[mid_right]&&mid_right<=cur-1){ res++; mid_right++; } break; } } return res;}/*int Search_binary(int sum){ int pos=lower_bound(res_left,res_left+cur,sum)-res_left; int res=0; if(pos<cur&&res_left[pos]==sum) res=1; else res=0; for(int i=pos+1;i<cur;i++){ if(res_left[i]==sum){ res++; } } return res;}*/int main(){ //freopen("out.txt","w",stdout); while(~scanf("%d",&number[0])){ for(int i=1;i<5;i++) scanf("%d",&number[i]); Ini(); int sum=0,res=0; for(int i=-50;i<=50;i++){ if(i==0) continue; for(int j=-50;j<=50;j++){ if(j==0) continue; for(int k=-50;k<=50;k++){ if(k==0) continue; sum=LI(i)*number[2]+LI(j)*number[3]+LI(k)*number[4]; res+=Search_binary(sum); } } } cout<<res<<endl; } return 0;}
代码二:
#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>#define LF(x) x*x*xusing namespace std;const int maxn=1000007; //maxn为散列表hash_[]的长度。 maxn < int数组长度(262144*10),const int MOD=maxn; // MOD除了作为MOD以外,还是hash_head[]的长度。MOD < KEY_max: 18750000,KEY_min: -18750000,< int值(+/- 2147483647)int cur=0;//当前指针int hash_head[MOD];//Hash(KEY) - 对应链表的首指针struct Node{ int key; int next;}hash_[maxn];//下标由cur赋值,逐渐递增int a[5];void Insert_hash(int key){ int temp=(key>0)?key:-key;//因为数组下标不会有负数 int cor_num=(temp)%MOD; hash_[cur].key=key; hash_[cur].next=hash_head[cor_num]; hash_head[cor_num]=cur++;}int Find_hash(int key){ int temp=(key>0)?key:-key; int cor_num=(temp)%MOD; int u=hash_head[cor_num]; int coun=0; while(u!=-1){ if(hash_[u].key==key){ coun++; } u=hash_[u].next; } return coun;}int main(){ while(~scanf("%d",&a[0])){ ///INI cur=0; for(int i=1;i<5;i++) scanf("%d",&a[i]); for(int i=0;i<MOD;i++){ //hash_[i].next=0; //hash_[i].key=0; hash_head[i]=-1; } ///SCANF int key=0; for(int i=-50;i<=50;i++){ if(i==0) continue; for(int j=-50;j<=50;j++){ if(j==0) continue; for(int k=-50;k<=50;k++){ if(k==0) continue; key=(a[0]*LF(i)+a[1]*LF(j)+a[2]*LF(k))*(-1); Insert_hash(key); } } } ///FIND int res=0; for(int i=-50;i<=50;i++){ if(i==0) continue; for(int j=-50;j<=50;j++){ if(j==0) continue; key=(a[3]*LF(i)+a[4]*LF(j)); res+=Find_hash(key); } } cout<<res<<endl; }}
0 0