Skip to content

Commit a846105

Browse files
committed
packages
1 parent 9d0cf90 commit a846105

1 file changed

Lines changed: 67 additions & 0 deletions

File tree

1/pack.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <queue>
4+
#include <iterator>
5+
#include <algorithm>
6+
7+
class Buf
8+
{
9+
public:
10+
Buf(int size): _size(size), _proc(0) {}
11+
~Buf() {}
12+
13+
void AddPackage(int time) {
14+
int duration = 0;
15+
std::cin >> duration;
16+
if (_check(time))
17+
_push(time, duration);
18+
else
19+
log.push_back(-1);
20+
}
21+
std::vector<int> log;
22+
23+
private:
24+
std::queue<int> _buffer;
25+
int _proc;
26+
int _size;
27+
28+
bool _check(int time) {
29+
if (_buffer.empty())
30+
return true;
31+
if (_buffer.front() <= time)
32+
_buffer.pop();
33+
if (_buffer.size() < _size)
34+
return true;
35+
return false;
36+
}
37+
38+
void _push(int time, int duration) {
39+
time = std::max(time, _proc);
40+
log.push_back(time);
41+
_buffer.push(time + duration);
42+
_proc = std::max(time + duration, _proc);
43+
}
44+
};
45+
46+
int main(void)
47+
{
48+
int size = 0;
49+
int n = 0;
50+
51+
std::cin >> size >> n;
52+
53+
if (n == 0)
54+
return 0;
55+
56+
Buf buf(size);
57+
58+
for (int i = 0; i < n; ++i) {
59+
int time = 0;
60+
std::cin >> time;
61+
buf.AddPackage(time);
62+
}
63+
64+
for (auto i : buf.log)
65+
std::cout << i << std::endl;
66+
return 0;
67+
}

0 commit comments

Comments
 (0)