ZeroJudge F374: Grouping

A country is hosting the World Championship, attracting top players from various countries to participate. Each player will be assigned a combat power index by the officials, which must be a single digit between 0 and 9. A higher number indicates that the officials believe the player has more potential.

Suppose there are 4 players, with A having a combat power index of 0, B having 4, C having 0, and D having 3. After drawing lots, the grouping sequence of the players is BCDA. Groups are formed from right to left in the sequence. If a group has three players, players A, D, and C will be grouped in the first group, with a total combat power of 0 + 3 + 0 = 3. Player B will be grouped in the second group, with a total combat power of 4.

Please write a program to analyze the group number with the highest total combat power and its corresponding value based on the number of players per group and the combat power index of each participant.

Sample Inputs/Outputs

Sample Input(s)Sample Output(s)
Each line of input contains two numbers, N (1 ≤ N ≤ 8) and P. There are N people in a group, where P represents the combat power index of each participant arranged in the group sequence. It is known that there will be no more than 9 participants, and at least 2 teams will be formed. It is guaranteed that the combat power index of the leftmost participant in the sequence will never be 0.Output the group number and the total combat power index of the group with the highest total combat power after grouping, separated by a single space. If there are multiple groups with the same total combat power index, output the group number of the larger group.
1 3691 9
2 1934263 10
5 101213151 12
4 123443212 10

Thought Process

可以將 P 使用字串的方式存起來,因為題目有講最多不會超過9位選手,所以可以使用reverse不用擔心會TLE,將字串反轉之後就使用 For迴圈 將每一個字元做判斷並且找出最大戰力總和的組別。

Sample Code-ZeroJudge F374: Grouping

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    int N;
    string str;
    cin >> N;
    cin >> str;
    reverse(str.begin(), str.end());
    int count = 0, sum = 0, max = -999, team, cnt = 0;
    for (int i = 0; i<str.length(); i++)
    {
        sum += int(str[i] - '0');
        count++;
        if (count == N || i == str.length()-1)
        {
            count = 0;
            cnt++;
            if (sum >= max)
            {
                max = sum;
                team = cnt;
            }
            sum = 0;
        }
    }
    cout << team << " " << max << endl;
}

//ZeroJudge F374
//Dr. SeanXD

Comments