当前位置: 代码迷 >> 综合 >> Codeforces--6C--Alice, Bob and Chocolate
  详细解决方案

Codeforces--6C--Alice, Bob and Chocolate

热度:66   发布时间:2024-01-26 23:38:30.0

题目描述:
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?
输入描述:
The first line contains one integer n (1?≤?n?≤ 1 0 5 10^5 ) — 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).
输出描述:
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.
输入:
5
2 9 8 2 7
输出:
2 3
题意:
吃巧克力,从Alice从左往右,Bob从右往左看谁吃了多少
题解
逐步比较,逐步模拟,水题。
代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;const int maxn = 100000 + 5;
int a[maxn];int main(){int n;while(scanf("%d",&n)!=EOF){for(int i = 0; i < n; i ++) scanf("%d",&a[i]);int p = a[0],q = a[n - 1];int i,j;for(i = 0,j = n - 1; i <= j;i ++,j --){if(p == q){p = a[i + 1];q = a[j - 1];}else if(p > q){i -= 1;p -= q;q = a[j - 1];}else{j += 1;q = q - p;p = a[i + 1];}}printf("%d %d\n",i,n - i);}return 0;
}