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?
InputThe 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).
OutputPrint 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.
Examples5 2 9 8 2 7
2 3
输入一行代表吃第i个巧克力所花费的时间,A、B两人,A从右开始向左吃巧克力,B从左向右开始吃巧克力,吃着吃着,如果两人同时开始吃某个巧克力,B会让给A吃,输出A,B所吃的巧克力的数目。
就是每个数比较,谁小谁先吃完当前的巧克力,另一个人正在吃的巧克力减去另一人吃完的巧克力,开吃的时候相等则两个人一起吃,过程中才是B让给A吃。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<math.h>
#include<algorithm>
#include<stack>
using namespace std;
typedef long long ll;
int main()
{
int n,a[100005],b[100005],i,j,sum1,sum2,t1,t2;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
b[i]=a[i];
}
if(n==1)
{
printf("1 0\n");
}
else
{
sum1=sum2=0;
t1=t2=0;
for(i=1,j=n;a[i]!=0||a[j]!=0;)
{
//cout<<i<<" "<<j<<endl;
//cout<<a[i]<<" "<<a[j]<<endl;
if(a[i]<a[j])
{
sum1++;
a[j]-=a[i];
t1+=b[i];
i++;
}
else if(a[i]>a[j])
{
sum2++;
a[i]-=a[j];
t2+=b[j];
j--;
}
else
{
if(i!=j)
{
sum1++;
sum2++;
a[i]-=a[i];
t1+=b[i];
a[j]-=a[j];
t2+=b[j];
}
else
{
if(t1==t2)
sum1++;
else if(t1>t2)
sum2++;
else if(t1<t2)
sum1++;
break;
}
i++;
j--;
}
}
printf("%d %d\n",sum1,sum2);
}
}