【NOIP2010】【Luogu1190】接水问题(给定顺序的模拟)

problem

题意:

  • 有n个人,每个人接水wi
  • 有m个水龙头,每个水龙头每秒供水1
  • 求n个人接完水要多少时间

注意:

  • 人按编号1~n接水(坑点,顺序确定)
  • 换人过程没有浪费
  • n < m 多余水龙头关闭

范围:

  • n < 1e4, m < 1e3, wi < 100

solution

规则已经确定了啊,不是求最优解,就是模拟这个过程得到答案而已。
1、用小根堆q存储每个水龙头剩余时间,每次取出剩余最少的把他加上wi放回去。
2、最后小根堆中值最大的就是接水时间。
复杂度O(nlogn).

codes

#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn = 110;
int n, m, a[maxn];
priority_queue<int,vector<int>,greater<int> >q;
int main(){
    cin>>n>>m;
    for(int i = 1; i <= n; i++){//因为顺序固定了
        if(i <= m){//最少的是0,直接丢
            int x;  cin>>x;  q.push(x);
        }else{//取出最少的加进去
            int x;  cin>>x;
            int t = q.top(); q.pop();
            t += x;
            q.push(t);
        }
    }
    int ans = 0;
    while(q.size()!=1)q.pop();
    ans = q.top();
    cout<<ans<<'\n';
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_33957603/article/details/81162512