# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
874637 | sleepntsheep | Mountain Trek Route (IZhO12_route) | C++17 | 2 ms | 6492 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <cassert>
#include <cstring>
#include <vector>
#include <algorithm>
#include <deque>
#include <set>
#include <utility>
#include <array>
using i64 = long long;
using u64 = unsigned long long;
using f64 = double;
using f80 = long double;
using namespace std;
#define ALL(x) x.begin(), x.end()
#define ShinLena cin.tie(nullptr)->sync_with_stdio(false);
#define N 1000000
int z, n, k; i64 a[N+3], t[N+3<<1],d[N+3<<1];
#define SSS int m = (l+r)/2,vl=v+1,vr=v+(m-l+1)*2
void build(int v, int l, int r)
{
if (l == r) return void(t[v] = a[l]);
SSS;
build(vl, l,m), build(vr,m+1,r);t[v]=min(t[vl],t[vr]);
}
void push(int v, int l,int r)
{
if(!d[v])return;
t[v]=d[v];
if (l-r)
{
SSS;
d[vl]=d[vr]=d[v];
}
d[v]=0;
}
void upd(int v, int l, int r, int x, int y ,i64 k)
{
if (r<x||y<l)return;
if(x<=l&&r<=y){d[v]=k;push(v,l,r);return;}
SSS;
upd(vl,l,m,x,y,k),upd(vr,m+1,r,x,y,k);t[v]=min(t[vl],t[vr]);
}
i64 qry(int v, int l, int r, int x, int y)
{
if (r<x||y<l)return 1e18+1;
if(x<=l&&r<=y){return t[v];}
SSS;
return min(qry(vl,l,m,x, y),qry(vr,m+1,r,x,y));
}
int main()
{
ShinLena;
cin >> n >> k;
a[0] = a[n+2] = 1e18+1;
for (int i = 1; i <= n; ++i) cin >> a[i];
a[n+1] = a[0];
n += 3;
build(0, 0, n-1);
deque<pair<i64, i64>> v; deque<int> s = {0};
for (int i = 1; i < n; ++i)
{
for (; s.size() && a[s.back()] <= a[i]; )
{
if (a[s.back()] == 1e18+1 && a[i] == 1e18+1) break;
i64 S = qry(0, 0, n-1, s.back() + 1, i);
if (a[s.back()]-S <= 0)
{
s.pop_back();
continue;
}
//cout<<s.back()<< ' ' << i << ' '<<a[s.back()]-S<<endl;
v.emplace_back(i - s.back() - 1,a[s.back()]-S);
upd(0,0,n-1,s.back(),i-1,a[s.back()]);
s.pop_back();
}
if (s.size() && a[s.back()] > a[i])
{
i64 S = qry(0, 0, n-1, s.back() + 1, i);
//cout<<s.back()<< ' ' << i << ' '<<a[s.back()]-S<<endl;
v.emplace_back(i - s.back() - 1,a[i]-S);
upd(0,0,n-1,s.back()+1,i,a[i]);
}
s.push_back(i);
}
sort(ALL(v));
for (; v.size() && v[0].first == 0; ) v.pop_front();
for (; v.size() && k >= v[0].first; )
{
i64 take = min(k / v[0].first, v[0].second);
//cout<<v[0].first<< ' '<<take<<endl;
k -= take * v[0].first;
z += 2 * take;
v.pop_front();
}
cout << z;
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |