【NWERC2015】【BZOJ4425】 AssigningWorkstations分配工作站 贪心优先队列加结构体快排
Assigning Workstations |
Time Limit: 20000ms, Special Time Limit:50000ms, Memory Limit:65536KB |
Total submit users: 7, Accepted users: 4 |
Problem 13604 : No special judgement |
Problem description |
Penelope is part of the admin team of the newly built supercomputer. Her job is to assign work-stations to the researchers who come here to run their computations at the supercomputer. Penelope is very lazy and hates unlocking ma-chines for the arriving researchers. She can un-lock 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 re-searchers 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 m 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 ≤ 300 000), 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 |
Sample Input |
3 5 1 5 6 3 14 6 5 10 2 6 1 2 17 7 3 9 15 6 |
Sample Output |
2 3 |
佩内洛普是新建立的超级计算机的管理员中的一员。 她的工作是分配工作站给到这里来运行他们的计算研究任务的研究人员。
佩内洛普非常懒惰,不喜欢为到达的研究者们解锁机器。 她可以从在她的办公桌远程解锁这些机器,但她并不觉得这卑贱的任务配得上她,所以她决定忽略安全指南偷偷懒。她可以直接地要求,研究者在他们离开时不用锁定自己的工作站,然后把未在使用且还在未锁定状态的工作站分配给新来的研究人员。 这样,她只需要为每一个工作站第一次被使用所属的研究员解锁工作站,这对佩内洛普的工作来说是一个巨大的改善。
不幸的是,如果一个工作站在未锁定且没被使用的状态下超过m分钟,会自动锁定自己,佩内洛普必须为使用它的下一个研究员再次打开它。 鉴于抵达和离开的研究人员的确切时间表,你可以告诉佩内洛普,要求研究者在离开时不锁定工作站最多可以使她节约多少次的解锁工作。你可以认为这儿总是有足够的可用工作站。
Input
一行两个整数n (1≤n≤300000) 研究员的数量n,以及 m (1≤m≤100000000) 工作站在未锁定且没被使用的状态下超过m分钟会自动锁定。
下面的n行,每一行两个整数a与s (1≤a,s≤100000000) 表示一个研究员在第a分钟时到达以及待了s分钟后离开。
Output
输出研究者在离开时不锁定工作站最多可以使她节约多少次解锁工作。
—————————————————华丽的分割线—————————————————
这题意感觉有点坑,分钟和人数都用a,结果以为是a个人,其实是1个人==,硬是看了很久的题目。。
思路:对到达时间进行排序,用贪心:每次分配新研究员时间最短的工作站。可以使用优先队列。
代码如下:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<queue> 5 #include<algorithm> 6 using namespace std; 7 struct node{ 8 int l,r; 9 }s[300010]; 10 bool cmp1(node a,node b) { return a.l<b.l; } 11 main() 12 { 13 int i,ans,n,m; 14 priority_queue<int,vector<int>,greater<int> >Q; 15 while(~scanf("%d%d",&n,&m)) 16 { 17 ans=0; 18 for(i=1;i<=n;i++) 19 { 20 scanf("%d%d",&s[i].l,&s[i].r); 21 s[i].r+=s[i].l; 22 } 23 sort(s+1,s+1+n,cmp1); 24 Q.push(s[1].r+m); 25 for(i=2;i<=n;i++) 26 { 27 while(!Q.empty()&&s[i].l>Q.top()) 28 Q.pop(); 29 if(s[i].l>=Q.top()-m&&s[i].l<=Q.top()) 30 ans++,Q.pop(); 31 Q.push(s[i].r+m); 32 } 33 printf("%d\n",ans); 34 } 35 }