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