练习cf1352D. Alice, Bob and Candies
题目如下
D. Alice, Bob and Candies
time limit per test2 seconds
memory limit per test256 megabytes
There are 𝑛 candies in a row, they are numbered from left to right from 1 to 𝑛. The size of the 𝑖-th candy is 𝑎𝑖.
Alice and Bob play an interesting and tasty game: they eat candy. Alice will eat candy from left to right, and Bob — from right to left. The game ends if all the candies are eaten.
The process consists of moves. During a move, the player eats one or more sweets from her/his side (Alice eats from the left, Bob — from the right).
Alice makes the first move. During the first move, she will eat 1 candy (its size is 𝑎1). Then, each successive move the players alternate — that is, Bob makes the second move, then Alice, then again Bob and so on.
On each move, a player counts the total size of candies eaten during the current move. Once this number becomes strictly greater than the total size of candies eaten by the other player on their previous move, the current player stops eating and the move ends. In other words, on a move, a player eats the smallest possible number of candies such that the sum of the sizes of candies eaten on this move is strictly greater than the sum of the sizes of candies that the other player ate on the previous move. If there are not enough candies to make a move this way, then the player eats up all the remaining candies and the game ends.
For example, if 𝑛=11 and 𝑎=[3,1,4,1,5,9,2,6,5,3,5], then:
move 1: Alice eats one candy of size 3 and the sequence of candies becomes [1,4,1,5,9,2,6,5,3,5].
move 2: Alice ate 3 on the previous move, which means Bob must eat 4 or more. Bob eats one candy of size 5 and the sequence of candies becomes [1,4,1,5,9,2,6,5,3].
move 3: Bob ate 5 on the previous move, which means Alice must eat 6 or more. Alice eats three candies with the total size of 1+4+1=6 and the sequence of candies becomes [5,9,2,6,5,3].
move 4: Alice ate 6 on the previous move, which means Bob must eat 7 or more. Bob eats two candies with the total size of 3+5=8 and the sequence of candies becomes [5,9,2,6].
move 5: Bob ate 8 on the previous move, which means Alice must eat 9 or more. Alice eats two candies with the total size of 5+9=14 and the sequence of candies becomes [2,6].
move 6 (the last): Alice ate 14 on the previous move, which means Bob must eat 15 or more. It is impossible, so Bob eats the two remaining candies and the game ends.
Print the number of moves in the game and two numbers:
𝑎 — the total size of all sweets eaten by Alice during the game;
𝑏 — the total size of all sweets eaten by Bob during the game.
Input
The first line contains an integer 𝑡 (1≤𝑡≤5000) — the number of test cases in the input. The following are descriptions of the 𝑡 test cases.
Each test case consists of two lines. The first line contains an integer 𝑛 (1≤𝑛≤1000) — the number of candies. The second line contains a sequence of integers 𝑎1,𝑎2,…,𝑎𝑛 (1≤𝑎𝑖≤1000) — the sizes of candies in the order they are arranged from left to right.
It is guaranteed that the sum of the values of 𝑛 for all sets of input data in a test does not exceed 2⋅105.
Output
For each set of input data print three integers — the number of moves in the game and the required values 𝑎 and 𝑏.
题目大意
A和B分别吃糖果,A从左边开始吃,B从右边开始吃,第一颗糖果由A来吃,A与B交替吃,每次吃的糖果的值都需要大于上一个人的糖果的值(允许一次吃多颗糖),直到吃完所有糖果(最后一回合不论是否大于上一人)。输出吃完这堆糖果的总回合数,以及A和B分别吃的糖果的总值。
题目分析
A和B分别从两边开始吃,那么可以采用双指针,在吃完所有糖果之前分回合进行,左右指针轮回移动。
代码如下
点击查看代码
#include <iostream>
using namespace std;
int main(){
int t;
cin >> t;
while(t--){
int n;
cin >> n;
int candies[1005];
for(int i = 0; i < n; i++){
cin >> candies[i];
}
int l = 0, r = n - 1;
int turn = 0;
int left = 0, right = 0;
int sumA = 0, sumB = 0;
while(l <= r){
if(turn % 2 == 0){
left = 0;
while(l <= r && left <= right) {
left += candies[l++];
}
sumA += left;
}else{
right = 0;
while(l <= r && left >= right){
right += candies[r--];
}
sumB += right;
}
turn++;
}
printf("%d %d %d\n", turn, sumA, sumB);
}
return 0;
}

浙公网安备 33010602011771号