Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

第一道Hash·~ 网上学的 贴代码

Posted by cssdnx at 2012-01-02 10:06:45 on Problem 2002
#include "iostream"
#include "stdio.h"
#include "cmath"
#include "cstring"
#include "algorithm"
using namespace std;

struct point
{
    int x,y;
}r[1001];

int hash[40001],next[1001],n;

bool cmp(point a, point b)
{
    if(a.x==b.x)
        return a.y<b.y;
    return a.x<b.x;
}

bool find(int x, int y)
{
    int value,index;
    value = abs(x+y);
    index = hash[value];
    while(index != -1)
    {
        if(r[index].x==x && r[index].y==y)
            return true;
        index = next[index];
    }
    return false;
}

int main()
{
    freopen("111.txt","r",stdin);
    int i,j,ans,a,b,value;

    while(scanf("%d",&n) && n)
    {
        memset(hash,-1,sizeof(hash));
        memset(next,-1,sizeof(next));

        for(i=0; i<n; i++)
            scanf("%d%d",&r[i].x,&r[i].y);

        sort(r,r+n,cmp);

        for(i=0; i<n; i++)
        {
            value=abs(r[i].x+r[i].y);
            next[i]=hash[value];
            hash[value]=i;
        }

        ans=0;
        for(i=0; i<n; i++)
        {
            for(j=i+1; j<n; j++)
            {
                a=r[i].x+r[i].y-r[j].y;
                b=r[i].y+r[j].x-r[i].x;
                if(!find(a,b))
                    continue;
                a=r[j].x+r[i].y-r[j].y;
                b=r[j].y+r[j].x-r[i].x;
                if(find(a,b))
                    ans++;
            }
        }
        printf("%d\n",ans/2);
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator