Cf learn from life (simple and greedy)

Source: Internet
Author: User

There are n people standing on the first floor. An elevator can carry up to k people.

Everyone has a floor to go. F [1]... f [N]. F [I] belongs to []

It takes ABS (a-B) seconds from layer a to layer B.

Ask the elevator how many seconds it will take to deliver the whole person and return to the first floor.

 

Ideas:

Sort the floors that everyone wants to go from small to large.

First, send the upper-level personnel to the lower-level personnel, and then send K at a time. (Proof: to send the highest level of the person's trip (complete) at least ** time, in this trip "by the belt" To Go To The K-1 high-rise building people to complete)

 

Code:

Int N, K; int f [2005]; int main () {scanf ("% d", & N, & K); rep (I, 0, n-1) scanf ("% d", & F [I]); sort (F, F + n); int ans = 0; For (INT I = n-1; I> = 0; i-= k) {ans + = (f [I]-1) * 2);} printf ("% d \ n", ANS );}

 

Cf learn from life (simple and greedy)

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.