12
Level up your coding skills with the C++ Standard Template Library (STL): SPOJ Problem: OLOLO BY JOYJIT CHOUDHURY

SPOJ Problem: OLOLO

Embed Size (px)

Citation preview

Page 1: SPOJ Problem: OLOLO

Level up your coding skills with

the C++ Standard Template

Library (STL):

SPOJ Problem: OLOLO

BY JOYJIT CHOUDHURY

Page 2: SPOJ Problem: OLOLO

OLOLO - Onotole needs your help

Is accessible at - http://www.spoj.com/problems/OLOLO/

Page 3: SPOJ Problem: OLOLO

Problem Statement

Onotole has a lot of pyani. Each pyani has a number, writing on it.

Pyanis with equal numbers are indistinguishable.

Onotole knows everything, so, he knows that each pyani appeared twice, and only one pyani is unique. He wants to get вздръжни эффект, and he needs the unique pyani.

Given the list of pyanis denote which one of them appeared once (it is guaranteed that other pyanis appeared twice).

Page 4: SPOJ Problem: OLOLO

Input First line of input contains number of pyanis

N <= 500 000. Next N lines contain a single positive

integer 1 <= Pi <= 10^9.

Output Output one positive integer on pyani, which appeared

once.

Time limit: 0.134sSource limit: 50000BMemory limit: 1536MB

Page 5: SPOJ Problem: OLOLO

So, the idea is…

There will be N numbers as input

All the numbers come in pairs, except one of them

We have to output that one number

Page 6: SPOJ Problem: OLOLO

So…

There can be many different approaches to solving this problem (read: using XOR)

A solution using std::unordered_set will be demonstrated here

For every number Pi, we check whether it’s already in the set or not:

If it’s not present, that means it’s the first occurrence of the number. Thus, we insert it into the set

If it is present, that means it has occurred once before. So, we remove it from the set

Page 7: SPOJ Problem: OLOLO

In the End

There will be just one number left in the unordered_set

The one which didn’t come in pair

The one we need to output

Page 8: SPOJ Problem: OLOLO

Code: I

Page 9: SPOJ Problem: OLOLO

Code: II

Page 10: SPOJ Problem: OLOLO
Page 11: SPOJ Problem: OLOLO

Solution Accepted!

Page 12: SPOJ Problem: OLOLO