Blue Bridge: take water resistant question

Water resistant connection problems

Problem Description
There ⼀ school ⾥ a water resistant housing, water resistant housing with a total of m ⾥ ⼀ a ⻰ students head for open water resistant, water resistant for an equal amount per head of each ⻰ are
To 1 . There are n students ready to take water resistant, water resistant pick their initial order has been determined. These water resistant bonding by students in order from 1 to n ed
No, i take the amount of water resistant number of students to the Wi . When the start then water resistant, 1 to m number of students each accounted ⼀ a water resistant ⻰ head while turning the head-water resistant ⻰ water resistant.
When one of some students j to complete its required amount of ground water resistant wj after queuing waiting to record the next take water resistant k students succeed on ⻢ j position of the students began to take
Water resistant. Face of this change process is instantaneous, and did not waste any water resistant of. I.e. j of students x complete water resistant bonding at the end of seconds, k the students
The first x + 1 seconds immediately while began to take water resistant. If the current access water resistant Face number n 'underexposed m , only of n' ⻰ head for water resistant, other m-n 'th ⻰ head off. Present
In the given n water resistant amount of contact students, in accordance with the rules of said ground water resistant, all students were asked how many seconds then finished water resistant.
START input format
Of 1 2 integers n and m , uses one separated by spaces, respectively, and then the number of water resistant Face number ⻰ head. The second n integers W1 , w2 of Use between, ......, wn, each of two integers
⼀ separated by spaces, wi represents i pick the amount of water resistant number of students.
Output Format
Only output
⼀ ⾏ 1 integer representing the total time required to get water resistant.
Sample lose START
5 3
4 4 1 2 1
Sample Output
4
Sample lose START
8 4
23 71 87 32 70 93 80 76
Sample Output
163
START input sample output 1 Description
First . 1 second, 3 Face pick water resistant. The first one at the end of seconds, 1 , 2 , . 3 No. Face students each have an amount of water resistant bonding 1 , . 3 No. students then finished
Water resistant, 4 Hao students succeed 3 Hao students began to take water resistant.
Second 2 seconds 3 Face pick water resistant. Second 2 end seconds, 1 , 2 No. Face each student has an amount of water resistant contact 2 , . 4 No. students Received
Water resistant capacity of 1 .
The first 3 seconds 3 Face pick water resistant. Of 3 At the end of seconds, 1 , 2 No. Face each student has an amount of water resistant contact 3 , . 4 No. students Received
Water resistant capacity of 2 . 4 Hao students then finished water resistant, 5 Hao students succeed 4 Hao students began to take water resistant.
First . 4 seconds 3 Face pick water resistant. The first 4 end second, 1 , 2 No. Face each student has an amount of water resistant contact 4 , . 5 No. students Received
Water resistant capacity of 1 . 1 , 2 , . 5 No. students then finished water resistant, i.e., complete access to all Face water resistant.
The total contact time was water resistant . 4 seconds.
Scale data and conventions
≤ ≤ n-10000. 1 , . 1 ≤m≤ 100 and m ≤ n- ;
≤ ≤ wi - 1 100 .
analysis:
1. Each time the students take water resistant, are ⾛ to line up before the minimum time required team line up every time some students need a minimum queue time teams are changing
2. Use priority queue, each queue head removed, to the incoming queue time plus the students, and then thrown into the tail
3. Finally, the team most ⻓, is water resistant then the total time ~
 
 
 
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main() {
 priority_queue<int, vector<int>, greater<int> > q;
 int n, m, ans;
 cin >> n >> m;
 for ( int i = 1; i <= n; i++) {
   int t;
   scanf("%d", &t);
   if( i <= m )
       q.push(t);
   else {
       int a = q.top();
       q.pop();
       q.push(a + t);
   }
 }
 while (!q.empty()) {
 	ans = q.top();
    q.pop();
 }
 cout << ans;
 return 0; 
}
Published 736 original articles · won praise 123 · views 80000 +

Guess you like

Origin blog.csdn.net/S_999999/article/details/103341611
Recommended