#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>
using ll = long long int;
ll h;
struct LeftNode
{
ll position, time;
LeftNode(ll position, ll time) {this->position = position; this->time = time;}
bool operator>(const LeftNode& other) const
{
ll maxtime = std::max(time, other.time);
ll mypos = position - (maxtime - time) * h;
ll otherpos = other.position - (maxtime - other.time) * h;
return mypos > otherpos;
}
bool operator<(const LeftNode& other) const
{
ll maxtime = std::max(time, other.time);
ll mypos = position - (maxtime - time) * h;
ll otherpos = other.position - (maxtime - other.time) * h;
return mypos < otherpos;
}
ll curtimepos(ll curtime)
{
return position - (curtime - time) * h;
}
};
struct RightNode
{
ll position, time;
RightNode(ll position, ll time) {this->position = position; this->time = time;}
bool operator<(const RightNode& other) const
{
ll maxtime = std::max(time, other.time);
ll mypos = position + (maxtime - time) * h;
ll otherpos = other.position + (maxtime - other.time) * h;
return mypos < otherpos;
}
bool operator>(const RightNode& other) const
{
ll maxtime = std::max(time, other.time);
ll mypos = position + (maxtime - time) * h;
ll otherpos = other.position + (maxtime - other.time) * h;
return mypos > otherpos;
}
ll curtimepos(ll curtime)
{
return position + (curtime-time) * h;
}
};
bool smaller(ll x, LeftNode ln, ll time)
{
ll nodepos = ln.curtimepos(time);
return x < nodepos;
}
bool bigger(ll x, RightNode rn, ll time)
{
ll nodepos = rn.curtimepos(time);
return x > nodepos;
}
int main()
{
std::priority_queue<LeftNode> leftprioq;
std::priority_queue<RightNode,
std::vector<RightNode>,
std::greater<RightNode> > rightprioq;
ll n, x, ans = 0;
std::cin >> n >> h;
std::cin >> x;
leftprioq.push(LeftNode(x, 0));
rightprioq.push(RightNode(x, 0));
for (int i = 1; i < n; ++i)
{
std::cin >> x;
LeftNode leftelem = leftprioq.top();
RightNode rightelem = rightprioq.top();
// std::cout << "i: " << i << "\n";
// std::cout << "borders: " << leftelem.curtimepos(i) << " " << rightelem.curtimepos(i) << "\n";
if (smaller(x, leftelem, i))
{
ll leftnodepos = leftelem.curtimepos(i);
leftprioq.pop();
leftprioq.push(LeftNode(x, i));
leftprioq.push(LeftNode(x, i));
LeftNode nextleftnode = leftprioq.top();
ll nextleftpos = nextleftnode.curtimepos(i);
ans += leftnodepos - x;
// std::cout << "left: " << nextleftpos << " " << leftnodepos << "\n";
rightprioq.push(RightNode(leftnodepos, i));
}
else if (bigger(x, rightelem, i))
{
ll rightnodepos = rightelem.curtimepos(i);
rightprioq.pop();
rightprioq.push(RightNode(x, i));
rightprioq.push(RightNode(x, i));
RightNode nextrightnode = rightprioq.top();
ll nextrightpos = nextrightnode.curtimepos(i);
ans += x - rightnodepos;
// std::cout << "right: " << rightnodepos << " " << nextrightpos << "\n";
leftprioq.push(LeftNode(rightnodepos, i));
}
else
{
leftprioq.push(LeftNode(x, i));
rightprioq.push(RightNode(x, i));
}
}
std::cout << ans;
}
Compilation message
safety.cpp: In function 'int main()':
safety.cpp:114:16: warning: unused variable 'nextleftpos' [-Wunused-variable]
ll nextleftpos = nextleftnode.curtimepos(i);
^~~~~~~~~~~
safety.cpp:130:16: warning: unused variable 'nextrightpos' [-Wunused-variable]
ll nextrightpos = nextrightnode.curtimepos(i);
^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
256 KB |
Output is correct |
4 |
Correct |
4 ms |
128 KB |
Output is correct |
5 |
Correct |
5 ms |
256 KB |
Output is correct |
6 |
Correct |
4 ms |
256 KB |
Output is correct |
7 |
Correct |
5 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
5 ms |
416 KB |
Output is correct |
3 |
Correct |
5 ms |
256 KB |
Output is correct |
4 |
Correct |
5 ms |
256 KB |
Output is correct |
5 |
Correct |
5 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
256 KB |
Output is correct |
5 |
Correct |
5 ms |
256 KB |
Output is correct |
6 |
Correct |
4 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
131 ms |
6656 KB |
Output is correct |
2 |
Correct |
176 ms |
8156 KB |
Output is correct |
3 |
Correct |
182 ms |
8400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
256 KB |
Output is correct |
4 |
Correct |
4 ms |
128 KB |
Output is correct |
5 |
Correct |
5 ms |
256 KB |
Output is correct |
6 |
Correct |
4 ms |
256 KB |
Output is correct |
7 |
Correct |
5 ms |
256 KB |
Output is correct |
8 |
Correct |
5 ms |
256 KB |
Output is correct |
9 |
Correct |
5 ms |
416 KB |
Output is correct |
10 |
Correct |
5 ms |
256 KB |
Output is correct |
11 |
Correct |
5 ms |
256 KB |
Output is correct |
12 |
Correct |
5 ms |
256 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
256 KB |
Output is correct |
4 |
Correct |
4 ms |
128 KB |
Output is correct |
5 |
Correct |
5 ms |
256 KB |
Output is correct |
6 |
Correct |
4 ms |
256 KB |
Output is correct |
7 |
Correct |
5 ms |
256 KB |
Output is correct |
8 |
Correct |
5 ms |
256 KB |
Output is correct |
9 |
Correct |
5 ms |
416 KB |
Output is correct |
10 |
Correct |
5 ms |
256 KB |
Output is correct |
11 |
Correct |
5 ms |
256 KB |
Output is correct |
12 |
Correct |
5 ms |
256 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
6 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
256 KB |
Output is correct |
4 |
Correct |
4 ms |
128 KB |
Output is correct |
5 |
Correct |
5 ms |
256 KB |
Output is correct |
6 |
Correct |
4 ms |
256 KB |
Output is correct |
7 |
Correct |
5 ms |
256 KB |
Output is correct |
8 |
Correct |
5 ms |
256 KB |
Output is correct |
9 |
Correct |
5 ms |
416 KB |
Output is correct |
10 |
Correct |
5 ms |
256 KB |
Output is correct |
11 |
Correct |
5 ms |
256 KB |
Output is correct |
12 |
Correct |
5 ms |
256 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
6 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
7 ms |
616 KB |
Output is correct |
29 |
Correct |
8 ms |
768 KB |
Output is correct |
30 |
Correct |
8 ms |
768 KB |
Output is correct |
31 |
Correct |
6 ms |
512 KB |
Output is correct |
32 |
Correct |
7 ms |
640 KB |
Output is correct |
33 |
Correct |
7 ms |
640 KB |
Output is correct |
34 |
Correct |
7 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
256 KB |
Output is correct |
4 |
Correct |
4 ms |
128 KB |
Output is correct |
5 |
Correct |
5 ms |
256 KB |
Output is correct |
6 |
Correct |
4 ms |
256 KB |
Output is correct |
7 |
Correct |
5 ms |
256 KB |
Output is correct |
8 |
Correct |
5 ms |
256 KB |
Output is correct |
9 |
Correct |
5 ms |
416 KB |
Output is correct |
10 |
Correct |
5 ms |
256 KB |
Output is correct |
11 |
Correct |
5 ms |
256 KB |
Output is correct |
12 |
Correct |
5 ms |
256 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
256 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
256 KB |
Output is correct |
17 |
Correct |
5 ms |
256 KB |
Output is correct |
18 |
Correct |
4 ms |
256 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
6 ms |
384 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
5 ms |
384 KB |
Output is correct |
33 |
Correct |
5 ms |
384 KB |
Output is correct |
34 |
Correct |
7 ms |
616 KB |
Output is correct |
35 |
Correct |
8 ms |
768 KB |
Output is correct |
36 |
Correct |
8 ms |
768 KB |
Output is correct |
37 |
Correct |
6 ms |
512 KB |
Output is correct |
38 |
Correct |
7 ms |
640 KB |
Output is correct |
39 |
Correct |
7 ms |
640 KB |
Output is correct |
40 |
Correct |
7 ms |
640 KB |
Output is correct |
41 |
Correct |
9 ms |
768 KB |
Output is correct |
42 |
Correct |
8 ms |
512 KB |
Output is correct |
43 |
Correct |
8 ms |
640 KB |
Output is correct |
44 |
Correct |
10 ms |
612 KB |
Output is correct |
45 |
Correct |
8 ms |
640 KB |
Output is correct |
46 |
Correct |
8 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
256 KB |
Output is correct |
4 |
Correct |
4 ms |
128 KB |
Output is correct |
5 |
Correct |
5 ms |
256 KB |
Output is correct |
6 |
Correct |
4 ms |
256 KB |
Output is correct |
7 |
Correct |
5 ms |
256 KB |
Output is correct |
8 |
Correct |
5 ms |
256 KB |
Output is correct |
9 |
Correct |
5 ms |
416 KB |
Output is correct |
10 |
Correct |
5 ms |
256 KB |
Output is correct |
11 |
Correct |
5 ms |
256 KB |
Output is correct |
12 |
Correct |
5 ms |
256 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
256 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
256 KB |
Output is correct |
17 |
Correct |
5 ms |
256 KB |
Output is correct |
18 |
Correct |
4 ms |
256 KB |
Output is correct |
19 |
Correct |
131 ms |
6656 KB |
Output is correct |
20 |
Correct |
176 ms |
8156 KB |
Output is correct |
21 |
Correct |
182 ms |
8400 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
5 ms |
384 KB |
Output is correct |
33 |
Correct |
6 ms |
384 KB |
Output is correct |
34 |
Correct |
5 ms |
384 KB |
Output is correct |
35 |
Correct |
5 ms |
384 KB |
Output is correct |
36 |
Correct |
5 ms |
384 KB |
Output is correct |
37 |
Correct |
7 ms |
616 KB |
Output is correct |
38 |
Correct |
8 ms |
768 KB |
Output is correct |
39 |
Correct |
8 ms |
768 KB |
Output is correct |
40 |
Correct |
6 ms |
512 KB |
Output is correct |
41 |
Correct |
7 ms |
640 KB |
Output is correct |
42 |
Correct |
7 ms |
640 KB |
Output is correct |
43 |
Correct |
7 ms |
640 KB |
Output is correct |
44 |
Correct |
9 ms |
768 KB |
Output is correct |
45 |
Correct |
8 ms |
512 KB |
Output is correct |
46 |
Correct |
8 ms |
640 KB |
Output is correct |
47 |
Correct |
10 ms |
612 KB |
Output is correct |
48 |
Correct |
8 ms |
640 KB |
Output is correct |
49 |
Correct |
8 ms |
640 KB |
Output is correct |
50 |
Correct |
201 ms |
8720 KB |
Output is correct |
51 |
Correct |
152 ms |
8168 KB |
Output is correct |
52 |
Correct |
122 ms |
8272 KB |
Output is correct |
53 |
Correct |
150 ms |
8272 KB |
Output is correct |
54 |
Correct |
192 ms |
8216 KB |
Output is correct |
55 |
Correct |
123 ms |
8280 KB |
Output is correct |
56 |
Correct |
140 ms |
8276 KB |
Output is correct |
57 |
Correct |
136 ms |
8268 KB |
Output is correct |
58 |
Correct |
106 ms |
7640 KB |
Output is correct |
59 |
Correct |
159 ms |
8272 KB |
Output is correct |