time limit: 1 sspace limit: 128000 KBTitle Description
Description
There is a water room in the school, the water room is equipped with a total of M a faucet for students to open water, each faucet per second of the same amount of water, are 1.
Now there are n students ready to pick up water, and their initial water order has been determined. These students according to water order from 1 to n number, I number of students of the water intake is WI. When the water starts, 1 to m students each occupy a faucet, and at the same time turn on the tap to connect water. When one of the students J finished their water demand WJ, the next line of students waiting to receive the water, K immediately take over the position of J classmate began to connect water. The process of substitution is instantaneous, without any waste of water. That is, J classmate at the end of the first X seconds to complete the water, then K classmate x+1 seconds immediately began to pick up water. If the current number of water access n ' less than m, then only n ' Faucet water supply, the other m?n ' faucet closed.
Now give the water intake of n students, according to the above water rules, ask all the students how many seconds to finish the water.
Enter a description
Input Description
Line 1th 2 integers n and m, separated by a space, respectively, indicating the number of water and faucet.
Line 2nd n integers W1, w2 、......、 Wn, separated by a space between each two integers, WI indicates the number I
The amount of water to learn.
Output description
Output Description
The output is only one row, 1 integers, indicating the total time required to receive water.
Sample input
Sample Input
5 3
4 4 1) 2 1
Sample output
Sample Output
4
Data range and Tips
Data Size & Hint
n<=10000, m<=100
The 1th second, 3 people connect water. At the end of the 1th second, 1, 2, 3rd, each of the students have received water for 1, 3rd students to finish, 4th students to replace the 3rd students began to connect water.
The 2nd second, 3 people connect water. At the end of the 2nd second, 1, 2nd students each have received water of 2, 4th students have received water of 1.
The 3rd second, 3 people connect water. At the end of the 3rd second, 1, 2nd students each have received water of 3, 4th students have received water of 2. 4th students after the water, No. 5th students to replace the 4th students began to connect water.
The 4th second, 3 people connect water. At the end of the 4th second, 1, 2nd students each have received water of 4, 5th students have received water of 1. 1, 2, 5th students after the water, that is, all the water to complete.
The total water connection time is 4 seconds.
To protect a small Gan, you can finally output the maximum value in the heap.
The code is actually:
#include <cstdio>
#include <iostream>
using namespace Std;
int n,m,a,b,c,d,ans,dui[120];
void xx (int k) {
b=2*k;c=2*k+1;
if (dui[b]>dui[c]&&c<=m) d=c;
else d=b;
if (d>m) return;
if (Dui[k]>dui[d]) {a=dui[k];d ui[k]=dui[d];d ui[d]=a;}
XX (d);
}
void xgd (int x) {
Dui[1]+=x;
XX (1);
}
int main () {
cin>>n>>m;
for (int i=1;i<=n;i++) {CIN>>A;XGD (a);}
for (int i=1;i<=m;i++) Ans=max (Ans,dui[i]);
printf ("%d\n", ans);
return 0;
}
The code into the question ~
Water connection Problem (2010 NOIP National League popularity Group)