78A - Haiku

Click to show code.


using namespace std;
inline bool is_vowel(char c)
{
    return c == 'a' or c == 'e' or c == 'i' or c == 'o' or c == 'u';
}
int count(string line)
{
    int ans = 0;
    string word;
    stringstream ss(line);
    while (ss >> word)
    {
        for (auto c : word)
        {
            if (is_vowel(c))
                ++ans;
        }
    }
    return ans;
}
int main(void)
{
    int line_number = 0, syllabes_count;
    int expected_syl[3] = {5, 7, 5};
    string line;
    while (line_number < 3)
    {
        getline(cin, line);
        syllabes_count = count(line);
        if (expected_syl[line_number] != syllabes_count)
        {
            cout << "NO" << endl;
            return 0;
        }
        line_number++;
    }
    cout << "YES" << endl;
    return 0;
}


779D - String Game

Click to show code.


using namespace std;
using vi = vector<int>;
using predicate = function<bool(int)>;
int n, m;
vi a;
string s, t;
bool ok(int q)
{
    vector<bool> usable(n, true);
    for (int i = 0; i < q; ++i)
        usable[a[i]] = false;
    int j = 0;
    for (int i = 0; i < n and j < m; ++i)
        if (usable[i] and t[j] == s[i])
            ++j;
    return j == m;
}
int bs(int l, int r, predicate p)
{
    while (l < r)
    {
        int m = l + (r - l + 1) / 2;
        if (p(m))
            l = m;
        else
            r = m - 1;
    }
    return l;
}
int main(void)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    cin >> s >> t;
    n = (int)s.size(), m = (int)t.size();
    a.resize(n);
    for (auto &ai : a)
        cin >> ai, ai--;
    cout << bs(0, n, ok) << endl;
    return 0;
}


761C - Dasha And Password

Click to show code.


using namespace std;
using ll = long long;
const int NMAX = 50 + 11;
const int MMAX = 50 + 11;
int n, m;
int mem[NMAX][3];
int solve(void)
{
    ll ans = 3 * MMAX;
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < n; ++j)
        {
            if (i == j)
                continue;
            for (int k = 0; k < n; ++k)
            {
                if (k == j or k == i)
                    continue;
                ans = min(ans, (ll)(mem[i][0] + mem[j][1] + mem[k][2]));
            }
        }
    }
    return ans;
}
int main(void)
{
    string s;
    cin >> n >> m;
    for (int i = 0; i < n; ++i)
        mem[i][0] = mem[i][1] = mem[i][2] = MMAX;
    for (int i = 0; i < n; ++i)
    {
        cin >> s;
        for (int j = 0; j < m; ++j)
        {
            if (isdigit(s[j]))
                mem[i][0] = min(mem[i][0], min(j, m - j));
            else if (isalpha(s[j]))
                mem[i][1] = min(mem[i][1], min(j, m - j));
            else
                mem[i][2] = min(mem[i][2], min(j, m - j));
        }
    }
    cout << solve() << endl;
    return 0;
}


753A - Santa Claus Candies

Click to show code.


using namespace std;
int main(void)
{
    int n, i;
    int sum = 0;
    cin >> n;
    for (i = 1; sum + i <= n and (n - sum - i) > i; ++i)
        sum += i;
    cout << i - 1 + (sum != n) << endl;
    for (int j = 1; j < i; ++j)
        cout << j << " ";
    if (sum != n)
        cout << n - sum << endl;
    return 0;
}


749C - Voting

Click to show code.


using namespace std;
using ll = long long;
const int NMAX = 2 * 10e5 + 11;
int n;
ll high;
char s[NMAX];
deque<ll> D, R;
int main(void)
{
    cin >> n;
    cin >> s;
    for (int i = 0; i < n; ++i)
    {
        if (s[i] == 'D')
            D.push_back(i);
        else
            R.push_back(i);
    }
    high = n;
    while (D.size() and R.size())
    {
        ll fd = D.front();
        D.pop_front();
        ll fr = R.front();
        R.pop_front();
        if (fd < fr)
            D.push_back(high++);
        else
            R.push_back(high++);
    }
    if (D.size())
        cout << "D" << endl;
    else
        cout << "R" << endl;
    return 0;
}


741B - Arpa Weak Amphitheater Valuable Hoses

Click to show code.


using namespace std;
using vi = vector<int>;
int const NMAX = 1e3 + 11;
int n, m, c, w[NMAX], b[NMAX], rep[NMAX], cur_rep;
bool visited[NMAX];
vi g[NMAX], component;
inline pair<int, int> add_pair(pair<int, int> a, pair<int, int> b)
{
    return make_pair(a.first + b.first, a.second + b.second);
}
pair<int, int> dfs(int u, int p = -1)
{
    pair<int, int> tbw = {b[u], w[u]};
    rep[u] = cur_rep;
    visited[u] = true;
    for (auto v : g[u])
    {
        if (v == p or visited[v])
            continue;
        tbw = add_pair(dfs(v, u), tbw);
    }
    component.push_back(u);
    return tbw;
}
int solve(void)
{
    vector<vi> dp(n + 1, vi(c + 1, 0));
    for (int u = 1; u <= n; ++u)
    {
        if (visited[u])
            continue;
        ++cur_rep;
        auto [tb, tw] = dfs(u);
        for (int cap = 0; cap <= c; ++cap)
        {
            auto &ans = dp[cur_rep][cap];
            ans = max(ans, dp[cur_rep - 1][cap]);
            if (cap - tw >= 0)
                ans = max(ans, dp[cur_rep - 1][cap - tw] + tb);
            for (auto v : component)
                if (cap - w[v] >= 0)
                    ans = max(ans, dp[cur_rep - 1][cap - w[v]] + b[v]);
        }
        component.clear();
    }
    return dp[cur_rep][c];
}
int main(void)
{
    ios_base::sync_with_stdio(false), cin.tie(NULL);
    int u, v;
    cin >> n >> m >> c;
    for (int i = 1; i <= n; ++i)
        cin >> w[i];
    for (int i = 1; i <= n; ++i)
        cin >> b[i];
    for (int i = 0; i < m; ++i)
    {
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    cout << solve() << endl;
    return 0;
}


734A - Anton Danik

Click to show code.


using namespace std;
int main(void)
{
    int n, a, d;
    string s;
    (void)n;
    a = d = 0;
    cin >> n;
    cin >> s;
    for (auto c : s)
    {
        if (c == 'A')
            ++a;
        else
            ++d;
    }
    if (a == d)
        cout << "Friendship" << endl;
    else if (a > d)
        cout << "Anton" << endl;
    else
        cout << "Danik" << endl;
    return 0;
}


732A - Buy Shovel

Click to show code.


using namespace std;
int solve(int k, int r)
{
    int rem;
    for (int i = 1; i < 10; ++i)
    {
        rem = (i * k) % 10;
        if (rem == 0 or rem == r)
            return i;
    }
    return 10;
}
int main(void)
{
    int k, r;
    cin >> k >> r;
    cout << solve(k, r) << endl;
    return 0;
}


731A - Night Museum

Click to show code.


using namespace std;
inline int ord(char c) { return c - 'a'; }
int main(void)
{
    int ans = 0, last = 0;
    string s;
    cin >> s;
    for (auto c : s)
    {
        ans += min(26 - abs(ord(c) - last), abs(ord(c) - last));
        last = ord(c);
    }
    cout << ans << endl;
    return 0;
}


716A - Crazy Computer

Click to show code.


using namespace std;
const int NMAX = 1e5 + 11;
int a[NMAX];
int main(void)
{
    int n, c, ans = 1;
    cin >> n >> c;
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    for (int i = 1; i < n; ++i)
    {
        ++ans;
        if (a[i] - a[i - 1] > c)
            ans = 1;
    }
    cout << ans << endl;
    return 0;
}