1391A - Suborrays

Click to show code.


using namespace std;
int main(void)
{
    int t, n;
    cin >> t;
    while (t--)
    {
        cin >> n;
        for (int i = 1; i <= n; ++i)
            cout << i << " ";
        cout << endl;
    }
    return 0;
}


1389A - Lcm Problem

Click to show code.


using namespace std;
using ii = pair<int, int>;
ii solve(int l, int r)
{
    if (2 * l <= r)
        return {l, 2 * l};
    else
        return {-1, -1};
}
int main(void)
{
    int t, l, r;
    cin >> t;
    while (t--)
    {
        cin >> l >> r;
        auto [a, b] = solve(l, r);
        cout << a << " " << b << endl;
    }
    return 0;
}


1385D - A Good String

Click to show code.


using namespace std;
int n;
string s;
int cost(int l, int r, char c)
{
    return count_if(
        s.begin() + l, s.begin() + r + 1, [c](char d) { return d != c; });
}
int solve(int l, int r, char c)
{
    if (r - l == 0)
        return c != s[l];
    else
    {
        int m = l + (r - l) / 2;
        return min(cost(l, m, c) + solve(m + 1, r, c + 1),
                   solve(l, m, c + 1) + cost(m + 1, r, c));
    }
}
int main(void)
{
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n >> s;
        cout << solve(0, n - 1, 'a') << endl;
    }
    return 0;
}


1385B - Restore Permutation Merger

Click to show code.


using namespace std;
using vi = vector<int>;
int main(void)
{
    int t, n, ai;
    vi cnt;
    cin >> t;
    while (t--)
    {
        cin >> n;
        cnt.assign(n + 1, 0);
        n *= 2;
        while (n--)
        {
            cin >> ai;
            cnt[ai]++;
            if (cnt[ai] == 2)
                cout << ai << " ";
        }
        cout << endl;
    }
    return 0;
}


1374E1 - Reading Books

Click to show code.


using namespace std;
using ll = long long;
using vi = vector<int>;
using ii = pair<int, int>;
int solve(int k, vector<ii> tab)
{
    sort(tab.begin(), tab.end());
    vector<int> choices;
    queue<int> queued[2];
    for (auto [t, sign] : tab)
    {
        if (sign == 2)
            choices.push_back(t);
        else
        {
            queued[sign].push(t);
            if (not queued[0].empty() and not queued[1].empty())
            {
                choices.push_back(queued[0].front() + queued[1].front());
                queued[0].pop(), queued[1].pop();
            }
        }
    }
    sort(choices.begin(), choices.end());
    if ((int)choices.size() < k)
        return -1;
    else
        return accumulate(choices.begin(), choices.begin() + k, 0);
}
int main(void)
{
    int n, k;
    cin >> n >> k;
    vector<ii> tab;
    for (int i = 0; i < n; ++i)
    {
        int ti, ai, bi;
        cin >> ti >> ai >> bi;
        if (not ai and not bi)
            continue;
        else
            tab.push_back({ti, (ai and bi ? 2 : ai)});
    }
    cout << solve(k, tab) << endl;
    return 0;
}


1370C - Number Game

Click to show code.


using namespace std;
using ll = long long;
bool is_prime(int n)
{
    if (n < 2)
        return false;
    for (int x = 2; x * x <= n; x++)
    {
        if (n % x == 0)
            return false;
    }
    return true;
};
int main(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t, n, m;
    cin >> t;
    while (t--)
    {
        cin >> n;
        m = 1;
        if (n % 2 == 1 and n != 1)
            m = 0;
        else if (n == 2)
            m = 0;
        else
        {
            int cnt = 0;
            while (n % 2 == 0)
            {
                n /= 2;
                ++cnt;
            }
            if ((n != 1 and cnt > 1) or (cnt == 1 && !is_prime(n)))
                m = 0;
        }
        cout << (m == 0 ? "Ashishgup" : "FastestFinger") << endl;
    }
    return 0;
}


1370B - Gcd Compression

Click to show code.


using namespace std;
using vi = vector<int>;
const int NMAX = 1e3 + 11;
int n, a[2 * NMAX];
void solve(void)
{
    vi rem[2];
    for (int i = 0; i < 2 * n; ++i)
        rem[a[i] % 2].push_back(i);
    if ((int)rem[0].size() % 2 == 1)
    {
        rem[0].pop_back();
        rem[1].pop_back();
    }
    else
    {
        int j = 0;
        if ((int)rem[j].size() < (int)rem[1 - j].size())
            j = 1 - j;
        rem[j].pop_back();
        rem[j].pop_back();
    }
    for (int r = 0; r < 2; ++r)
    {
        for (int i = 0; i < (int)rem[r].size(); ++i)
        {
            cout << rem[r][i] + 1 << " ";
            if (i % 2 == 1)
                cout << endl;
        }
    }
}
int main(void)
{
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n;
        for (int i = 0; i < 2 * n; ++i)
            cin >> a[i];
        solve();
    }
    return 0;
}


1370A - Maximum Gcd

Click to show code.


using namespace std;
int main(void)
{
    int t, n;
    cin >> t;
    while (t--)
    {
        cin >> n;
        cout << (n / 2) << endl;
    }
    return 0;
}


1368B - Codeforces Subsequences

Click to show code.


using namespace std;
using vi = vector<int>;
using ll = long long;
using predicate = function<bool(int)>;
ll k;
string cf = "codeforces";
const int n = 10;
bool can_distribute(int x)
{
    vi each(n, 1 + x / n);
    x %= n;
    for (int i = 0; i < n and x > 0; ++i, --x)
        each[i] += 1;
    ll kp = 1;
    for (auto y : each)
        kp *= y;
    return kp >= k;
}
int bsearch(int l, int r, predicate p)
{
    while (l < r)
    {
        int mid = l + (r - l) / 2;
        if (p(mid) == true)
            r = mid;
        else
            l = mid + 1;
    }
    return l;
}
int main(void)
{
    cin >> k;
    int ans = bsearch(0, 40 * 10, can_distribute);
    vi each(n, 1 + ans / n);
    ans %= n;
    for (int i = 0; i < n and ans > 0; ++i, --ans)
        each[i] += 1;
    for (int i = 0; i < n; ++i)
    {
        while (each[i]--)
            cout << cf[i];
    }
    cout << endl;
    return 0;
}


1368A - C+=

Click to show code.


using namespace std;
int main(void)
{
    int t, a, b, n, ans;
    cin >> t;
    while (t--)
    {
        cin >> a >> b >> n;
        if (a < b)
            swap(a, b);
        ans = 0;
        while (a <= n)
        {
            b += a;
            swap(a, b);
            ++ans;
        }
        cout << ans << endl;
    }
    return 0;
}