ACM voting-two-way and acm-two-way Voting

Source: Internet
Author: User

ACM voting-two-way and acm-two-way Voting
Problem description
There are N cities, and each city has Ai individuals.
Now we want to start voting. Each person has a vote.
As a leader, you have B boxes. You must distribute the B boxes to N cities. Each city needs at least one box.
Everyone must vote and not discard the vote. That is to say, they should drop the vote into the box (each city has an Ai ticket ).
Now, how can I assign these boxes to the minimum number of votes in the box with the largest number of votes in all these boxes?

Input
The input contains N (1 <= N <= 500,000) and B (N <= B <= 2,000,000 ).
The next N rows contain N numbers, and each number of Ai (1 <= Ai <= 5,000,000) indicates the number of people in each city.
Input N is-1 and B is-1.

Output
Output the number of soldiers carried by the generals with the largest number of troops.

Sample Input
2 7
200000
500000
4 6
120
2680
3400
200
-1-1

Sample output
100000
1700


Analysis

If a box is located in each city, the current answer is max (Ai (1 <= I <= n )). Therefore, if we need to allocate at least one box to the city with the largest value, the answer is certainly no longer that city (or the number of people in a city may be the same as that in the city, the answer remains unchanged ).
Next, if you still have boxes, you must allocate at least one box to the city in the secondary region;
Next, if you still have boxes, you must allocate at least one box to the city in the next region;
Next, if you have another box, you must allocate at least one box to the city in the next region;
Next, if you still have boxes, you must allocate at least one box to the city in the next round;
...
When?
Until half of the number of votes in the original largest city (an odd number needs to be increased by 1) is greater than or equal to the number of votes in the city you just arranged from large to small. Why?
Because the number of votes behind the city must be less than or equal to that city, none of these cities will have a larger number of votes than half of the number of votes in the largest city, that is, in the current state, in all cities, half of the original largest cities became the current maximum.

Q: What do you think? -- Two points!
We have two-digit votes. The number of votes ranges from 1 to the maximum number of votes in a city.
Note l = 1, r = max, then mid = (1 + max)/2;
If mid is the answer, we need to calculate a total of several boxes.

int num = 0;for (int i = 1; i <= n; i++){    if (a[i] % mid) num += a[i] / mid + 1;    else num += a[i] / mid;}



Finally, we need num boxes, but we have B boxes, so we need to compare them.
If num> B, the boxes we need are insufficient. That is to say, some cities in the cities with no less than the mid value cannot find enough boxes. Therefore, our final answer must be larger than num. Then the binary range falls into [mid + 1, max].
If num <= B, we need enough boxes. That is to say, all the cities in which the number of votes is not less than the mid value can be allocated enough boxes, our final answer must be less than or equal to num. Then the binary range falls into [1, mid].
Then, continue to the second point until the final range is l> = r.

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.