HDU 2374 || SDUT2386 A Game with Marbles(简单题)

A Game with Marbles

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 611    Accepted Submission(s): 339

Problem Description

There are n bowls, numbered from 1 to n. Initially, bowl i contains mi marbles. One game step consists of removing one marble from a bowl. When removing a marble from bowl i (i > 1), one marble is added to each of the first i-1 bowls; if a marble is removed from bowl 1, no new marble is added. The game is finished after each bowl is empty.

Your job is to determine how many game steps are needed to finish the game. You may assume that the supply of marbles is sufficient, and each bowl is large enough, so that each possible game step can be executed. 

Input

The input contains several test cases. Each test case consists of one line containing one integer n (1 ≤ n ≤ 50), the number of bowls in the game. The following line contains n integers mi (1 ≤ i ≤ n, 0 ≤ mi ≤ 1000), where mi gives the number of marbles in bowl i at the beginning of the game. 

The last test case is followed by a line containing 0. 

 

Output

For each test case, print one line with the number of game steps needed to finish the game. You may assume that this number fits into a signed 64-bit integer.

 

Sample Input

10

3 3 3 3 3 3 3 3 3 3

5

1 2 3 4 5

0

Sample Output

3069

129

Source

2008水题公开赛(比速度,OJ压力测试)

 

Recommend

lcy

 

解题报告:这道题读懂了就好做了,就是若移第i个碗的每一个大理石时,它前面的全部加1,即移完第i个碗中的大理石,它前面的每个碗中增加的的碗数就是第i个碗中的大理石,问移到第一个碗中最少需要多少步,就是从最后的碗中向前移即可!

代码如下:

#include <stdio.h>
__int64 a[1010], ans;
int main()
{
int n, i, j;
while(scanf("%d", &n) != EOF && n)
{
for (i = 1; i <= n; ++i)
{
scanf("%I64d", &a[i]);
}
ans = 0;
for (i = n; i >= 1; --i)
{
ans += a[i];
for(j = i- 1; j >=1; --j)//让i前面的全部加上a[i],表示把第i个碗中的大理石全部移完
{
a[j] += a[i];
}
}
printf("%I64d\n", ans);
}
return 0;
}



 

转载于:https://www.cnblogs.com/lidaojian/archive/2012/03/05/2380871.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值