6C - Alice, Bob and Chocolate

本文介绍了一道关于两人轮流按顺序吃巧克力的游戏问题。游戏中,Alice从左向右吃,Bob从右向左吃,每块巧克力有不同的消耗时间。若两人同时到达同一巧克力,则Bob礼让Alice。文章通过一个简洁的算法确定了最终各自能吃掉多少巧克力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C. Alice, Bob and Chocolate
time limit per test
2 seconds
memory limit per test
64 megabytes
input
standard input
output
standard output

Alice and Bob like games. And now they are ready to start a new game. They have placed n chocolate bars in a line. Alice starts to eat chocolate bars one by one from left to right, and Bob — from right to left. For each chocololate bar the time, needed for the player to consume it, is known (Alice and Bob eat them with equal speed). When the player consumes a chocolate bar, he immediately starts with another. It is not allowed to eat two chocolate bars at the same time, to leave the bar unfinished and to make pauses. If both players start to eat the same bar simultaneously, Bob leaves it to Alice as a true gentleman.

How many bars each of the players will consume?

Input

The first line contains one integer n (1 ≤ n ≤ 105) — the amount of bars on the table. The second line contains a sequence t1, t2, ..., tn(1 ≤ ti ≤ 1000), where ti is the time (in seconds) needed to consume the i-th bar (in the order from left to right).

Output

Print two numbers a and b, where a is the amount of bars consumed by Alice, and b is the amount of bars consumed by Bob.

Examples
input
5
2 9 8 2 7
output
2 3

n个糖,a从前往后吃,b从后往前吃,每个糖都有吃完需要的时间,每个人在同一时间只能吃一个,吃完某一个后,才能吃下一个,如果两个人同时开始吃同一个,b会让给a,问最后每个人总共吃了多少糖


比较简单的题目,但是需要仔细理解题意,觉得思维题目最重要的是要找到最优化的方法去解决,这样才能训练思维能力,而不能做出来就行了...


/*
http://blog.youkuaiyun.com/liuke19950717
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1e5+5;
int x[maxn];
int cnt(double s)
{
    int tp=0;
    for(int i=1;i<maxn;++i)
    {
        tp+=x[i];
        if(tp>s)
        {
            return i;
        }
    }
}
int judge(int sign)
{
    int tp=0;
    for(int i=1;i<sign;++i)
    {
        tp+=x[i];
    }
    return tp;
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        int sum=0;
        for(int i=1;i<=n;++i)
        {
            scanf("%d",&x[i]);
            sum+=x[i];
        }
        int sign=cnt(sum/2.0);
        sum-=x[sign];
        int tp=judge(sign);
        if(tp>sum/2.0)
        {
            --sign;
        }
        printf("%d %d\n",sign,n-sign);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值