bestcoder#36 1002 哈希

bestcoder#36 1002 哈希

Gunner

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

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 i−th

bird stands on the top of the i−th

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]

1≤n,m≤1000000(106)

1≤h[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.

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<ctype.h>

using namespace std;

const int maxn=1000100;
const int HM=1000100;

int n,m;
struct Hash
{
    int date;
    int cnt;
    Hash *next;
};
Hash H[maxn];

inline int read()
{
    char c=getchar();
    while(!isdigit(c)) c=getchar();
    int f=c-‘0‘;
    while(isdigit(c=getchar())) f=f*10+c-‘0‘;
    return f;
}

int h(int x)
{
    return x%HM;
}

void insert(Hash*H,int date)
{
    int key=h(date);
    Hash *pre=&H[key],*p=pre->next;
    while(p!=NULL){
        if(p->date==date){
            p->cnt++;
            return;
        }
        pre=p;
        p=p->next;
    }
    p=(Hash*)malloc(sizeof(Hash));
    pre->next=p;
    p->next=NULL;
    p->date=date;
    p->cnt=1;
}

void find_del_output(Hash*H,int date)
{
    int key=h(date);
    Hash *pre=&H[key],*p=pre->next;
    while(p!=NULL){
        if(p->date==date){
            printf("%d\n",p->cnt);
            pre->next=p->next;///删掉结点
            free(p);
            return;
        }
        pre=p;
        p=p->next;
    }
    puts("0");
}

int main()
{
    while(cin>>n>>m){
        memset(H,0,sizeof(H));
        while(n--){
            int h=read();
            insert(H,h);
        }
        while(m--){
            int q=read();
            find_del_output(H,q);
        }
    }
    return 0;
}

时间: 2024-08-04 22:25:36

bestcoder#36 1002 哈希的相关文章

bestcoder#32 1002 哈希+后缀数组

bestcoder#32 1002 哈希+后缀数组 Negative and Positive (NP) Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1161    Accepted Submission(s): 59 Problem Description When given an array (a0,a1,a2,?an−1) a

Bestcoder#5 1002

Bestcoder#5 1002 Poor MitsuiTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 336    Accepted Submission(s): 70 Problem Description Because of the mess Mitsui have made, (not too long ago, Mitsui,

HDOJ 4884 &amp; BestCoder#2 1002

TIANKENG’s rice shop Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 212    Accepted Submission(s): 9 Problem Description TIANKENG managers a pan fried rice shop. There are n kinds of fried rice

bestcoder#43 1002 在数组中找两个数的和取模的最大值 二分

bestcoder#43 1002 在数组中找两个数的和取模的最大值  二分 pog loves szh II Accepts: 97 Submissions: 834 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem Description Pog and Szh are playing games. There is a sequence with n number

HDU 5269 &amp;&amp; BestCoder #44 1002 ZYB loves Xor I (分治)

题目地址:HDU 5269 比赛的时候想到了分治的思路,但是脑残了.,.写麻烦了...调了好久也没调出来..(分治写的太少..)赛后优化了一下..就过了.. 我的思路是先排序,排序是按照的将每个数字的二进制表示倒过来的字典序从大到小排,比如样例2中的2,6,5,4,0,二进制分别是010,110,101,100,000,排序之后是 101 110 010 100 000 这样的话就把后缀相同的都给放在一块了.其实也相当于字典树,不过比赛的时候没想到字典树,只想到了分治.. 然后从末位开始找,对所

bestcoder #9 1002(hdu4994)Revenge of Nim(博弈)

题目地址:HDU 4994 在这个题中,谁拥有了第一个大于1的数的控制权,就是谁赢,因为它可以有两种选择,一种是全选,另一种是选n-1个,此时另一个只能选剩下的那一个.但是当数为1的时候是没法控制的,只能选这一个.这时候就可以在每次选一个大于1的数的时候,就通过这两种选择,来让自己会正好选下一个大于1的数,由于中间的全是1,所以完全可以达到目的.这样只要控制了第一个大于1的数,那后面的就可以通过这次的操作让下面一个大于1的数仍然正好轮到自己,一直到最后一个也是.所以就转化成了判断第一个大于1的数

bestcoder#23 1002 Sequence II 树状数组+DP

Sequence II Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 652    Accepted Submission(s): 164 Problem Description Long long ago, there is a sequence A with length n. All numbers in this sequenc

【bestcoder #36】ABCD题解

Strange Class Accepts: 519 Submissions: 1749 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 在Vivid的学校里,有一个奇怪的班级(SC).在SC里,这些学生的名字非常奇怪.他们的名字形式是这样的anbncn(a,b,c两两不相同.).例如,叫"abc","ddppqq"的学生是在SC里的,然而叫&

【HDU】4908 (杭电 BC #3 1002题)BestCoder Sequence ——哈希

BestCoder Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 573    Accepted Submission(s): 201 Problem Description Mr Potato is a coder.Mr Potato is the BestCoder. One night, an amazing s