51nod-1786 algorithms in the data stream-majority

Source: Internet
Author: User

1786 algorithms in the data stream-majority base time limit: 1.5 Second space limit: 131072 KB score: 80 Difficulty: 5 level algorithm The data stream statistics function is on-line, which makes a great contribution to 51nod to enhance the user experience. But the new question comes along, and the jacket master wants to know that in one window, the most visited users (that is, the number of people in the window). If there is more than one number, take one of the least user IDs. (the window means a fixed-length interval!) (because the data stream is real-time and online, it is not allowed to use the offline algorithm ^_^) Input
The first behavior is the integer n, K. (1 <= N <= 5 * 10^6,1 <= K <=) n represents how many operations, K represents the window size. Next n rows, each row represents an operation. The first integer per line is the operand. Operand 1: User access Input format: <1, id> User ID is an integer in [0, Int_max] closed interval. The user who owns this ID has visited the site once and the window is moved accordingly. Operand 2: Ask the majority input format:<2> the number of people in the Output window, if there are multiple, the one with the lowest output ID. P.S. For operations that ask for the majority, the window guarantees that the P.S.S is not empty. The window may be dissatisfied with the operation of asking for the majority
Output
For operations that ask for a majority, each line outputs an integer.
Input example
10 51 21 11 21 1 1 21 121 31 32
Output example
11

Main topic:
At first saw this question, did not understand what meaning, has been wondering what the window is. Later only to understand, is to implement the query, the query when the first K access times, the number of the most visited users.
Analysis:
First of all, the consideration is only to consider the first K access before the query, the complexity is the number of k* queries, if most of the query is obviously difficult to pass.
Then, with the priority queue to implement, the priority queue content is a struct, stores the user's ID, and the number of visits. However, the priority queue does not support delete operations,
So how to implement the deletion has exceeded the K user data? My approach is to use the map to record the number of visits per user, and to delete the users of K before they are only updated in the map. Then, when you query
To loop over the top element of the priority queue, check if the number of accesses stored by this element is the same as the number of records in the map, and if not, the node is invalidated, out of the queue, and then the top element, until the same.
Then, turn it over, and then time out, change the map to Hash_map, optimize the input and then turn it in, 700ms through.
In fact, the priority queue after the writing, and did not immediately use Hash_map, the program changed to use set to implement, set the maximum value is set in the last element, insert delete complexity, log (n), 2000ms, timeout ... It then changes the map to Hash_map, optimizes the input and output, and then
700ms through ...
Ps:hash_map with VC + + to turn on can be successfully compiled
Priority queue implementation #include<iostream>using namespace Std; #include <queue> #include <cstdio> #include 

  

Set implementation #include<iostream>using namespace Std; #include <queue> #include <cstdio> #include 

  

51nod-1786 algorithms in the data stream-majority

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.