Submission #38308

# Submission time Handle Problem Language Result Execution time Memory
38308 2018-01-03T15:16:18 Z romanasa Mountain Trek Route (IZhO12_route) C++14
0 / 100
2000 ms 10544 KB
#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
1 Correct 0 ms 2184 KB Output is correct
2 Correct 0 ms 2184 KB Output is correct
3 Correct 0 ms 2184 KB Output is correct
4 Correct 0 ms 2184 KB Output is correct
5 Correct 0 ms 2184 KB Output is correct
6 Correct 0 ms 2184 KB Output is correct
7 Correct 76 ms 2184 KB Output is correct
8 Correct 283 ms 2184 KB Output is correct
9 Correct 1183 ms 2184 KB Output is correct
10 Correct 156 ms 9840 KB Output is correct
11 Execution timed out 2000 ms 10544 KB Execution timed out
12 Halted 0 ms 0 KB -