1284A - Newyear Naming

Click to show code.


using namespace std;
int n, m;
string S[22];
string T[22];
string solve(int year)
{
    string ans;
    ans = S[(year - 1) % n] + T[(year - 1) % m];
    return ans;
}
int main(void)
{
    int q;
    cin >> n >> m;
    for (int i = 0; i < n; ++i)
       cin >> S[i];
    for (int i = 0; i < m; ++i)
       cin >> T[i];
    cin >> q;
    while (q--)
    {
        int year;
        cin >> year;
        cout << solve(year) << endl;
    }
    return 0;
}


1283D - Christmas Trees

Click to show code.


using namespace std;
using ll = long long;
using vi = vector<int>;
pair<ll, vi> msbfs(vi const &sources, int limit)
{
    vector<pair<int, int>> order;
    set<int> visited;
    queue<pair<int, int>> frontier;
    for (auto source : sources)
    {
        frontier.push({source, 0});
        visited.insert(source);
    }
    while ((int)order.size() < limit and not frontier.empty())
    {
        auto [i, dist] = frontier.front();
        frontier.pop();
        for (auto j : {i - 1, i + 1})
        {
            if (visited.find(j) == visited.end())
            {
                order.emplace_back(dist + 1, j);
                frontier.push({j, dist + 1});
                visited.insert(j);
                if ((int)order.size() == limit)
                    break;
            }
        }
    }
    ll ans = 0;
    vi x;
    for (auto [dist, i] : order)
    {
        ans += dist;
        x.push_back(i);
    }
    return {ans, x};
}
int main(void)
{
    int n, m;
    vi sources;
    cin >> n >> m;
    sources.resize(n);
    for (auto &source : sources)
        cin >> source;
    auto [ans, x] = msbfs(sources, m);
    cout << ans << endl;
    for_each(x.begin(), x.end(), [](int const &xi) { cout << xi << " "; }),
        cout << endl;
    return 0;
}


1282C - Petya And Exam

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<ll, ll>;
using vi = vector<ll>;
ll n, T, a, b;
vector<ii> time_cost;
ll get_extra(ll window, ll easy_left, ll hard_left)
{
    auto temp_extra = min(window / a, easy_left), extra = temp_extra;
    window -= a * temp_extra;
    temp_extra = min(window / b, hard_left), extra += temp_extra;
    window -= b * temp_extra;
    return extra;
}
int solve(void)
{
    ll ans(0), acc_cost(0), taken_prev = 0;
    ll easy_left =
        count_if(time_cost.begin(), time_cost.end(), [](ii tc) { return tc.second == a; });
    ll hard_left =
        count_if(time_cost.begin(), time_cost.end(), [](ii tc) { return tc.second == b; });
    sort(time_cost.begin(), time_cost.end());
    time_cost.emplace_back(T + 1, a);
    for (auto [t, c] : time_cost)
    {
        ll new_acc_cost = acc_cost + c, cur = 0;
        taken_prev = n - (easy_left + hard_left);
        if (acc_cost < t)
            cur = max(cur,
                      taken_prev + get_extra(max((t - 1) - acc_cost, 0LL),
                                             easy_left,
                                             hard_left));
        if (c == a)
            easy_left--;
        else
            hard_left--;
        acc_cost = new_acc_cost;
        ans = max(ans, cur);
    }
    return ans;
}
int main(void)
{
    int tc;
    cin >> tc;
    while (tc--)
    {
        cin >> n >> T >> a >> b;
        time_cost.resize(n);
        for (auto &[t, c] : time_cost)
            cin >> c, c = (c ? b : a);
        for (auto &[t, c] : time_cost)
            cin >> t;
        cout << solve() << endl;
    }
    return 0;
}


1282B2 - K For Price One

Click to show code.


using namespace std;
const int NMAX = 2e5 + 11;
int n, p, k, a[NMAX], dp[NMAX];
int solve(void)
{
    int ans = 0;
    dp[0] = 0;
    sort(a + 1, a + n + 1);
    for (int i = 1; i <= n; ++i)
    {
        dp[i] = a[i];
        if (i - k >= 0)
            dp[i] += dp[i - k];
        else
            dp[i] += dp[i - 1];
    }
    for (int i = 1; i <= n; ++i)
    {
        if (dp[i] <= p)
            ans = max(ans, i);
    }
    return ans;
}
int main(void)
{
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n >> p >> k;
        for (int i = 1; i <= n; ++i)
            cin >> a[i];
        cout << solve() << endl;
    }
    return 0;
}


1281C - Cut And Paste

Click to show code.


using namespace std;
using ll = long long;
int const MOD = 1e9 + 7;
inline int add(ll a, ll b) { return ((a % MOD) + (b % MOD)) % MOD; }
inline int mult(ll a, ll b) { return ((a % MOD) * (b % MOD)) % MOD; }
int solve(int x, string s)
{
    int n = s.size();
    for (int i = 1; i <= x; ++i)
    {
        int rep = s[i - 1] - '1';
        n = add(n, mult(n - i + MOD, rep));
        if ((int)s.size() <= x)
        {
            string sright(s.begin() + i, s.end());
            while (rep--)
                s += sright;
        }
    }
    return n;
}
int main(void)
{
    int t, x;
    string s;
    cin >> t;
    while (t--)
    {
        cin >> x >> s;
        cout << solve(x, s) << endl;
    }
    return 0;
}


1272E - Nearest Opposite Parity

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
int const NMAX = 2e5 + 11;
vi g[NMAX], gi[NMAX], odds, evens;
int n, a[NMAX], visited[NMAX], dp[NMAX];
vi msbfs(vi srcs)
{
    vector<int> visited(n + 1, false);
    queue<int> frontier;
    vector<int> dist(n + 1, -1);
    for (auto x : srcs)
    {
        dist[x] = 0;
        frontier.push(x);
        visited[x] = true;
    }
    while (not frontier.empty())
    {
        auto u = frontier.front();
        frontier.pop();
        for (auto v : gi[u])
        {
            if (not visited[v])
            {
                frontier.push(v);
                visited[v] = true;
                dist[v] = dist[u] + 1;
            }
        }
    }
    return dist;
}
int main(void)
{
    cin >> n;
    for (int u = 1; u <= n; ++u)
        cin >> a[u];
    for (int u = 1; u <= n; ++u)
    {
        int l = u - a[u], r = u + a[u];
        if (l >= 1)
            g[u].push_back(l), gi[l].push_back(u);
        if (r <= n)
            g[u].push_back(r), gi[r].push_back(u);
        a[u] &= 1LL;
        if (a[u])
            odds.push_back(u);
        else
            evens.push_back(u);
    }
    vi ans(n + 1);
    auto d1 = msbfs(odds);
    for (int u = 1; u <= n; ++u)
        if (d1[u] != 0)
            ans[u] = d1[u];
    auto d2 = msbfs(evens);
    for (int u = 1; u <= n; ++u)
        if (d2[u] != 0)
            ans[u] = d2[u];
    for (int u = 1; u <= n; ++u)
        cout << ans[u] << " ";
    cout << endl;
    return 0;
}


1271A - Suits

Click to show code.


using namespace std;
int main(void) {
  int a, b, c, d, e, f, ne, nf;
  cin >> a >> b >> c >> d >> e >> f;
  auto ex = [&]() {
    ne = min(a, d);
    d -= ne;
  };
  auto fx = [&]() {
    nf = min({b, c, d});
    d -= nf;
  };
  if (e >= f) {
    ex();
    fx();
  } else {
    fx();
    ex();
  }
  cout << e * ne + f * nf << endl;
  return 0;
}


1258C - Celex Update

Click to show code.


using namespace std;
using ll = long long;
int main(void)
{
    ll t, x1, y1, x2, y2;
    cin >> t;
    while (t--)
    {
        cin >> x1 >> y1 >> x2 >> y2;
        cout << (x2 - x1) * (y2 - y1) + 1 << endl;
    }
    return 0;
}


1258B - Maria Self Isolation

Click to show code.


using namespace std;
const int NMAX = 1e5 + 11;
int n, a[NMAX];
int solve(void)
{
    int grannies = 0;
    bool found = false;
    sort(a, a + n);
    for (int i = n - 1; i >= 0; --i)
    {
        grannies = i + 1;
        if (grannies >= a[i])
        {
            found = true;
            break;
        }
    }
    return grannies + found;
}
int main(void)
{
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n;
        for (int i = 0; i < n; ++i)
            cin >> a[i];
        cout << solve() << endl;
    }
    return 0;
}


1253D - Harmonious Graph

Click to show code.


using namespace std;
using ll = long long;
using vi = vector<int>;
int const NMAX = 2e5 + 11;
int n, m;
vi g[NMAX];
set<int> edges;
int dfs(int u, vector<bool> &visited)
{
    int maxv = u;
    visited[u] = true;
    for (auto v : g[u])
        if (not visited[v])
            maxv = max(maxv, dfs(v, visited));
    return maxv;
}
int solve(void)
{
    int ans = 0;
    vector<bool> visited(n + 1, false);
    while (not edges.empty())
    {
        int u = *edges.begin(), v = dfs(u, visited);
        for (int i = u; i <= v; ++i)
        {
            if (not visited[i])
            {
                g[u].push_back(i);
                g[i].push_back(u);
                v = max(v, dfs(i, visited));
                ++ans;
            }
            edges.erase(i);
        }
    }
    return ans;
}
int main(void)
{
    cin >> n >> m;
    for (int i = 0; i < m; ++i)
    {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
        edges.insert(u), edges.insert(v);
    }
    cout << solve() << endl;
    return 0;
}