918D - Madmax

Click to show code.


using namespace std;
using ii = pair<int, int>;
int const NMAX = 1e2 + 11;
int const CMAX = 26;
int n, m;
bool mem[NMAX][NMAX][CMAX], vis[NMAX][NMAX][CMAX];
vector<ii> g[NMAX];
bool dp(int u, int v, int c)
{
    auto &ans = mem[u][v][c];
    if (vis[u][v][c])
        return ans;
    else
    {
        vis[u][v][c] = true;
        return ans = any_of(g[u].begin(), g[u].end(), [c, v](ii uc) {
                   auto [up, cp] = uc;
                   return cp >= c and not dp(v, up, cp);
               });
    }
}
int main(void)
{
    ios_base::sync_with_stdio(false), cin.tie(NULL);
    int u, v;
    char c;
    cin >> n >> m;
    for (int i = 0; i < m; ++i)
    {
        cin >> u >> v >> c, --u, --v, c -= 'a';
        g[u].emplace_back(v, (int)c);
    }
    for (int u = 0; u < n; ++u)
    {
        for (int v = 0; v < n; ++v)
            cout << (dp(u, v, 0) ? 'A' : 'B');
        cout << endl;
    }
    return 0;
}


918B - Radio Station

Click to show code.


using namespace std;
string line;
int pos;
map<int, string> ip_to_name;
bool is_char(char c) { return 'a' <= c and c <= 'z'; }
bool is_digit(char c) { return '0' <= c and c <= '9'; }
string parse_name(void)
{
    string name;
    while (is_char(line[pos]))
    {
        name += line[pos];
        ++pos;
    }
    ++pos;
    return name;
}
int parse_ip(void)
{
    int i = 0, ip[4], ans = 0;
    string cnum;
    while (is_digit(line[pos]) or line[pos] == '.')
    {
        if (line[pos] == '.')
        {
            ip[i] = stoi(cnum);
            ++i;
            cnum = string();
        }
        else
            cnum += line[pos];
        ++pos;
    }
    ip[i] = stoi(cnum);
    for (int i = 0; i < 4; ++i)
        ans += ip[i] * pow(256, 4 - (i + 1));
    return ans;
}
int main(void)
{
    string name;
    int n, m, ip;
    cin >> n >> m;
    cin.ignore();
    for (int i = 0; i < n; ++i)
    {
        pos = 0;
        getline(cin, line);
        name = parse_name();
        ip = parse_ip();
        ip_to_name[ip] = name;
    }
    for (int i = 0; i < m; ++i)
    {
        pos = 0;
        getline(cin, line);
        name = parse_name();
        ip = parse_ip();
        cout << line << " #" << ip_to_name[ip] << endl;
    }
    return 0;
}


907 - Winterim Backpacking Trip

Click to show code.


using namespace std;
const int NMAX = 600 + 11;
int n, k, dist[NMAX];
bool simulate(int max_dist)
{
    int dist_left = max_dist, stops = 0;
    for (int i = 0; i < n + 1; ++i)
    {
        if (dist_left >= dist[i])
            dist_left -= dist[i];
        else
        {
            ++stops;
            dist_left = max_dist - dist[i];
        }
        if (stops > k or dist_left < 0)
            return false;
    }
    return true;
}
int binary_search(int l, int r)
{
    while (l < r)
    {
        int mid = (l + r) / 2;
        if (simulate(mid))
            r = mid;
        else
            l = mid + 1;
    }
    return l;
}
int main(void)
{
    int sum;
    while (cin >> n >> k)
    {
        sum = 0;
        for (int i = 0; i < n + 1; ++i)
        {
            cin >> dist[i];
            sum += dist[i];
        }
        cout << binary_search(0, sum) << endl;
    }
    return 0;
}


898E - Squares Not Squares

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
int main(void)
{
    int n;
    cin >> n;
    vector<ii> diff;
    for (int i = 0; i < n; ++i)
    {
        ll ai, sq;
        cin >> ai;
        sq = round(sqrt(ai));
        diff.emplace_back(abs(ai - sq * sq), ai);
    }
    sort(diff.begin(), diff.end());
    ll ans = 0;
    for (int i = 0; i < n / 2; ++i)
        ans += diff[i].first;
    for (int i = n / 2; i < n; ++i)
        ans += (diff[i].first != 0 ? 0 : 1 + !diff[i].second);
    cout << ans << endl;
    return 0;
}


873B - Balanced Substring

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
int solve1(vi a)
{
    int n = (int)a.size(), ans = 0;
    for (int l = 0; l < n - 1; ++l)
        for (int r = l + 1; r < n; ++r)
            if (accumulate(a.begin() + l, a.begin() + r + 1, 0) == 0)
                ans = max(ans, (r - l + 1));
    return ans;
}
vi prefix_sum(vi a)
{
    int n = (int)a.size();
    vi pa(n);
    for (int i = 0; i < n; ++i)
        pa[i] = a[i] + (i == 0 ? 0 : pa[i - 1]);
    return pa;
}
int solve2(vi a)
{
    int n = (int)a.size(), ans = 0;
    vi pa = prefix_sum(a);
    auto sum = [&pa](int l, int r) { return pa[r] - (l == 0 ? 0 : pa[l - 1]); };
    for (int l = 0; l < n - 1; ++l)
        for (int r = l + 1; r < n; ++r)
            if (sum(l, r) == 0)
                ans = max(ans, (r - l + 1));
    return ans;
}
int solve3(vi a)
{
    int n = (int)a.size(), ans = 0;
    map<int, int> sum_ix;
    sum_ix[0] = -1;
    int ps = 0;
    for (int i = 0; i < n; ++i)
    {
        ps += a[i];
        if (sum_ix.find(ps) == sum_ix.end())
            sum_ix[ps] = i;
        else
            ans = max(ans, i - sum_ix[ps]);
    }
    return ans;
}
int main(void)
{
    int n;
    string s;
    cin >> n >> s;
    vi a(n);
    transform(s.begin(), s.end(), a.begin(), [](char c) { return c == '0' ? -1 : +1; });
    cout << solve3(a) << endl;
    return 0;
}


855A - Tom Riddle Diary

Click to show code.


using namespace std;
map<string, bool> exists;
int main(void)
{
    int n;
    string s;
    cin >> n;
    while (n--)
    {
        cin >> s;
        if (exists[s])
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
        exists[s] = true;
    }
    return 0;
}


850B - Arpa List Numbers

Click to show code.


const int TAMAX = 2e6 + 22;
using namespace std;
using ll = long long;
using vll = vector<ll>;
ll n, x, y;
vll a, acc_sum(TAMAX, 0), acc_cnt(TAMAX, 0);
inline ll cnt(int l, int r)
{
    if (r < l)
        return 0;
    return acc_cnt[r] - (l <= 0 ? 0 : acc_cnt[l - 1]);
}
inline ll sum(int l, int r)
{
    if (r < l)
        return 0;
    return acc_sum[r] - (l <= 0 ? 0 : acc_sum[l - 1]);
}
ll cost(ll p)
{
    ll local_ans = 0, local_inc_cost, local_del_cost;
    ll rel_border = min(p - 1, x / y);
    for (ll j = p; j < TAMAX; j += p)
    {
        ll border = j - rel_border;
        local_del_cost = x * cnt(j - p + 1, border - 1);
        local_inc_cost = y * (j * cnt(border, j - 1) - sum(border, j - 1));
        local_ans += local_inc_cost + local_del_cost;
    }
    return local_ans;
}
ll solve(void)
{
    ll ans = x * n;
    for (ll p = 2; p < TAMAX; ++p)
        ans = min(ans, cost(p));
    return ans;
}
int main(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> x >> y;
    a = vll(n);
    for (auto &x : a)
    {
        cin >> x;
        ++acc_cnt[x];
        acc_sum[x] += x;
    }
    for (int i = 1; i < TAMAX; ++i)
    {
        acc_cnt[i] += acc_cnt[i - 1];
        acc_sum[i] += acc_sum[i - 1];
    }
    cout << solve() << endl;
    return 0;
}


812C - Sagheer Nubian Market

Click to show code.


using namespace std;
using ll = long long;
const int NMAX = 10e5 + 11;
ll n, S;
ll a[NMAX], ka[NMAX];
ll compute(ll k)
{
    ll ans = 0;
    for (int i = 0; i < n; ++i)
        ka[i] = a[i] + (i + 1) * k;
    sort(ka, ka + n);
    for (int i = 0; i < k; ++i)
        ans += ka[i];
    return ans;
}
int binary_search(ll low, ll high, ll target)
{
    auto p = [&](ll k) { return compute(k + 1) > target; };
    while (low < high)
    {
        ll mid = low + (high - low + 1) / 2;
        if (p(mid))
            high = mid - 1;
        else
            low = mid;
    }
    if (p(low))
        return -1;
    return low;
}
int main(void)
{
    cin >> n >> S;
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    int index = binary_search(0, n - 1, S);
    cout << index + 1 << " " << (index == -1 ? 0 : compute(index + 1)) << endl;
    return 0;
}


792B - Counting Out Rhyme

Click to show code.


using namespace std;
using vi = vector<int>;
void print(vi v)
{
    for (auto x : v)
        cout << x << " ";
    cout << endl;
}
int main(void)
{
    int n, k, ai;
    cin >> n >> k;
    vi children(n);
    for (int i = 0; i < n; ++i)
        children[i] = i;
    int pos = 0, killed;
    while (k--)
    {
        cin >> ai;
        killed = (pos + ai % children.size()) % children.size();
        cout << children[killed] + 1 << " ";
        children.erase(children.begin() + killed);
        pos = killed % (children.size());
    }
    cout << endl;
}


791A - Bear Big Brother

Click to show code.


using namespace std;
int main(void)
{
    int a, b, ans = 0;
    cin >> a >> b;
    while (a <= b)
    {
        a *= 3;
        b *= 2;
        ++ans;
    }
    cout << ans << endl;
    return 0;
}