这题很简单,就是对于每本书,在下一次出现前能否写完,能写完就写,写不完就在下一次idea出现之后判断,这样不停的判断,把中断的书全都加到队列里去,到最后没书写的时候把队列里的书拿出来继续写,由于要求的是天数总和最小,所以需要用到优先队列。
代码:
#include<bits/stdc++.h>
#define MEM(a,x) memset(a,x,sizeof(a));
#define MEMINF(a) memset(a,0x3f,sizeof(a));
using namespace std;
typedef long long LL;
const int MAXN=1e5+10;
const int INF=0x3f3f3f3f;
const int MOD=1000000007;
int n;
struct node {
LL r,p;
bool operator < (const node &a) const {
return r<a.r;
}
}nod[MAXN];
priority_queue<LL,vector<LL>,greater<LL> >q;
int main() {
freopen("pulp.in","r",stdin);
freopen("pulp.out","w",stdout);
cin>>n;
for (int i=1; i<=n; ++i) {
scanf("%lld %lld",&nod[i].r,&nod[i].p);
nod[i].r++;
}
LL ans=0;
LL t;
sort(nod+1,nod+n+1);
for (int i=1; i<=n; ++i) {
q.push(nod[i].p);
t=nod[i].r;
if (i<n)
while (!q.empty()) {
if (t>=nod[i+1].r) break;
LL u=q.top();
q.pop();
if (t+u-1>=nod[i+1].r) {
u-=nod[i+1].r-t;
t=nod[i+1].r;
q.push(u);
}
else {
t=t+u;
u=0;
ans+=t-1;
}
}
}
while(!q.empty()) {
LL u=q.top();
q.pop();
t+=u;
ans+=t-1;
}
cout<<ans<<endl;
}