Memorise Me!——用數值作地址,實現快速查找

題目以下:數組

Arijit is a brilliant boy. He likes memory games. He likes to participate alone but this time he has to have a partner. So he chooses you.less

In this Game , your team will be shown N numbers for few minutes . You will have to memorize these numbers.ide

Now, the questioner will ask you Q queries, in each query He will give you a number , and you have to tell him the total number of occurrences of that number in the array of numbers shown to your team . If the number is not present , then you will have to say 「NOT PRESENT」 (without quotes).ui

INPUT And OUTPUTthis

The first line of input will contain N, an integer, which is the total number of numbers shown to your team.spa

The second line of input contains N space separated integers .orm

The third line of input contains an integer Q , denoting the total number of integers.xml

The Next Q lines will contain an integer denoting an integer, Bi , for which you have to print the number of occurrences of that number (Bi) in those N numbers on a new line.ip

If the number Bi isn’t present then Print 「NOT PRESENT」 (without quotes) on a new line.ci

CONSTRAINTS

1≤N≤105

0≤Bi≤1000

1≤Q≤105

SAMPLE INPUT
 
 
6
1 1 1 2 2 0
6
1
2
1
0
3
4
SAMPLE OUTPUT
 
 
3
2
3
1
NOT PRESENT
NOT PRESENT
 
Explanation

The given array is (1,1,1,2,2,0) of size 6.

Total number of queries is 6 also.

For the first query i.e for 1 , the total of number of occurrences of 1 in the given array is 3 . Hence the corresponding output is 3.

For the second query i.e. for 2, the total of number of occurrences of 2 in the given array is 2 . Hence the corresponding output is 2.

For the fifth query i.e. for 3. 3 is not present in the array . So the corresponding output is "NOT PRESENT" (without quotes).

Time Limit:0.6 sec(s) for all input files combined.
Memory Limit:256 MB
Source Limit:1024 KB
Marking Scheme:Marks are awarded when all the testcases pass.
Allowed Languages:Bash, C, C++, C++14, Clojure, C#, D, Erlang, F#, Go, Groovy, Haskell, Java, Java 8, JavaScript(Rhino), JavaScript(Node.js), TypeScript, Julia, Kotlin, Lisp, Lisp (SBCL), Lua, Objective-C, OCaml, Octave, Pascal, Perl, PHP, Python, Python 3, R(RScript), Racket, Ruby, Rust, Scala, Swift, Swift-4.1, Visual Basic
 
 
https://www.hackerearth.com/zh/practice/data-structures/arrays/1-d/practice-problems/algorithm/memorise-me/description/
 
這道題的關鍵在於時間效率,由於有一個sample數據很是多,而這道題對時間的要求(0.6sec)遠比空間要求(256MB)高,因此選擇以空間換時間。
題目的關鍵在於數字的大小不會超過1000,因此能夠建立一個大小爲1001的數組(使用vector更好,初始化方便),而後用數字直接作地址記錄頻率。最後將所需數字的頻率輸出便可。
相關文章
相關標籤/搜索