4425: [Nwerc2015]Assigning Workstations分配工作站
4425: [Nwerc2015]Assigning Workstations分配工作站
Description
Penelope is part of the admin team of the newly built supercomputer. Her job is to assign workstations to the researchers who come here to run their computations at the supercomputer.
Penelope is very lazy and hates unlocking machines for the arriving researchers. She can unlock the machines remotely from her desk, but does not feel that this menial task matches her qualifications. Should she decide to ignore the security guidelines she could simply ask the researchers not to lock their workstations when they leave, and then assign new researchers to workstations that are not used any more but that are still unlocked. That way, she only needs to unlock each workstation for the first researcher using it, which would be a huge improvement for Penelope.
Unfortunately, unused workstations lock themselves automatically if they are unused for more than mm minutes. After a workstation has locked itself, Penelope has to unlock it again for the next researcher using it. Given the exact schedule of arriving and leaving researchers, can you tell Penelope how many unlockings she may save by asking the researchers not to lock their workstations when they leave and assigning arriving researchers to workstations in an optimal way? You may assume that there are always enough workstations available.
Input
The input consists of:
one line with two integers n (1≤n≤300000), the number of researchers, and m (1≤m≤10^8), the number of minutes of inactivity after which a workstation locks itself;
n lines each with two integers a and s (1≤a,s≤10^8), representing a researcher that arrives after a minutes and stays for exactly s minutes.
Output
Output the maximum number of unlockings Penelope may save herself.
佩内洛普是新建立的超级计算机的管理员中的一员。 她的工作是分配工作站给到这里来运行他们的计算研究任务的研究人员。
佩内洛普非常懒惰,不喜欢为到达的研究者们解锁机器。 她可以从在她的办公桌远程解锁这些机器,但她并不觉得这卑贱的任务配得上她,所以她决定忽略安全指南偷偷懒。她可以直接地要求,研究者在他们离开时不用锁定自己的工作站,然后把未在使用且还在未锁定状态的工作站分配给新来的研究人员。 这样,她只需要为每一个工作站第一次被使用所属的研究员解锁工作站,这对佩内洛普的工作来说是一个巨大的改善。
不幸的是,如果一个工作站在未锁定且没被使用的状态下超过m分钟,会自动锁定自己,佩内洛普必须为使用它的下一个研究员再次打开它。 鉴于抵达和离开的研究人员的确切时间表,你可以告诉佩内洛普,要求研究者在离开时不锁定工作站最多可以使她节约多少次的解锁工作。你可以认为这儿总是有足够的可用工作站。
Input
一行两个整数n (1≤n≤300000) 研究员的数量n,以及 m (1≤m≤100000000) 工作站在未锁定且没被使用的状态下超过m分钟会自动锁定。
下面的n行,每一行两个整数a与s (1≤a,s≤100000000) 表示一个研究员在第a分钟时到达以及待了s分钟后离开。
Output
输出研究者在离开时不锁定工作站最多可以使她节约多少次解锁工作。
Sample Input
Sample Input 1
3 5
1 5
6 3
14 6
Sample Input 2
5 10
2 6
1 2
17 7
3 9
15 6
3 5
1 5
6 3
14 6
Sample Input 2
5 10
2 6
1 2
17 7
3 9
15 6
Sample Output
Sample Output 1
2
Sample Output 2
3
2
Sample Output 2
3
题解:
贪心。还是比较明显的。。
我们维护一个小跟堆,存p[i].a+p[i].b+m
如果堆中的元素值<当前的p[i].a,那么很明显它没什么用了(反正都要增加一次次数)。
找到第一个>=p[i].a的元素,再判断。。
#include<stdio.h> #include<iostream> #include<algorithm> using namespace std; const int N=300005; #define p1 (p<<1) #define p2 (p<<1|1) struct node { int a,b; }p[N]; int n,m,i,k,ans,t[N]; bool cmp(const node&x,const node&y) { return x.a<y.a; } void up(int p) { while((p>>1)>0) { if(t[p]<t[p>>1]) { swap(t[p],t[p>>1]); p>>=1; } else break; } } void down(int p) { int x; while(p1<=k) { if(p2<=k) { if(t[p1]<t[p2]) x=p1;else x=p2; } else x=p1; if(t[p]>t[x]) { swap(t[p],t[x]); p=x; } else break; } } int main() { scanf("%d%d",&n,&m); for(i=1;i<=n;i++) scanf("%d%d",&p[i].a,&p[i].b); sort(p+1,p+n+1,cmp); k=1;ans=0;t[1]=p[1].a+p[1].b+m; for(i=2;i<=n;i++) { while(k>0&&t[1]<p[i].a) { t[1]=t[k--]; down(1); } if(t[1]>=p[i].a&&t[1]-m<=p[i].a) { ans++; t[1]=t[k--]; down(1); } t[++k]=p[i].a+p[i].b+m; up(k); } cout<<ans; return 0; }
一念起,天涯咫尺; 一念灭,咫尺天涯。