POJ1840 离散化+二分 OR hash_链接法 (简单题)

1 题意

2 分析

Note:代码一的二分,如果用STL的lower_bound(),速度很慢、甚至超时;而二分自己写的话速度就很快。

代码一:离散化+二分 (1000ms+ / 5000ms)

代码二:hash (500ms+ / 5000ms)

代码一:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#define LI(x) x*x*x
using 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*x
using 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值