优先队列入门题。
我们贪心的想,发现对于每次操作,选择时间最短的机器肯定是最优的。但是如果每次操作都 寻找时间最短的机器肯定会 T,所以考虑使用优先队列。
优先队列分为两种:大根堆 priority_queue<int>q 和小根堆 priority_queue<int,vector<int>,greater<int> >q,其中大根堆默认堆顶为堆的最大值,小根堆默认堆顶为堆的最小值,对于本题,显然用小根堆。我们需要三种操作:访问堆顶、弹出堆顶、弹入堆。这三种操作都是 的,所以本题总复杂度为 。
为了方便,我们还可以使用 。当我们将优先队列的类型改为 时,它会默认按 的第一关键字排序,更加方便我们操作。
最后,由于 , 的范围较大,我们需要开 long long。
code:
#include<bits/stdc++.h>
#define PII pair<int,int>
#define int long long
#define fi first
#define se second
using namespace std;
const int N=1e5+10;
inline int read() {
int s=0,x=1;char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') x=-1;ch=getchar();}
while(isdigit(ch)) s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
return s*x;
}
int n,k,a[N],b[N],ans;
priority_queue<PII,vector<PII>,greater<PII> >q;
signed main() {
n=read(),k=read();
for(int i=1;i<=n;++i) {
a[i]=read(),b[i]=read();
q.push({a[i],i});//pair的第一关键字为时间,第二关键字为下标
}
for(int i=1;i<=k;++i) {
auto u=q.top();q.pop();
ans+=u.fi;u.fi+=b[u.se];
q.push(u);
}
printf("%lld\n",ans);
return 0;
}