我是靠谱客的博主 深情钢笔,最近开发中收集的这篇文章主要介绍D - Alice, Bob and Candies CodeForces - 1352D,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

There are nn candies in a row, they are numbered from left to right from 11 to nn. The size of the ii-th candy is aiai.
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 11 candy (its size is a1a1). 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 n=11n=11 and a=[3,1,4,1,5,9,2,6,5,3,5]a=[3,1,4,1,5,9,2,6,5,3,5], then:
move 1: Alice eats one candy of size 33 and the sequence of candies becomes [1,4,1,5,9,2,6,5,3,5][1,4,1,5,9,2,6,5,3,5].
move 2: Alice ate 33 on the previous move, which means Bob must eat 44 or more. Bob eats one candy of size 55 and the sequence of candies becomes [1,4,1,5,9,2,6,5,3][1,4,1,5,9,2,6,5,3].
move 3: Bob ate 55 on the previous move, which means Alice must eat 66 or more. Alice eats three candies with the total size of 1+4+1=61+4+1=6 and the sequence of candies becomes [5,9,2,6,5,3][5,9,2,6,5,3].
move 4: Alice ate 66 on the previous move, which means Bob must eat 77 or more. Bob eats two candies with the total size of 3+5=83+5=8 and the sequence of candies becomes [5,9,2,6][5,9,2,6].
move 5: Bob ate 88 on the previous move, which means Alice must eat 99 or more. Alice eats two candies with the total size of 5+9=145+9=14 and the sequence of candies becomes [2,6][2,6].
move 6 (the last): Alice ate 1414 on the previous move, which means Bob must eat 1515 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:
aa — the total size of all sweets eaten by Alice during the game;
bb — the total size of all sweets eaten by Bob during the game.
Input
The first line contains an integer tt (1≤t≤50001≤t≤5000) — the number of test cases in the input. The following are descriptions of the tt test cases.Each test case consists of two lines. The first line contains an integer nn (1≤n≤10001≤n≤1000) — the number of candies. The second line contains a sequence of integers a1,a2,…,ana1,a2,…,an (1≤ai≤10001≤ai≤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 nn for all sets of input data in a test does not exceed 2⋅1052⋅105.
Output
For each set of input data print three integers — the number of moves in the game and the required values aa and bb.
Example
Input
7
11
3 1 4 1 5 9 2 6 5 3 5
1
1000
3
1 1 1
13
1 2 3 4 5 6 7 8 9 10 11 12 13
2
2 1
6
1 1 1 1 1 1
7
1 1 1 1 1 1 1
Output
6 23 21
1 1000 0
2 1 2
6 45 46
2 2 1
3 4 2
4 4 3

#include<stdio.h>
int main()
{
int a, b, a1, b1;
int n, m;
int t;
int i, j, k;
int x[1000];
scanf("%d", &t);
for ( i = 0; i < t; i++)
{
scanf("%d", &n);
for ( j = 0; j < n; j++)
{
scanf("%d", &x[j]);
}
a1 = 0; b1 = 0; n = n - 1; m = 0; a = 0; b = 0; k = 0;
while (1)
{
a1 = 0;
for ( j = m;j<=n&&a1<=b1; j++)
{
a1 += x[j];
}
a += a1; m = j; k++;
if (n<m)
{
break;
}
b1 = 0;
for ( j = n;j>=m&&b1<=a1 ; j--)
{
b1 = b1 + x[j];
}
n = j; b += b1; k++;
if (n<m)
{
break;
}
}
printf("%d %d %dn", k, a, b);
}
return 0;
}

最后

以上就是深情钢笔为你收集整理的D - Alice, Bob and Candies CodeForces - 1352D的全部内容,希望文章能够帮你解决D - Alice, Bob and Candies CodeForces - 1352D所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(64)

评论列表共有 0 条评论

立即
投稿
返回
顶部