# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
38308 | romanasa | 등산 경로 (IZhO12_route) | C++14 | 2000 ms | 10544 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define err(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
using namespace std;
typedef long long ll;
#define TASK "g"
/*
const int MAXMEM = (int)5e7;
char MEM[MAXMEM];
int mpos;
inline void* operator new(size_t n) {
char *res = MEM + mpos;
mpos += n;
return (void*)res;
}
inline void operator delete(void*) {}
*/
const int base = (int)1e7;
ll get(pair<int, int> x) {return 1ll * x.first * base + x.second; }
typedef pair<int, ll> segment;
//#define val second.first
//#define len second.second
#define pos first
set<segment> S;
priority_queue<pair<int, int> > T;
void calc(int pos, int mx, vector<int> &x) {
int n = (int)x.size();
vector<pair<int, int> > cur;
for (int i = 0; i < n; i++) {
int ind = (pos + i);
if (ind >= n) ind -= n;
if (cur.empty() || cur.back().first != x[ind]) cur.push_back({ x[ind], 1 });
else cur.back().second++;
}
for (int i = 0; i < (int)cur.size(); i++) {
auto c = cur[i];
int pr = cur[i ? i - 1 : (int)cur.size() - 1].first;
int nt = cur[(i + 1) % (int)cur.size()].first;
S.insert(make_pair(pos, get(c)));
if (c.first < pr && c.first < nt) T.push({ -c.second, pos });
pos = (pos + c.second);
if (pos >= n) pos -= n;
}
}
void add(segment c) {
auto it = S.insert(c).first;
auto prv = it;
if (it == S.begin()) prv = --S.end();
else --prv;
auto nxt = it;
if (it == --S.end()) nxt = S.begin();
else ++nxt;
if (c.second / base < nxt->second / base && c.second / base < prv->second / base) T.push({ -c.second % base, c.first });
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
vector<int> x(n);
for (int i = 0; i < n; i++) cin >> x[i];
int pos = -1;
for (int i = 0; i < n; i++) {
if (x[i] != x[(i + 1) % n]) pos = (i + 1) % n;
}
if (pos == -1) return 0 * puts("0");
int mx = *max_element(x.begin(), x.end());
calc(pos, mx, x);
int ans = 0;
while (T.size() && k >= -T.top().first) {
k -= (-T.top().first);
auto it = S.lower_bound(make_pair(T.top().second, -1));
T.pop();
ans += 2;
if (S.size() == 1) break;
auto prv = it;
if (it == S.begin()) prv = --S.end();
else --prv;
auto nxt = it;
if (it == --S.end()) nxt = S.begin();
else ++nxt;
if (prv == nxt) {
if (it->second / base + 1 < prv->second / base) {
auto c = *it;
S.erase(it);
//c.val++;
c.second += base;
add(c);
} else {
auto c = *it;
c.second += base;
c.second += prv->second % base;
//c.val++;
//c.len += prv->len;
S.erase(it);
S.erase(prv);
add(c);
}
} else {
if (it->second / base + 1 < prv->second / base && it->second / base + 1 < nxt->second / base) {
auto c = *it;
S.erase(it);
//c.val++;
c.second += base;
add(c);
} else if (it->second / base + 1 < prv->second / base) {
auto c = *it;
//c.len += nxt->len;
//c.val++;
c.second += base;
c.second += nxt->second % base;
S.erase(it);
S.erase(nxt);
add(c);
} else if (it->second / base + 1 < nxt->second / base) {
auto c = *it;
//c.len += prv->len;
//c.pos = prv->pos;
//c.val++;
c.second += prv->second % base;
c.second += base;
c.first = prv->first;
S.erase(it);
S.erase(prv);
add(c);
} else {
auto c = *it;
//c.len += prv->len + nxt->len;
//c.pos = prv->pos;
//c.val++;
c.second += prv->second % base + nxt->second % base;
c.second += base;
c.first = prv->first;
S.erase(it);
S.erase(prv);
S.erase(nxt);
add(c);
}
}
}
cout << ans << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |