# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1113691 | Tsagana | Mountain Trek Route (IZhO12_route) | C++14 | 1 ms | 336 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<bits/stdc++.h>
#define IOS ios_base::sync_with_stdio(false);cin.tie();cout.tie();
#define all(x) x.begin(), x.end()
#define lnl long long
#define pq priority_queue
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define pp pop_back
#define F first
#define S second
using namespace std;
int a[1000001];
map<int, int> mp;
map<int, int> ms;
map<int, int> mb;
pq<int> q;
void solve () {
int n, k; cin >> n >> k;
int calc = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
if (i > 1) calc += abs(a[i] - a[i-1]);
if (mp.find(a[i]) == mp.end()) {
q.push(-a[i]);
ms[a[i]] = a[i];
}
mp[a[i]]++;
}
calc += abs(a[n] - a[1]);
int mn;
while (1) {
int x = -q.top(); q.pop();
if (q.empty()) {
mn = -1;
break ;
}
int y = mp[x];
int z = -q.top();
if (k < y) {
mn = x;
break ;
}
if (k >= y * (z - x)) {
k -= y * (z - x);
ms[x] = z;
mb[x] = 1;
mp[z] += mp[x];
continue ;
}
if (k >= y) {
int l = k / y;
ms[x] = x + l;
mb[x] = 1;
mn = x + l;
k -= y * l;
break ;
}
mn = x;
break ;
}
for (int i = 1; i <= n; i++) {
while (mb[a[i]]) a[i] = ms[a[i]];
}
int ans = 0;
for (int i = 2; i <= n; i++) {
ans += abs(a[i] - a[i-1]);
}
cout << calc - (ans + abs(a[n] - a[1]));
}
int main() {IOS solve(); return 0;}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |