Suggestions logoSuggestions

Answers

Detailed solutions and code implementations for Competitive Programming.

1. 1703B — ICPC Balloons

#include <bits/stdc++.h>
using namespace std;

void solve()
{
    int n;
    string s;
    cin >> n >> s;

    vector<bool> seen(26, false);
    int ans = 0;

    for (char c : s)
    {
        int x = c - 'A';

        if (!seen[x])
        {
            seen[x] = true;
            ans += 2;
        }
        else
            ans++;
    }

    cout << ans << '\n';
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;

    while (t--)
        solve();

    return 0;
}

2. 1343B — Balanced Array

#include <bits/stdc++.h>
using namespace std;

void solve()
{
    int n;
    cin >> n;

    int half = n / 2;

    if (half % 2 != 0)
    {
        cout << "NO\n";
        return;
    }

    cout << "YES\n";

    long long evenSum = 0, oddSum = 0;

    for (int i = 1; i <= half; i++)
    {
        int x = 2 * i;
        cout << x << ' ';
        evenSum += x;
    }

    for (int i = 1; i < half; i++)
    {
        int x = 2 * i - 1;
        cout << x << ' ';
        oddSum += x;
    }

    cout << evenSum - oddSum << '\n';
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;

    while (t--)
        solve();

    return 0;
}

3. 750A — New Year and Hurry

#include <bits/stdc++.h>
using namespace std;

void solve()
{
    int n, k;
    cin >> n >> k;

    int left = 240 - k;
    int used = 0, solved = 0;

    for (int i = 1; i <= n; i++)
    {
        used += 5 * i;

        if (used <= left)
            solved++;
        else
            break;
    }

    cout << solved << '\n';
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    solve();

    return 0;
}

4. 1915C — Can I Square?

#include <bits/stdc++.h>
using namespace std;

void solve()
{
    int n;
    cin >> n;

    long long totalSquares = 0;

    while (n--)
    {
        long long x;
        cin >> x;
        totalSquares += x;
    }

    long long squareRoot = sqrtl(totalSquares);

    if (squareRoot * squareRoot == totalSquares)
        cout << "YES\n";
    else
        cout << "NO\n";
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;

    while (t--)
        solve();

    return 0;
}

5. 2008C — Longest Good Array

#include <bits/stdc++.h>
using namespace std;

void solve()
{
    long long l, r;
    cin >> l >> r;

    long long k = floor((1 + sqrt(1 + 8 * (r - l))) / 2);
    cout << k << '\n';
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;

    while (t--)
        solve();

    return 0;
}

6. 34B — Sale

#include <bits/stdc++.h>
using namespace std;

void solve()
{
    int n, m;
    cin >> n >> m;

    vector<int> arr(n);

    for (int &x : arr)
        cin >> x;

    sort(arr.begin(), arr.end());

    int earned = 0;

    for (int i = 0; i < m; i++)
        if (arr[i] < 0)
            earned += -arr[i];

    cout << earned << '\n';
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    solve();

    return 0;
}

7. 546A — Soldier and Bananas

#include <bits/stdc++.h>
using namespace std;

void solve()
{
    long long k, n, w;
    cin >> k >> n >> w;

    long long cost = k * (w * (w + 1) / 2);
    long long borrow = max(0LL, cost - n);

    cout << borrow << '\n';
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    solve();

    return 0;
}

8. 1956A — Nene’s Game

#include <bits/stdc++.h>
using namespace std;

void solve()
{
    int k, q;
    cin >> k >> q;

    int firstPosition;
    cin >> firstPosition;

    for (int i = 1; i < k; i++)
    {
        int x;
        cin >> x;
    }

    int maxWinners = firstPosition - 1;

    for (int i = 0; i < q; i++)
    {
        int players;
        cin >> players;

        cout << min(players, maxWinners) << (i + 1 == q ? '\n' : ' ');
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;

    while (t--)
        solve();

    return 0;
}

9. 1665A — GCD vs LCM

#include <bits/stdc++.h>
using namespace std;

void solve()
{
    long long n;
    cin >> n;

    cout << 1 << ' ' << n - 3 << ' ' << 1 << ' ' << 1 << '\n';
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;

    while (t--)
        solve();

    return 0;
}

10. 1165B — Polycarp Training

#include <bits/stdc++.h>
using namespace std;

void solve()
{
    int n;
    cin >> n;

    vector<int> arr(n);

    for (int &x : arr)
        cin >> x;

    sort(arr.begin(), arr.end());

    int day = 0;

    for (int x : arr)
        if (x > day)
            day++;

    cout << day << '\n';
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    solve();

    return 0;
}

11. 71A — Way Too Long Words

#include <bits/stdc++.h>
using namespace std;

void solve()
{
    string s;
    cin >> s;

    if (s.size() <= 10)
        cout << s << '\n';
    else
        cout << s[0] << s.size() - 2 << s.back() << '\n';
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;

    while (t--)
        solve();

    return 0;
}

12. 242B — Big Segment

#include <bits/stdc++.h>
using namespace std;

void solve()
{
    int n;
    cin >> n;

    vector<int> l(n), r(n);
    int mn = INT_MAX, mx = INT_MIN;

    for (int i = 0; i < n; i++)
    {
        cin >> l[i] >> r[i];
        mn = min(mn, l[i]);
        mx = max(mx, r[i]);
    }

    for (int i = 0; i < n; i++)
    {
        if (l[i] == mn && r[i] == mx)
        {
            cout << i + 1 << '\n';
            return;
        }
    }

    cout << -1 << '\n';
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    solve();

    return 0;
}

On this page