Statistics of Recompre…

来源:互联网 发布:js或 编辑:程序博客网 时间:2024/06/04 19:01
Statistics of Recompressing Videos
TimeLimit:3000MS     MemoryLimit:262144KB     64bitIO Format:%I64d & %I64u
Submit Status Practice CodeForces523D

Description

A social network for dogs called DH (DogHouse)has k specialservers to recompress uploaded videos of cute cats. After eachvideo is uploaded, it should be recompressed on one (any) of theservers, and only after that it can be saved in the socialnetwork.

We know that each server takes one second to recompress a oneminute fragment. Thus, any servertakes m secondsto recompress a mminute video.

We know the time when each ofthe n videoswere uploaded to the network (in seconds starting from the momentall servers started working). All videos appear at differentmoments of time and they are recompressed in the order they appear.If some video appeared attime s, then its recompressing can startat that very moment, immediately. Some videos can awaitrecompressing when all the servers are busy. In this case, as soonas a server is available, it immediately starts recompressinganother video. The videos that await recompressing go in a queue.If by the moment the videos started being recompressed some serversare available, then any of them starts recompressing the video.

For each video find the moment it stops being recompressed.

Input

The first line of the input containsintegers n and k (1 ≤ n, k ≤ 5·105)— the number of videos and servers, respectively.

Next n linescontain the descriptions of the videos as pairs ofintegers si, mi (1 ≤ si, mi ≤ 109),where si isthe time in seconds whenthe i-th video appearedand mi isits duration in minutes. It is guaranteed that allthe si's are distinctand the videos are given in the chronological order of upload, thatis in the order ofincreasing si.

Output

Print n numbers e1, e2, ..., en,where ei isthe time in seconds after the servers start working, whenthe i-th video will berecompressed.

Sample Input

Input
3 2
1 5
2 5
3 5
Output
6
7
11
Input
6 1
1 1000000000
2 1000000000
3 1000000000
4 1000000000
5 1000000000
6 3
Output
1000000001
2000000001
3000000001
4000000001
5000000001
5000000004

题目意思就是有一堆机器和一些工程,每个工程都有出现时间和完成它所需要的时间,求出每个工程最快完成的时间。题目中有一个明显的单词queue,因此用优先队列来进行维护。并且输入已经将序列排好,不解的是我的ide居然不能完成greater的函数。刚学习了队列,用的不是很熟,占了不少内存。中间也有不少废代码,可以消去。

 

#include
#include
#include
using namespace std;
struct Video{
 long long st;
 long long ed;
 long long id;
 bool operator <(const Video&a)const
 {
  return st + ed>a.ed + a.st;
 }
}video[500005];
long long re[500005];
priority_queueq;
int main()
{
 ios::sync_with_stdio(false);
 long long n, k, i, j, ans;
 cin >> n >> k;
 for (i = 1; i <= n; i++)
 {
  cin >> video[i].st >> video[i].ed;
  video[i].id = i;
 }
 re[1] = video[1].ed + video[1].st;
 q.push(video[1]);
 ans = 1;
 for (i = 2; i <= n; i++)
 {
  if (!q.empty())
  {
   if (q.top().ed + q.top().st <= video[i].st)
   {
    re[i] = video[i].st + video[i].ed;
    q.pop();
   }
   else
   {
    if (ans < k)
    {
     ans++;
     re[i] = video[i].st + video[i].ed;
    }
    else
    {
     video[i].st = q.top().st + q.top().ed;
     re[i] = video[i].st + video[i].ed;
     q.pop();
    }
   }
   q.push(video[i]);
  }
  else
  {
   re[i] = video[i].st + video[i].ed;
   q.push(video[i]);
   ans++;
  }
 }
 for (i = 1; i <= n; i++)
  cout << re[i] << "\n";
 return 0;
}
0 0
原创粉丝点击