zoukankan      html  css  js  c++  java
  • C

    Problem description

    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
    解题思路:简单贪心。女士从前往后贪心,男士从后往前贪心,即吃完当前位置的巧克力所花费时间较少者,便有优先往后(或往前)再吃一块巧克力,直到两者相遇即此时已经吃完了所有的巧克力,求女士和男士各吃了多少块巧克力。
    AC代码:
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int n,first,last,sum=0,a,b,s[100005];
     4 int main(){
     5     cin>>n;
     6     for(int i=1;i<=n;++i)cin>>s[i];
     7     a=s[(first=1)],b=s[(last=n)];
     8     while(first<=last){
     9         if(a<=b)a+=s[++first];//用时相等即两人刚好要吃相同的巧克力时,女士优先,男士靠边。
    10         else b+=s[--last];
    11     }
    12     cout<<(first-1)<<' '<<(n-last)<<endl;
    13     return 0;
    14 }
    
    
  • 相关阅读:
    【公告】对乐逍遥和王者之剑利用破解程序插入刷流量广告处理结果
    Thunder7.2.13.3884 JayXon
    免费获取半年 Bitdefender Total Security 2014
    WIN8.1 PRO RTM VOL.2013.09.18
    免费一年MAP2014+6个月免费MIS2014
    大蜘蛛9.0正式版
    腾讯控股涉足商业银行 微信或成先头兵
    植物大战僵尸2:奇妙时空之旅[官方安卓简体中文高清版]有内含....
    苹果iPhone 5C和5S发布后,消费者如何选择?
    pandas中DataFrame操作(一)
  • 原文地址:https://www.cnblogs.com/acgoto/p/9124815.html
Copyright © 2011-2022 走看看