702A - Maximum Increase

Click to show code.


using namespace std;
int n;
int a[100010];
int solve(void)
{
    int ans = 1;
    int cur = 1;
    for (int i = 1; i < n; ++i)
    {
        if (a[i] > a[i - 1])
            ++cur;
        else
            cur = 1;
        ans = max(ans, cur);
    }
    return ans;
}
int main(void)
{
    cin >> n;
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    cout << solve() << endl;
    return 0;
}


691D - Swaps In Permutation

Click to show code.


using namespace std;
using vi = vector<int>;
struct dsu
{
    int n;
    vi parent;
    vi sz;
    dsu(int n) : n(n)
    {
        parent.resize(n);
        sz.resize(n);
        for (int i = 0; i < n; ++i)
        {
            parent[i] = i;
            sz[i] = 0;
        }
    }
    void merge_set(int u, int v)
    {
        u = find_set(u);
        v = find_set(v);
        if (u != v)
        {
            if (sz[u] < sz[v])
                swap(u, v);
            parent[v] = u;
            sz[u] += sz[v];
        }
    }
    int find_set(int u)
    {
        if (u == parent[u])
            return u;
        return parent[u] = find_set(parent[u]);
    }
};
int main(void)
{
    int n, m, u, v;
    vi p;
    cin >> n >> m;
    p.resize(n);
    dsu d(n);
    for (auto &pi : p)
        cin >> pi;
    for (int i = 0; i < m; ++i)
    {
        cin >> u >> v, u--, v--;
        d.merge_set(u, v);
    }
    vector<set<int, greater<int>>> sorted(n);
    for (int i = 0; i < n; ++i)
        sorted[d.find_set(i)].insert(p[i]);
    for (int i = 0; i < n; ++i)
    {
        int p = d.find_set(i);
        cout << *sorted[p].begin() << " ";
        sorted[p].erase(sorted[p].begin());
    }
    return 0;
}


677A - Vanya Fence

Click to show code.


using namespace std;
int main(void)
{
    int n, h, ai, ans = 0;
    cin >> n >> h;
    for (int i = 0; i < n; ++i)
    {
        cin >> ai;
        ans += 1 + (ai > h);
    }
    cout << ans;
    return 0;
}


669A - Magical Boxes

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, ll>;
using vii = vector<ii>;
inline ll iceil(ll a, ll b) { return (a + b - 1) / b; }
int main(void)
{
    int n;
    vii a;
    cin >> n;
    a.resize(n);
    for (auto &[k, c] : a)
        cin >> k >> c;
    sort(a.begin(), a.end());
    for (int i = 1; i < n; ++i)
    {
        int dk = a[i].first - a[i - 1].first;
        if (dk < 15)
            a[i].second =
                max(a[i].second, iceil(a[i - 1].second, (1LL << (2 * dk))));
    }
    do
    {
        auto [k, c] = a.back();
        a.emplace_back(k + 1, iceil(c, (1LL << 2)));
    } while (a.back().second != 1);
    cout << a.back().first << endl;
    return 0;
}


665D - Simple Subset

Click to show code.


using namespace std;
using ll = long long;
using vi = vector<int>;
int const AMAX = 2e6 + 11;
bitset<AMAX> is_prime;
void sieve()
{
    is_prime.set();
    is_prime[0] = is_prime[1] = 0;
    for (ll i = 2; i < AMAX; ++i)
        if (is_prime[i])
            for (ll j = i * i; j < AMAX; j += i)
                is_prime[j] = 0;
}
int main(void)
{
    int n;
    vi a, ans;
    cin >> n;
    a.resize(n);
    for (auto &ai : a)
        cin >> ai;
    sort(a.begin(), a.end());
    sieve();
    if (a[0] == 1)
    {
        auto it = upper_bound(a.begin(), a.end(), 1);
        int ones = distance(a.begin(), it);
        int x = -1;
        for (; it != a.end(); ++it)
        {
            if (is_prime[*it + 1])
            {
                x = *it;
                break;
            }
        }
        ans = vi(a.begin(), a.begin() + ones);
        if (x != -1)
            ans.push_back(x);
    }
    if ((int)ans.size() < 2)
    {
        for (int i = 0; i < n - 1; ++i)
        {
            for (int j = i + 1; j < n; ++j)
            {
                if (is_prime[a[i] + a[j]])
                {
                    ans.clear();
                    ans.push_back(a[i]), ans.push_back(a[j]);
                    break;
                }
            }
            if ((int)ans.size() == 2)
                break;
        }
    }
    if ((int)ans.size() < 1)
        ans = {a[0]};
    cout << ans.size() << endl;
    for (auto x : ans)
        cout << x << " ";
    cout << endl;
    return 0;
}


665C - Simple Strings

Click to show code.


using namespace std;
string s;
char anything_but(char c, char d)
{
    for (char x = 'a'; x <= 'z'; ++x)
        if (x != c and x != d)
            return x;
    return 0;
}
string solve(void)
{
    int n = s.size();
    char last = s[0];
    for (int i = 1; i < n - 1; ++i)
    {
        if (s[i] == last)
            s[i] = anything_but(last, s[i + 1]);
        last = s[i];
    }
    if (n != 1 and s[n - 1] == last)
        s[n - 1] = anything_but(last, last);
    return s;
}
int main(void)
{
    cin >> s;
    cout << solve() << 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;
}


628 - Passwords

Click to show code.


using namespace std;
using vs = vector<string>;
int n, m;
vs dictionary;
string cpattern;
void match(int pos, string ans)
{
    if (pos == cpattern.size())
    {
        cout << ans << endl;
        return;
    }
    if (cpattern[pos] == '0')
    {
        for (int i = 0; i < 10; ++i)
            match(pos + 1, ans + to_string(i));
    }
    else
    {
        for (int i = 0; i < n; ++i)
            match(pos + 1, ans + dictionary[i]);
    }
}
int main(void)
{
    string word;
    string pattern;
    while (cin >> n)
    {
        for (int i = 0; i < n; ++i)
        {
            cin >> word;
            dictionary.push_back(word);
        }
        cin >> m;
        cout << "--" << endl;
        for (int j = 0; j < m; ++j)
        {
            cin >> pattern;
            cpattern = pattern;
            match(0, "");
        }
        dictionary = vs();
    }
    return 0;
}