101498B - Longest Prefix

  • Greedily match the characters of the second string to the first string.

Time complexity: $O(n)$

Memory complexity: $O(n)$

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
int main(void)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    int t;
    cin >> t;
    while (t--)
    {
        string a, b;
        vi bcnt(26);
        cin >> a >> b;
        for (auto ch : b)
            bcnt[ch - 'a']++;
        int i = 0, n = (int)(a).size();
        while (i < n and bcnt[a[i] - 'a'] > 0)
        {
            bcnt[a[i] - 'a']--;
            ++i;
        }
        cout << i << endl;
    }
    return 0;
}


101498A - Watching Tv

  • Maintain a map that stores the frequency count.
  • Find frequency with biggest count.

Time complexity: $O(n \log(n))$

Memory complexity: $O(n)$

Click to show code.


using namespace std;
using ll = long long;
int main(void)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        map<int, int> counter;
        while (n--)
        {
            string s;
            int freq;
            cin >> s >> freq;
            counter[freq]++;
        }
        int ans = max_element(begin(counter), end(counter), [](auto a, auto b) {
                      if (a.second == b.second)
                          return a.first > b.first;
                      return a.second < b.second;
                  })->first;
        cout << ans << endl;
    }
    return 0;
}


1327E - Count The Blocks

  • For a block of size i, you have 10 ways to choose it.
  • The two neighboring digits have 9 ways each to be chosen.
  • The remaining digits have $10^{n - i - 2}$ ways to be chosen. Implementation details:
  • Precompute powers of $10$ mod $998244353$.
  • To ease computation, distinguish between border and inner blocks.
  • See editorial for info on last point.

Time complexity: $O(n)$

Memory complexity: $O(n)$

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
template <typename InputIterator,
          typename T = typename iterator_traits<InputIterator>::value_type>
void read_n(InputIterator it, int n)
{
    copy_n(istream_iterator<T>(cin), n, it);
}
template <typename InputIterator,
          typename T = typename iterator_traits<InputIterator>::value_type>
void write(InputIterator first, InputIterator last, const char *delim = "\n")
{
    copy(first, last, ostream_iterator<T>(cout, delim));
}
int const MOD = 998244353;
int const NMAX = 2e5 + 11;
int main(void)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    int n;
    cin >> n;
    vector<ll> pten(n, 1);
    for (int i = 1; i < n; ++i)
        pten[i] = (pten[i - 1] * 10) % MOD;
    for (int i = 1; i < n; ++i)
    {
        ll res = 2 * 10 * 9 * pten[n - i - 1];
        res %= MOD;
        res += (n - 1 - i) * 10 * 9 * 9 * pten[max(n - i - 2, 0)];
        res %= MOD;
        cout << res << ' ';
    }
    cout << 10 << endl;
    return 0;
}


1213G - Path Queries

  • Sort edges increasingly and add them to a dsu in chunks of equal weight.
  • Compute answer for each weight.
  • For queries, binary search the answer.

Time complexity: $O(m \log(n))$

Memory complexity: $O(n)$

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using iii = array<int, 3>;
using vi = vector<int>;
template <typename InputIterator,
          typename T = typename iterator_traits<InputIterator>::value_type>
void read_n(InputIterator it, int n)
{
    copy_n(istream_iterator<T>(cin), n, it);
}
template <typename InputIterator,
          typename T = typename iterator_traits<InputIterator>::value_type>
void write(InputIterator first, InputIterator last, const char *delim = "\n")
{
    copy(first, last, ostream_iterator<T>(cout, delim));
}
struct DisjointSetUnion
{
    using vi = vector<int>;
    using ll = long long;
    int n;
    ll ans = 0;
    vi parent, ssize;
    DisjointSetUnion(int n) : n(n)
    {
        parent.resize(n);
        ssize.resize(n, 1);
        for (int u = 0; u < n; ++u)
            parent[u] = u;
    }
    int find_set(int u)
    {
        if (u == parent[u])
            return u;
        return (parent[u] = find_set(parent[u]));
    }
    inline ll contribution(ll m) { return (m * (m - 1)) / 2; }
    void union_sets(int u, int v)
    {
        u = find_set(u);
        v = find_set(v);
        if (u != v)
        {
            if (ssize[u] < ssize[v])
                swap(u, v);
            ans -= contribution(ssize[v]);
            ans -= contribution(ssize[u]);
            ans += contribution(ssize[u] + ssize[v]);
            parent[v] = u;
            ssize[u] += ssize[v];
        }
    }
};
ll const INF = 1e16;
int main(void)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    int n, m;
    cin >> n >> m;
    vector<iii> edges(n - 1);
    for (auto &[w, u, v] : edges)
        cin >> u >> v >> w, u--, v--;
    DisjointSetUnion dsu(n);
    vector<pair<int, ll>> ans = {{0, 0}};
    sort(begin(edges), end(edges));
    auto it = edges.begin();
    while (it != end(edges))
    {
        auto it_temp = it;
        while (it_temp != end(edges) and it_temp->at(0) == it->at(0))
        {
            dsu.union_sets(it_temp->at(1), it_temp->at(2));
            it_temp++;
        }
        ans.emplace_back(it->at(0), dsu.ans);
        it = it_temp;
    }
    while (m--)
    {
        int qi;
        cin >> qi;
        auto it = prev(upper_bound(begin(ans), end(ans), make_pair(qi, INF)));
        cout << it->second << " ";
    }
    cout << endl;
    return 0;
}


abc183_c - Travel

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
template <typename InputIterator,
          typename T = typename iterator_traits<InputIterator>::value_type>
void read_n(InputIterator it, int n)
{
    copy_n(istream_iterator<T>(cin), n, it);
}
template <typename InputIterator,
          typename T = typename iterator_traits<InputIterator>::value_type>
void write(InputIterator first, InputIterator last, const char *delim = "\n")
{
    copy(first, last, ostream_iterator<T>(cout, delim));
}
int main(void)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    int n, k;
    cin >> n >> k;
    vector<vi> t(n, vi(n));
    for (auto &ti : t)
        read_n(ti.begin(), n);
    vi a(n);
    for (int i = 0; i < n; ++i)
        a[i] = i;
    int ans = 0;
    do
    {
        int cur = 0;
        for (int i = 0; i < n; ++i)
            cur += t[a[i]][a[(i + 1) % n]];
        ans += cur == k;
    } while (next_permutation(a.begin() + 1, a.end()));
    cout << ans << endl;
    return 0;
}


abc180_d - Takahashi Unevolved

Click to show code.


using namespace std;
using ll = unsigned long long;
using ii = pair<int, int>;
using vi = vector<int>;
ll iceil(ll a, ll b) { return (a + b - 1) / b; }
int main(void)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    ll x, y, a, b;
    cin >> x >> y >> a >> b;
    ll xp = 0;
    while (x * a <= x + b and iceil(y, x) > a)
    {
        x *= a;
        xp++;
    }
    ll d = (y - x) % b == 0 ? (y - x - 1) / b : (y - x) / b;
    xp += d;
    cout << xp << endl;
    return 0;
}


651D - Image Preview

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<ll>;
template <typename InputIterator,
          typename T = typename iterator_traits<InputIterator>::value_type>
void read_n(InputIterator it, int n)
{
    copy_n(istream_iterator<T>(cin), n, it);
}
template <typename InputIterator,
          typename T = typename iterator_traits<InputIterator>::value_type>
void write(InputIterator first, InputIterator last, const char *delim = "\n")
{
    copy(first, last, ostream_iterator<T>(cout, delim));
}
using predicate = function<bool(ll)>;
ll n, a, b, t;
string s;
vi rot;
ll bs(ll l, ll r, predicate p)
{
    while (l < r)
    {
        ll m = l + (r - l) / 2;
        if (p(m))
            r = m;
        else
            l = m + 1;
    }
    if (not p(l))
        return -1;
    return l;
}
int main(void)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    cin >> n >> a >> b >> t;
    cin >> s;
    rot.resize(n);
    transform(begin(s), end(s), begin(rot), [](char c) { return b * (c == 'w'); });
    partial_sum(begin(rot), end(rot), begin(rot));
    ll ans = 0;
    for (ll i = 0; i < n; ++i)
    {
        ll solo = rot[i] + a * i + (i + 1);
        if (solo > t)
            break;
        ans = max(ans, i + 1);
        if (i < n - 1)
        {
            ll right = solo + a * i;
            if (right > t)
                continue;
            auto left_cost = [](ll ix) -> ll {
                return a * (n - ix) + (n - ix) + rot[n - 1] - rot[ix - 1];
            };
            auto ok = [right, left_cost](ll ix) {
                return right + left_cost(ix) <= t;
            };
            ll j = bs(i + 1, n - 1, ok);
            if (j == -1)
                continue;
            ans = max(ans, i + 1 + (n - j));
        }
    }
    cout << ans << endl;
    return 0;
}


651C - Watchmen

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
template <typename InputIterator,
          typename T = typename iterator_traits<InputIterator>::value_type>
void read_n(InputIterator it, int n)
{
    copy_n(istream_iterator<T>(cin), n, it);
}
template <typename InputIterator,
          typename T = typename iterator_traits<InputIterator>::value_type>
void write(InputIterator first, InputIterator last, const char *delim = "\n")
{
    copy(first, last, ostream_iterator<T>(cout, delim));
}
int main(void)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    auto fv = [](ii a, ii b) { return a.first < b.first; };
    auto sv = [](ii a, ii b) { return a.second < b.second; };
    int n;
    cin >> n;
    vector<ii> xy(n);
    for (auto &[x, y] : xy)
        cin >> x >> y;
    ll ans = 0;
    sort(begin(xy), end(xy), fv);
    int i = 0;
    while (i < n)
    {
        int j = i, cur = xy[i].first;
        while (j < n and cur == xy[j].first)
            ++j;
        ll k = j - i;
        ans += (k * (k - 1)) / 2;
        i = j;
    }
    i = 0;
    sort(begin(xy), end(xy), sv);
    while (i < n)
    {
        int j = i, cur = xy[i].second;
        while (j < n and cur == xy[j].second)
            ++j;
        ll k = j - i;
        ans += (k * (k - 1)) / 2;
        i = j;
    }
    i = 0;
    sort(begin(xy), end(xy));
    while (i < n)
    {
        int j = i;
        ii cur = xy[i];
        while (j < n and cur == xy[j])
            ++j;
        ll k = j - i;
        ans -= (k * (k - 1)) / 2;
        i = j;
    }
    cout << ans << endl;
    return 0;
}


651A - Joysticks

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
template <typename InputIterator,
          typename T = typename iterator_traits<InputIterator>::value_type>
void read_n(InputIterator it, int n)
{
    copy_n(istream_iterator<T>(cin), n, it);
}
template <typename InputIterator,
          typename T = typename iterator_traits<InputIterator>::value_type>
void write(InputIterator first, InputIterator last, const char *delim = "\n")
{
    copy(first, last, ostream_iterator<T>(cout, delim));
}
int main(void)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    int a1, a2;
    cin >> a1 >> a2;
    int t = 0;
    if (a1 < a2)
        swap(a1, a2);
    while (a1 != 0)
    {
        int delta = a1 % 2 == 0 ? (a1 - 1) / 2 : a1 / 2;
        if (delta == 0)
        {
            t += a1 == 2;
            break;
        }
        t += delta;
        a1 -= 2 * delta;
        a2 += delta;
        swap(a1, a2);
    }
    cout << t << endl;
    return 0;
}


PHONELST - Phone List

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
template <typename InputIterator,
          typename T = typename iterator_traits<InputIterator>::value_type>
void read_n(InputIterator it, int n)
{
    copy_n(istream_iterator<T>(cin), n, it);
}
template <typename InputIterator,
          typename T = typename iterator_traits<InputIterator>::value_type>
void write(InputIterator first, InputIterator last, const char *delim = "\n")
{
    copy(first, last, ostream_iterator<T>(cout, delim));
}
const int ALPH = 26;
const int TRIEMAX = 20;
struct Node
{
    bool finish, visited;
    array<Node *, ALPH> children;
    Node(void)
    {
        visited = finish = false;
        fill(children.begin(), children.end(), nullptr);
    }
    ~Node()
    {
        finish = visited = false;
        for (auto child : children)
            delete child;
    }
};
struct Trie
{
    Node *root;
    Trie() { root = new Node(); }
    ~Trie()
    {
        delete root;
        root = nullptr;
    }
    bool insert(string s)
    {
        auto cur = root;
        for (int i = 0, n = (int)s.size(); i < n; ++i)
        {
            int j = s[i] - '0';
            if (not cur->children[j])
                cur->children[j] = new Node();
            if (cur->finish)
                return false;
            cur->visited = true;
            cur = cur->children[j];
        }
        if (cur->visited)
            return false;
        return cur->visited = cur->finish = true;
    }
};
int main(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin >> t;
    while (t--)
    {
        Trie tree;
        int n;
        bool flag = true;
        cin >> n;
        while (n--)
        {
            string num;
            cin >> num;
            if (flag and not tree.insert(num))
                flag = false;
        }
        cout << (flag ? "YES" : "NO") << endl;
    }
}