이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
const ll inf = LLONG_MAX;
struct slope_trick
{
multiset < ll > left_slope, right_slope;
ll height, left_offset, right_offset;
slope_trick()
{
height = left_offset = right_offset = 0;
}
ll get_left()
{
if (left_slope.empty())
return - inf;
return *left_slope.rbegin() + left_offset;
}
ll get_right()
{
if (right_slope.empty())
return inf;
return *right_slope.begin() + right_offset;
}
void add_basic(ll value)
{
ll left = get_left(), right = get_right();
if (value >= left && value <= right)
{
left_slope.insert(value - left_offset);
right_slope.insert(value - right_offset);
}
else
if (value < left)
{
left_slope.insert(value - left_offset);
left_slope.insert(value - left_offset);
right_slope.insert(left - right_offset);
left_slope.erase(left_slope.find(left - left_offset));
height = height + abs(left - value);
}
else
if (value > right)
{
right_slope.insert(value - right_offset);
right_slope.insert(value - right_offset);
left_slope.insert(right - left_offset);
right_slope.erase(right_slope.find(right - right_offset));
height = height + abs(right - value);
}
}
void minimum_interval(ll d)
{
left_offset -= d;
right_offset += d;
}
};
const int maxn = 2e5 + 10;
int n, a[maxn], d;
void solve()
{
cin >> n >> d;
slope_trick st;
for (int i = 1; i <= n; i ++)
{
cin >> a[i];
st.minimum_interval(d);
st.add_basic(a[i]);
}
cout << st.height << endl;
}
int main()
{
solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |