Submission #8665756


Source Code Expand

/**
 *    author:  tourist
 *    created: 26.11.2019 17:52:04       
**/
#include <bits/stdc++.h>

using namespace std;

template <typename A, typename B>
string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string& s) {
  return '"' + s + '"';
}

string to_string(const char* s) {
  return to_string((string) s);
}

string to_string(bool b) {
  return (b ? "true" : "false");
}

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  string s;
  cin >> s;
  int low = 1, high = n;
  while (low < high) {
    int mid = (low + high + 1) >> 1;
    vector<int> b(n);
    for (int i = 0; i < n; i++) {
      b[i] = (a[i] >= mid);
    }
    vector<int> seq(1, 1);
    for (int i = 1; i < n; i++) {
      if (b[i] != b[i - 1]) {
        seq.push_back(1);
      } else {
        seq.back() += 1;
      }
    }
    seq[0] += n;
    seq.back() += n;
    int sz = (int) seq.size();
    vector<int> what(sz);
    for (int i = 0; i < sz; i++) {
      what[i] = (b[0] + i) % 2;
    }
    vector<set<pair<int, int>>> inner(2);
    vector<int> delta(2, 0);
    for (int i = 0; i < sz; i++) {
      inner[what[i]].emplace(seq[i], i);
    }
    vector<int> pr(sz);
    vector<int> ne(sz);
    for (int i = 0; i < sz; i++) {
      pr[i] = i - 1;
      ne[i] = i + 1;
    }
    auto Next = [&](int x) {
      return ne[x];
    };
    auto Prev = [&](int x) {
      return pr[x];
    };
    int head = 0;
    auto Remove = [&](int x) {
      if (x == head) {
        head = ne[head];
      }
      if (ne[x] < sz) {
        pr[ne[x]] = pr[x];
      }
      ne[pr[x]] = ne[x];
    };
    for (char c : s) {
      debug(mid, (string) "" + c, seq, delta, inner, ne);
      int u = (c == 'm' ? 0 : 1);
      delta[u] += 1;
      delta[u ^ 1] -= 1;
      while (!inner[u ^ 1].empty() && inner[u ^ 1].begin()->first + delta[u ^ 1] == 0) {
        int me = inner[u ^ 1].begin()->second;
        debug(me);
        inner[u ^ 1].erase(inner[u ^ 1].begin());
        inner[u].erase({seq[Prev(me)], Prev(me)});
        inner[u].erase({seq[Next(me)], Next(me)});
        seq[Prev(me)] += seq[Next(me)] + delta[u];
        inner[u].emplace(seq[Prev(me)], Prev(me));
        Remove(me);
        Remove(Next(me));
      }
      if (what[0] == u) {
        inner[u].erase({seq[0], 0});
        seq[0] -= 1;
        inner[u].emplace(seq[0], 0);
      }
    }
    vector<int> v;
    for (int i = 0; i < sz; i = ne[i]) {
      v.push_back(seq[i] + delta[what[i]]);
    }
    debug(v);
    int sum = accumulate(v.begin(), v.end(), 0);
//    assert(sum % 2 == 1);
    debug(mid, sum);
    int half = n + 1;
    int have = 0;
    int ptr = 0;
    int winner = -1;
    for (int i = 0; i < sz; i = ne[i]) {
      have += v[ptr++];
      if (have >= half) {
        winner = what[i];
        break;
      }
    }
    debug(seq, delta);
    debug("hi");
/*    int winner;
    if (Next(head) == sz) {
      winner = what[head];
    } else {
      if (Next(Next(head)) == sz) {
        winner = (seq[head] > seq[Next(head)] ? what[head] : what[Next(head)]);
      } else {
        winner = what[Next(head)];
      }
    }*/
    if (winner == 1) {
      low = mid;
    } else {
      high = mid - 1;
    }
  }
  cout << low << '\n';
  return 0;
}

Submission Info

Submission Time
Task B - Compression
User tourist
Language C++14 (GCC 5.4.1)
Score 1400
Code Size 5222 Byte
Status AC
Exec Time 415 ms
Memory 6344 KB

Judge Result

Set Name Sample subtask1 subtask2 All
Score / Max Score 0 / 0 400 / 400 800 / 800 200 / 200
Status
AC × 4
AC × 13
AC × 13
AC × 53
Set Name Test Cases
Sample sample_1.txt, sample_2.txt, sample_3.txt, sample_4.txt
subtask1 sample_2.txt, subtask_1.2_1.txt, subtask_1.2_2.txt, subtask_1_1.txt, subtask_1_10.txt, subtask_1_2.txt, subtask_1_3.txt, subtask_1_4.txt, subtask_1_5.txt, subtask_1_6.txt, subtask_1_7.txt, subtask_1_8.txt, subtask_1_9.txt
subtask2 sample_1.txt, subtask_1.2_1.txt, subtask_1.2_2.txt, subtask_2_1.txt, subtask_2_10.txt, subtask_2_2.txt, subtask_2_3.txt, subtask_2_4.txt, subtask_2_5.txt, subtask_2_6.txt, subtask_2_7.txt, subtask_2_8.txt, subtask_2_9.txt
All sample_1.txt, sample_2.txt, sample_3.txt, sample_4.txt, 2_1.txt, 2_2.txt, sample_1.txt, sample_2.txt, sample_3.txt, sample_4.txt, subtask_1.2_1.txt, subtask_1.2_2.txt, subtask_1_1.txt, subtask_1_10.txt, subtask_1_2.txt, subtask_1_3.txt, subtask_1_4.txt, subtask_1_5.txt, subtask_1_6.txt, subtask_1_7.txt, subtask_1_8.txt, subtask_1_9.txt, subtask_2_1.txt, subtask_2_10.txt, subtask_2_2.txt, subtask_2_3.txt, subtask_2_4.txt, subtask_2_5.txt, subtask_2_6.txt, subtask_2_7.txt, subtask_2_8.txt, subtask_2_9.txt, subtask_3_1.txt, subtask_3_10.txt, subtask_3_11.txt, subtask_3_12.txt, subtask_3_13.txt, subtask_3_14.txt, subtask_3_15.txt, subtask_3_16.txt, subtask_3_17.txt, subtask_3_18.txt, subtask_3_19.txt, subtask_3_2.txt, subtask_3_20.txt, subtask_3_21.txt, subtask_3_3.txt, subtask_3_4.txt, subtask_3_5.txt, subtask_3_6.txt, subtask_3_7.txt, subtask_3_8.txt, subtask_3_9.txt
Case Name Status Exec Time Memory
2_1.txt AC 1 ms 256 KB
2_2.txt AC 1 ms 256 KB
sample_1.txt AC 1 ms 256 KB
sample_2.txt AC 1 ms 256 KB
sample_3.txt AC 1 ms 256 KB
sample_4.txt AC 1 ms 256 KB
subtask_1.2_1.txt AC 1 ms 256 KB
subtask_1.2_2.txt AC 1 ms 256 KB
subtask_1_1.txt AC 172 ms 4296 KB
subtask_1_10.txt AC 127 ms 4296 KB
subtask_1_2.txt AC 92 ms 4296 KB
subtask_1_3.txt AC 77 ms 4296 KB
subtask_1_4.txt AC 93 ms 3836 KB
subtask_1_5.txt AC 23 ms 768 KB
subtask_1_6.txt AC 142 ms 4296 KB
subtask_1_7.txt AC 1 ms 256 KB
subtask_1_8.txt AC 133 ms 4296 KB
subtask_1_9.txt AC 127 ms 4296 KB
subtask_2_1.txt AC 317 ms 4552 KB
subtask_2_10.txt AC 302 ms 4424 KB
subtask_2_2.txt AC 190 ms 4552 KB
subtask_2_3.txt AC 310 ms 4552 KB
subtask_2_4.txt AC 177 ms 4552 KB
subtask_2_5.txt AC 1 ms 256 KB
subtask_2_6.txt AC 1 ms 256 KB
subtask_2_7.txt AC 1 ms 256 KB
subtask_2_8.txt AC 325 ms 4476 KB
subtask_2_9.txt AC 37 ms 1280 KB
subtask_3_1.txt AC 415 ms 4296 KB
subtask_3_10.txt AC 278 ms 4296 KB
subtask_3_11.txt AC 411 ms 4296 KB
subtask_3_12.txt AC 99 ms 4296 KB
subtask_3_13.txt AC 102 ms 4296 KB
subtask_3_14.txt AC 107 ms 4296 KB
subtask_3_15.txt AC 95 ms 4296 KB
subtask_3_16.txt AC 92 ms 4296 KB
subtask_3_17.txt AC 94 ms 4296 KB
subtask_3_18.txt AC 90 ms 4296 KB
subtask_3_19.txt AC 102 ms 4296 KB
subtask_3_2.txt AC 308 ms 6344 KB
subtask_3_20.txt AC 102 ms 4296 KB
subtask_3_21.txt AC 102 ms 4296 KB
subtask_3_3.txt AC 37 ms 820 KB
subtask_3_4.txt AC 350 ms 3836 KB
subtask_3_5.txt AC 309 ms 3476 KB
subtask_3_6.txt AC 350 ms 4296 KB
subtask_3_7.txt AC 275 ms 4296 KB
subtask_3_8.txt AC 284 ms 4296 KB
subtask_3_9.txt AC 194 ms 4296 KB