UPC 1773 water problem (priority_queue priority queue)

Question F: Water catch problem

Time Limit:  1 Sec   Memory Limit:  128 MB
Commits:  276   Resolved:  107
[ Commit ][ Status ][ Discussion Board ][Assert By: admin ]

Topic description

There is a water room in the school. There are a total of m faucets in the water room for students to turn on the water.

Now there are n students ready to receive water, and their initial order of receiving water has been determined. The students are numbered from 1 to n in the order of receiving water, and the amount of water received by class i is wi. At the beginning of the water collection, students from 1 to m each have a faucet and turn on the faucet to collect water at the same time. When one of the students j completes his water intake requirement wj, the next classmate k who is waiting in line to receive the water immediately takes over from the position of classmate j and starts to receive the water. The replacement process is instantaneous, and there is no waste of water. That is, when student j finishes receiving water at the end of the xth second, then student k starts to receive water immediately at the x+1 second. If the current number of people receiving water n' is less than m, only n' taps supply water, and the other m−n' taps are closed.

Now, given the water intake of n students, according to the above water intake rules, ask how many seconds it takes for all students to receive the water.

enter

In the first line, two integers n and m, separated by a space, represent the number of people receiving water and the number of faucets, respectively.
The n integers w1, w2, ..., wn in the second line, separated by a space between each two integers, wi represents the amount of water received by classmate i.
1≤n≤10000, 1≤m≤100 and m≤n; 1≤wi≤100.

output

The output has only one line, 1 integer, representing the total time it takes to pick up the water.

sample input

5 3
4 4 1 2 1

样例输出

4

提示

第1秒,3人接水。第1秒结束时,1、2、3号同学每人的已接水量为1,3号同学接完水,4号同学接替3号同学开始接水。
第2秒,3人接水。第2秒结束时,1、2号同学每人的已接水量为2,4号同学的已接水量为1。
第3秒,3人接水。第3秒结束时,1、2号同学每人的已接水量为3,4号同学的已接水量为2。4号同学接完水,5号同学接替4号同学开始接水。
第4秒,3人接水。第4秒结束时,1、2号同学每人的已接水量为4,5号同学的已接水量为1。1、2、5号同学接完水,即所有人完成接水。
总接水时间为4秒。



#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;

int main()
{
    int n, m;
    while(cin >> n >> m)
    {
        priority_queue<int, vector<int>, greater<int> >a;
        for(int i=1; i<=n; i++)
        {
            int t, j;
            cin >> t;
            if(i <= m)
                a.push(t);
            else
            {
                j = a.top();
                a.pop();
                a.push(j+t);
            }
        }
        for(int i=1; i<m; i++)
            a.pop();
        cout << a.top() << endl;
    }
}
/**
8 4
4 3 6 5 2 7 8 4
*/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325724245&siteId=291194637