Uyhip: the most demanding voting Program

Source: Internet
Author: User

A country has n citizens numbered from 1 to n. This is a democratic country. Every decision made by the state requires all citizens.VoteEach user must and can only vote for one vote.

However, as the population of the country increasesVoteThe efficiency is getting lower and lower. As a result, this country has implemented a new democratic system. Every four years, this country will hold a "representative election conference". At that time, every citizen must and can only nominate a person he can trust as his own representative. Note: You can nominate yourself as your representative. For each nominated person, the number of people who nominate him is equivalent to the number of votes (rounded down ). In the next four years, when the country wants to make a decision, it only needs these representatives to participate.

For example, there are 200 people in this country. At the representative election conference, 98 people nominate No. 1 citizens as representatives and 101 people nominate No. 2 citizens as representatives, one person nominated 200 as a representative of the citizen. The result is that only citizens 1 and 2 become representatives and participate in the next four years.VoteAmong them, one vote for citizens on the first day is 49 votes, and one vote for citizens on the second is 50 votes. It is worth noting that, despite the nomination of citizen No. 200, the support rate is only 0.5%. Therefore, he will not have the right to be represented in the next four years.


To support the new democratic policy, you need to design a set of algorithms to calculate which citizens become representatives after each session of the representative election conference and their respective votes in their hands. The input data of the program comes from a tape. There are n numbers on the tape, respectively recording the numbers of the representatives nominated by the N citizens respectively (because the nominations are anonymous, the data on the tape is not sequential, you cannot determine who each number belongs ). The program can read the tape multiple times, but each time it can only read each number from start to end in sequence. Because the number of people in this country has increased to a certain extent, your program must be very efficient. Specifically, your algorithm must meet the following restrictions:

1. Your program should read as few tapes as possible;

2. The tape is read-only;

3. The program can store variables in its own memory, but it can only use the space of O (1) Units (that is, the memory space consumed is not related to N );

4. the space of each unit in the memory can only store integers (including 0 and N) between 0 and N );

5. After the pre-processing stage is completed, the procedure will enter the inquiry stage, that is, to answer questions such as "how many votes are obtained by Citizen X. Once preprocessing is completed and enters the inquiry stage, the program will no longer be able to access the tape.

The problem now is, in the worst case, How many times does the tape need to be read at least? Give an algorithm that meets the requirements and prove that the number of tapes read is no longer enough.




Known Quantity: a maximum of 100 candidates

Constraints: each person can be selected only when the number of votes obtained is greater than or equal to N/100.

) The voting sequence can only be traversed in sequence, but not randomly. It can be traversed multiple times. The sequence cannot be modified.

The memory used is O (1), and each memory unit can only store integers ranging from 0 to n.

Must answer the number of votes received by anyone, and the target can be anyone.

) Traversal sequence as little as possible

Unknown number: design an algorithm to identify the number of votes of all candidates.


There can be a maximum of 100 candidates, so at least 100 of memory space is required.

If the number of a person is less than 1%, the right value is 0, and no number of votes is required to be recorded. Therefore, the problem can be understood as finding out all the numbers in the sequence that appear greater than 1%.


Try to reduce the constraints if more memory space is available. We can use the space of O (n) to calculate the number of occurrences of each number.

Then collect the number with the Count greater than or equal to 1%. In this way, the condition ") the number of votes each person receives is greater than or equal to N/100 to be selected" is useless (whether you have used all the conditions)

So the key to this problem is: how to use the ratio of some numbers> = % 1


In order to facilitate thinking, we specialized the problem and looked for more than 1/2 numbers in the sequence. This problem seems simpler. (Special)


After reading "the beauty of programming", I was able to think of the question of "looking for a poster", and I was looking for more than half of the numbers. (Can you think of a similar problem ?)

Find more than half of the IDs in the ID sequence

The answer to this question is clever. You only need to traverse the sequence. The most important thing is how to use a number to show more than half.

Memory dictionary, which records the counts of two numbers and these two numbers. In the traversal process, if two numbers are collected, the count is reduced by 1. If the count is 0, discard this number. This operation is called cleanup.

When traversing the sequence, if this number is recorded in the dictionary, add 1 to the memory count. Otherwise, add it to the dictionary and set the count to 1. Then, execute the cleanup operation.

Each time two different numbers are cleared, the target number is more than half, and the problem scale is reduced. After traversing all the sequences, only one number is left.


Extended question: How can I solve this problem if all three numbers exceed 1/4 and the remaining number is less than 1/4?

The above method is used, but the number of records must be 4 in the dictionary.


In this way, if 99 numbers exceed 1%, and the remaining number is less than 1%, is it equivalent to the original problem?

This is not the case. In our case, more than 1% of the numbers can be 0 to 100, not necessarily 99.


Use the above method to apply it to our problem scenarios. What is the final situation? (Can I use it ?)

It seems that the number of occurrences greater than or equal to 1% will appear in our dictionary, but the number of occurrences less than 1% will also appear in our dictionary.


Of course, pay attention to some details: if there are 100 numbers in the sequence, each account for 1%. If the Count of each number is 1 after the last number is traversed, clear the number again, this will cause all numbers to be discarded.

In summary, the last clean-up will result in the cleaning of numbers with a proportion less than or equal to 1%. The number of accounts for 1% must be recorded. The last number cannot be cleared after traversal.

1) First, after the first traversal, there are 100 numbers in the dictionary, with the number of percentages greater than or equal to 1% retained, and some percentages less than 1%.

2) then, we just need to traverse the sequence again and count how many times the 100 number appeared.

3) Finally, collect the number of occurrences greater than or equal to 1% and Their counts in the second traversal.


Related Article

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.