Optimizing String Construction and GCD Generation for Competitive Programming

To maximizee the number of positive votes, we can separate reviews into two groups: one for negative ratings (value 2) and another for all others. Since negative reviews contribute nothing to the total, we simply count all non-negatvie ratings.

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

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

    int T;
    cin >> T;
    while (T--) {
        int n;
        cin >> n;
        int count = 0;
        for (int i = 0; i < n; ++i) {
            int rating;
            cin >> rating;
            if (rating != 2) count++;
        }
        cout << count << '\n';
    }
    return 0;
}

B. GCD Length

We are tasked with constructing two integers a and b with specified digit lengths such that their GCD has exactly c digits. A simple strategy is to use a number composed of c ones (e.g., 111...1) as the GCD base. Then, scale it by multiplying with small primes (2 and 3) to reach the required digit lengths for a and b.

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

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

    int T;
    cin >> T;
    while (T--) {
        int lenA, lenB, gcdLen;
        cin >> lenA >> lenB >> gcdLen;

        long long base = 0;
        for (int i = 0; i < gcdLen; ++i) {
            base = base * 10 + 1;
        }

        if (lenA == lenB && lenA == gcdLen) {
            cout << base << ' ' << base << '\n';
            continue;
        }

        long long numA = base, numB = base;
        long long minA = pow(10, lenA - 1);
        long long minB = pow(10, lenB - 1);

        while (numA < minA) numA *= 2;
        while (numB < minB) numB *= 3;

        cout << numA << ' ' << numB << '\n';
    }
    return 0;
}

C. Yet Another Card Deck

Given a deck of cards with values up to 50, we simulate frequent queries that move a specific card to the front. Instead of physically shifting the entire array, we track the first occurrence of each value. When a card is moved to the front, we increment the positiosn of all other cards that were originally before it.

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

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

    int n, q;
    cin >> n >> q;
    vector<int> deck(n + 1);
    vector<int> firstPos(51, 0); // 1-indexed values up to 50

    for (int i = 1; i <= n; ++i) {
        cin >> deck[i];
        if (firstPos[deck[i]] == 0) {
            firstPos[deck[i]] = i;
        }
    }

    while (q--) {
        int x;
        cin >> x;
        cout << firstPos[x] << ' ';

        // Shift all cards that were before x
        for (int val = 1; val <= 50; ++val) {
            if (val != x && firstPos[val] < firstPos[x]) {
                firstPos[val]++;
            }
        }
        firstPos[x] = 1; // Move x to front
    }
    cout << '\n';
    return 0;
}

D. Min Cost String

To construct a string of length n using the first k lowercase letters with minimal cost (defined as avoiding repeated adjacent substrings of length 2), we generate all unique two-character combinations in lexicographic order: start with single characters, then generate pairs like "ab", "ac", ..., "bc", "bd", etc., ensuring no duplicate substrings are formed.

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

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

    int n, k;
    cin >> n >> k;

    string pattern = "";
    for (char c1 = 'a'; c1 < 'a' + k; ++c1) {
        pattern += c1;
        for (char c2 = c1 + 1; c2 < 'a' + k; ++c2) {
            pattern += c1;
            pattern += c2;
        }
    }

    for (int i = 0; i < n; ++i) {
        cout << pattern[i % pattern.size()];
    }
    cout << '\n';
    return 0;
}

Tags: competitive-programming gcd string-construction simulation lexicographic-order

Posted on Sat, 06 Jun 2026 16:47:02 +0000 by plasmahba