hdu 5199 Gunner

题意:给了两个集合A,B,然后求集合A中的元素在集合B中出现的次数,当然如果有两个相同的询问,那么后一个询问为0。。有两种解法。

解法一:Hash  因为这是典型的给两个集合,然后直接求一个集合在另外一个集合中是存在。。。所以一直用模素数的方法解决冲突。

解法二:两次二分,然后算中间的差值,那么就是出现的次数,这也是那篇论文里面的解法 二分。。。

题目:

Gunner

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1083    Accepted Submission(s): 477


Problem Description
Long long ago, there is a gunner whose name is Jack. He likes to go hunting very much. One day he go to the grove. There are  n  birds and  n  trees. The  ith  bird stands on the top of the  ith  tree. The trees stand in straight line from left to the right. Every tree has its height. Jack stands on the left side of the left most tree. When Jack shots a bullet in height H to the right, the bird which stands in the tree with height  H  will falls.
Jack will shot many times, he wants to know how many birds fall during each shot.

a bullet can hit many birds, as long as they stand on the top of the tree with height of  H .
 

Input
There are multiple test cases (about 5), every case gives  n,m  in the first line,  n  indicates there are  n  trees and  n  birds,  m  means Jack will shot  m  times.

In the second line, there are  n  numbers  h[1],h[2],h[3],,h[n]  which describes the height of the trees.

In the third line, there are m numbers  q[1],q[2],q[3],,q[m]  which describes the height of the Jack’s shots.

Please process to the end of file.

[Technical Specification]

1n,m1000000(106)

1h[i],q[i]1000000000(109)

All inputs are integers.
 

Output
For each  q[i] , output an integer in a single line indicates the number of birds Jack shot down.
 

Sample Input
  
  
4 3 1 2 3 4 1 1 4
 

Sample Output
  
  
1 0 1
Hint
Huge input, fast IO is recommended.
 

Source
 

Recommend
hujie   |   We have carefully selected several similar problems for you:   5201  5200  5198  5197  5196 
 

代码“

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<map>
using namespace std;

map<int,bool>mp;

const int maxn=1000000+10;
const int prime=14997;
int a[maxn],b[maxn];



typedef struct Node
{
    int key;
    struct Node *next;
}node,*listnode;

listnode table[prime];

void init()
{
    for(int i=0;i<prime;i++)
    {
        table[i]=(listnode)malloc(sizeof(node));
        table[i]->next=NULL;
    }
}

inline int ReadInt()//优化接受int数,省时间,具体内容自己看懂,当成模板使用
{
    char ch = getchar();
    int data = 0;
    while (ch < '0' || ch > '9')
        ch = getchar();
    do
    {
        data = data*10 + ch-'0';
        ch = getchar();
    }while (ch >= '0' && ch <= '9');
    return data;
}

void inSert(int real_val,int val)
{
    listnode p,New;
    p=table[val];
    New=(listnode)malloc(sizeof(node));
    New->key=real_val;
    New->next=p->next;
    p->next=New;
}

int Search(int mod,int x)
{
    listnode p;
    p=table[mod]->next;
    int sum=0;
    while(p!=NULL)
    {
       if(p->key==x)
       {
           p->key=0;
           sum++;
       }
       p=p->next;
    }
    return sum;
}

int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        init();
        for(int i=1;i<=n;i++)
        {
            a[i]=ReadInt();
            int Hashval=a[i]%prime;
            inSert(a[i],Hashval);
        }
        mp.clear();
        for(int i=1;i<=m;i++)
        {
            int x=ReadInt();
            if(mp[x])
            {
                printf("0\n");
                continue;
            }
            int mod=x%prime;
            mp[x]=true;
            printf("%d\n",Search(mod,x));
        }
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值