Day 08: GESP Level 1 – Time Planning

Learn programming with Lao Ma by “leveling up and fighting monsters”!

  • Involves examination: Computer Society Programming Ability Level Certification (GESP)
  • Activity content: Provides real exam questions of different levels for students to choose for practice
  • Preparation advice: Choose corresponding questions based on your preparation level
  • Additional value: Can be used as preparation training for whitelist competitions

Day 08: GESP Level 1 – Time Planning 2023.06

[Submit]

https://www.luogu.com.cn/problem/B3838

[Problem Description]

Little Ming is planning his study time. Now he wants to know how many minutes are between two moments. Can you help him with programming?

[Input Description]

Input 4 lines, the first line is the hour of the start time, the second line is the minute of the start time, the third line is the hour of the end time, and the fourth line is the minute of the end time.

The input guarantees that the two moments are on the same day, and the start time is always before the end time. The time is in 24-hour format, with hours ranging from 0 to 23 and minutes from 0 to 59.

[Output Description]

Output one line, containing an integer, which is the number of minutes from the start time to the end time.

[Sample Input 1]

9
5
9
6

[Sample Output 1]

1

[Sample Input 2]

9
5
10
0

[Sample Output 2]

55

Reference Answer:

Define the necessary variables according to the problem requirements and implement input;

Use the end time’s hour minus the start time’s hour, multiply the result by 60 to get the number of minutes corresponding to the hour difference;

Use the end time’s minute minus the start time’s minute to get the minute difference;

Add the above results and output;

/*
* [GESP202306 Level 1] Time Planning
* https://www.luogu.com.cn/problem/B3838
*/
#include <iostream>

using namespace std;

int main()
{
    int h1 = 0, m1 = 0, h2 = 0, m2 = 0;
    cin >> h1 >> m1;
    cin >> h2 >> m2;
    cout << (h2 - h1) * 60 + (m2 - m1) << endl;
    return 0;
}

Day 08: GESP Level 2 – Little Yang’s Day Matrix

[Submit]

https://www.luogu.com.cn/problem/B3955

[Problem Description]

Little Yang wants to construct a day matrix of size ( is odd), specifically, this matrix has rows and characters per row, where the leftmost and rightmost columns are <span>|</span>, and the first row, last row, and the middle row (i.e., the row) have the character as <span>-</span>, while all other characters are lowercase letters <span>x</span>. For example, a day matrix looks like this:

|---|
|xxx|
|---|
|xxx|
|---|

Please help Little Yang print the corresponding “day matrix” based on the given .

[Input Description]

A single integer ( is guaranteed to be odd).

[Output Description]

Output the corresponding “day matrix”.

Please strictly follow the format requirements for output, do not add any spaces, punctuation, blank lines, or any symbols arbitrarily. You must output exactly lines, each line must contain exactly characters, which can be <span>-</span>, <span>|</span>, or <span>x</span>.Your output must match the standard answer exactly to score, please check carefully before submission.

[Sample Input 1]

5

[Sample Output 1]

|---|
|xxx|
|---|
|xxx|
|---|

[Sample Input 2]

7

[Sample Output 2]

|-----|
|xxxxx|
|xxxxx|
|-----|
|xxxxx|
|xxxxx|
|-----|

Reference Answer:

/*
* [GESP202403 Level 2] Little Yang's Day Matrix
* https://www.luogu.com.cn/problem/B3955
*/
#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            char ch;
            if (j == 0 || j == n - 1) {
                ch = '|';
            } elseif (i == 0 || i == n - 1 || i == n / 2) {
                ch = '-';
            } else {
                ch = 'x';
            }
            cout << ch;
        }
        cout << endl;
    }
    return 0;
}

Day 08: GESP Level 3 – Little Yang’s Savings

[Submit]

https://www.luogu.com.cn/problem/B3867

[Problem Description]

Little Yang has piggy banks, numbered from 0 to . Starting from day 1, Little Yang saves money in the piggy banks every day. Specifically, on the day, he will choose a piggy bank and save coins. After days, he has forgotten how much money he saved in each piggy bank. Can you help him?

[Input Description]

Input 2 lines, the first line contains two integers ; the second line contains integers, where the integer is (guaranteed to be ).

Each integer in a line is separated by a single space.

It is guaranteed that ;

[Output Description]

Output integers separated by a single space, where the integer represents how much money is in the piggy bank numbered .

[Special Reminder]

In regular programs, providing prompts during input and output is a good habit. However, in this exam, due to system limitations, please do not include any prompt information in the input and output.

[Sample Input 1]

2 3
0 1 0

[Sample Output 1]

4 2

[Sample Explanation 1]

Little Yang saved 1 coin in piggy bank 0 on day 1, 2 coins in piggy bank 1 on day 2, and 3 coins in piggy bank 0 on day 3, so piggy bank 0 has 1 + 3 = 4 coins, while piggy bank 1 has 2 coins.

[Sample Input 2]

3 5
0 0 0 2 0

[Sample Output 2]

11 0 4

Reference Answer:

/*
* [GESP202309 Level 3] Little Yang's Savings
* https://www.luogu.com.cn/problem/B3867
*/

# include<iostream>

using namespace std;

int main()
{
    int N, D;
    int b[1000];
    cin >> N >> D;
    for (int i = 0;i < N;i++)
    {
        b[i] = 0;
    }
    for (int i = 0;i < D;i++)
    {
        int a;
        cin >> a;
        b[a] += (i + 1);
    }
    cout << b[0];
    for (int i = 1;i < N;i++)
    {
        cout << " " << b[i];
    }
    cout << endl;
    return 0;
}

Day 08: GESP Level 4 – Lucky Number

[Submit]

https://www.luogu.com.cn/problem/B3850

[Problem Description]

Little Ming invented a “lucky number”. A positive integer has even positions unchanged (the unit digit is the 1st position, the ten digit is the 2nd position, and so on), while the odd positions undergo the following transformation: multiply the digit by 7, if the result is not greater than 9, it is the transformation result; otherwise, sum the digits of the result, if the result is not greater than 9, it is the transformation result; otherwise (if the result is still greater than 9), continue summing the digits until the result is not greater than 9, which becomes the transformation result. After the transformation, sum the digits of the transformation result, if the sum is a multiple of 8, the original positive integer is called a lucky number.

For example, 16347: the 1st position is 7, multiplying by 7 gives 49, which is greater than 9, summing the digits gives 13, which is still greater than 9, continue summing the digits, the final result is 4; the 3rd position is 3, the transformation result is 3; the 5th position is 1, the transformation result is 7. The final transformation result is 76344, and for the result 76344, the sum of the digits is 24, which is a multiple of 8. Therefore, 16347 is a lucky number.

[Input Description]

The first line contains a positive integer , indicating the number of positive integers to be judged. It is stipulated that .

From the 2nd line onwards, each line contains a positive integer to be judged. It is stipulated that these positive integers are less than 10^12.

[Output Description]

Output lines, indicating whether each positive integer is a lucky number, output <span>T</span> if it is, otherwise output <span>F</span>.

Note: You do not need to wait until all inputs are finished to output; you can judge one number and output it, then input the next number.

[Sample Input]

2
16347
76344

[Sample Output]

T
F

Reference Answer:

/*
* [GESP202306 Level 4] Lucky Number
* https://www.luogu.com.cn/problem/B3850
*/
#include<iostream>

using namespace std;

// Function to process odd position data
int func(int n)
{
    int a = n * 7;
    if (a <= 9)
    {
        return a;
    }
    int b;
    do
    {
        b = 0;
        // Typical digit separation method
        while (a > 0)
        {
            b += a % 10;
            a = a / 10;
        }
        a = b;
    } while (b > 9);
    return b;
}

int main()
{
    // Input data
    int N;
    cin >> N;
    for (int i = 0; i < N; i++)
    {
        int k = 1;// Record which position is being processed
        int total = 0;// Record the accumulated result
        int x;
        cin >> x;
        // Typical digit separation method
        while (x > 0)
        {
            int a = x % 10;
            if (k % 2 != 0)
            {
                total += func(a);
            }
            else
            {
                total += a;
            }
            k += 1;
            x = x / 10;
        }
        if (total % 8 == 0)
        {
            cout << "T" << endl;
        }
        else
        {
            cout << "F" << endl;
        }
    }
    return 0;
}

Day 08: GESP Level 5 – Weapon Enhancement

[Submit]

https://www.luogu.com.cn/problem/B4071

[Problem Description]

Little Yang has types of weapons and types of enhancement materials. The type of enhancement material is compatible with the type of weapon, and Little Yang can spend coins to modify the corresponding weapon of that material to any weapon.

Little Yang’s favorite weapon is the type, so he hopes that the number of types of enhancement materials compatible with that weapon is strictly greater than that of other weapons. Can you help Little Yang calculate the minimum cost required to meet this condition?

[Input Description]

The first line contains two positive integers , meaning as described in the problem statement.

Then, lines, each containing two positive integers, representing the compatible weapon of the type of enhancement material and the modification cost.

[Output Description]

Output an integer representing the minimum cost in coins required to make the number of types of enhancement materials compatible with the type of weapon strictly greater than that of other weapons.

[Sample Input 1]

4 4
1 1
2 1
3 1
3 2

[Sample Output 1]

1

[Sample Explanation]

By spending , change the compatible weapon of the third enhancement material to . At this point, weapon has types of enhancement materials compatible, while weapons and each have types of enhancement materials compatible. This satisfies the condition that the number of types of enhancement materials compatible with the type of weapon is strictly greater than that of other weapons.

[Data Range]

Day 08: GESP Level 1 - Time Planning

For all data, it is guaranteed that ,,,.

Reference Answer:

/*
* [GESP202412 Level 5] Weapon Enhancement
* https://www.luogu.com.cn/problem/B4071
*/
# include<iostream>
# include<vector>
# include<algorithm>

using namespace std;

struct weapon
{
    vector<int> storage;
};
weapon arr[1005];

long long ans = 1e18;
int n, m;//n weapon types, m material types

// Calculate the minimum cost
long long func(int x)
{
    int cnt = x - arr[1].storage.size();// Calculate how many materials need to be robbed
    long long sum = 0;
    vector<int> temp;
    for (int i = 2;i <= n;i++)
    {
        int ll = arr[i].storage.size() - x + 1;
        for (int j = 0;j < arr[i].storage.size();j++)
        {
            if (ll >= 1)// Rob excess materials
            {
                sum += arr[i].storage[j];
                ll--;
                cnt--;
            }
            else// Store remaining materials
            {
                temp.push_back(arr[i].storage[j]);
            }
        }
    }
    sort(temp.begin(), temp.end());
    for (int i = 0;i < cnt;i++)
    {
        sum += temp[i];
    }
    return sum;
}

int main()
{
    //1. Store data   
    cin >> n >> m;
    for (int i = 1;i <= m;i++)
    {
        int id, price;
        cin >> id >> price;
        arr[id].storage.push_back(price);
    }
    // 2. Sort the decoration material prices for each weapon
    for (int i = 1;i <= n;i++)
    {
        sort(arr[i].storage.begin(), arr[i].storage.end());
    }
    // 3. Enumerate the number of decoration materials for the first weapon
    int i = max(int(arr[1].storage.size()), 1);
    for (;i <= m;i++)
    {
        // 4. Calculate the cost
        ans = min(ans, func(i));
    }
    // 5. Output result
    cout << ans;
    return 0;
}

Day 08: GESP Level 6 – Calculate Score

[Submit]

https://www.luogu.com.cn/problem/P10721

[Problem Description]

Little Yang wants to calculate the score of a string composed of lowercase letters.

Little Yang has set a scoring sequence containing positive integers, and if a substring of the string is composed of characters, it can score , and the characters in the string cannot be counted for score multiple times. The total score of the entire string is the sum of the scores of the scoring substrings.

For example, suppose the string has the following possible scoring methods:

  • or , where and do not score, total score is .
  • , total score is .
  • , total score is .

Little Yang wants to know the maximum total score for a given string.

[Input Description]

  • The first line contains a positive integer , representing the length of the scoring sequence.
  • The second line contains positive integers, representing the scoring sequence.
  • The third line contains a positive integer , representing the length of the string.
  • The fourth line contains a string composed of lowercase letters.

[Output Description]

Output an integer representing the maximum total score for the given string.

[Sample Input 1]

3
3 1 2
13
dabcabcabcabz

[Sample Output 1]

9

[Sample Explanation]

The optimal scoring method is , with a total score of , totaling points.

[Data Range]

Day 08: GESP Level 1 - Time Planning

For all data, it is guaranteed that , and .

Reference Answer:

/*
* [GESP202406 Level 6] Calculate Score
* https://www.luogu.com.cn/problem/P10721
*/
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

// Check if k consecutive "abc" starting from position start is valid
bool isValidABC(const string&amp; s, int start, int k) 
{
    for (int j = 0; j < k; ++j)
    {
        int pos = start + 3 * j;
        if (s.substr(pos, 3) != "abc") 
        {
            return false;
        }
    }
    return true;
}

int main()
{
    // Input processing
    int n;  // Length of scoring sequence
    cin >> n;
    vector<int> a(n + 1);  // Scoring sequence, a[1] to a[n]
    for (int i = 1; i <= n; ++i)
    {
        cin >> a[i];
    }
    int m;  // Length of string
    cin >> m;
    string s;  // Input string
    cin >> s;

    // dp[i] represents the maximum score for the first i characters
    vector<long long> dp(m + 1, 0);

    for (int i = 3; i <= m; ++i) // Start checking from the 3rd character
    {
        dp[i] = dp[i - 1];  // Initialize to the case of not selecting the current character

        // Quick check: only when the current character is 'c' can it possibly form "abc"
        if (s[i - 1] == 'c') // Note that string index starts from 0, so it's s[i-1]
        {
            // Check all possible k "abc" combinations (k from 1 to n)
            for (int k = 1; k <= n && i >= 3 * k; ++k)
            {
                // Check if k "abc" starting from position i-3k is valid
                if (isValidABC(s, i - 3 * k, k))
                {
                    // If valid, update the current maximum score
                    dp[i] = max(dp[i], dp[i - 3 * k] + a[k]);
                }
            }
        }
    }
    // Output the maximum score for the entire string
    cout << dp[m] << endl;
    return 0;
}

Youth Programming Competition Exchange

The “Youth Programming Competition Exchange Group” has been established (suitable for young people aged 6 to 18). Add the assistant’s WeChat to invite everyone into the learning group. After joining the group, everyone can participate in regularly organized 21-day problem-solving check-ins, level exam assessments, Ministry of Education whitelist competition coaching, and youth programming team competitions.

Day 08: GESP Level 1 - Time Planning

Leave a Comment