Codeforces B. Design Tutorial: learn from life

Source: Internet
Author: User
Tags cmath

There is an elevator. Everyone wants to take the elevator to the floor they want to reach!
The time from layer a to layer B is | a-B | the time for passengers to move up or down the elevator is negligible! Minimum questions
How long does it take ....
This is a question of God. Without knowing it, I followed the thought of annotation and thought about it.
Try to simulate it with the priority queue. Maybe it's still not simulated. All the time! However
The priority queue has been simulated! I didn't expect it to be solved by a few simple lines of the greedy algorithm!

1 # include <iostream> 2 # include <cmath> 3 # include <cstdio> 4 # include <algorithm> 5 # include <cmath> 6 # include <cstring> 7 # define n 2005 8 using namespace STD; 9 10 int f [N]; 11 12 INT main () {13 int N, K; 14 CIN> N> K; 15 for (INT I = 1; I <= N; ++ I) 16 CIN> F [I]; 17 sort (F + 1, F + n + 1, greater <int> ()); 18 19 int ans = 0; 20 21 for (INT I = 1; I <= N;) {// sort by the highest floor, fill K people with elevators, let people with low floors go down on the way! 22 ans + = (F [I]-1) * 2; // All the round-trip times are 2 times the time when the floor is reached. 23 I + = K; 24} 25 cout <ans <Endl; 26 return 0; 27}
View code

 

Codeforces B. Design Tutorial: learn from life

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.