http://codeforces.com/contest/677/problem/B
Vanya smashes potato in a vertical food processor. At each moment of time the height of the potato in the processor doesn't exceed h and the processor smashes k centimeters of potato each second. If there are less than k centimeters remaining, than during this second processor smashes all the remaining potato.
Vanya has n pieces of potato, the height of the i-th piece is equal to ai. He puts them in the food processor one by one starting from the piece number 1 and finishing with piece number n. Formally, each second the following happens:
- If there is at least one piece of potato remaining, Vanya puts them in the processor one by one, until there is not enough space for the next piece.
- Processor smashes k centimeters of potato (or just everything that is inside).
Provided the information about the parameter of the food processor and the size of each potato in a row, compute how long will it take for all the potato to become smashed.
The first line of the input contains integers n, h and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ h ≤ 109) — the number of pieces of potato, the height of the food processor and the amount of potato being smashed each second, respectively.
The second line contains n integers ai (1 ≤ ai ≤ h) — the heights of the pieces.
Print a single integer — the number of seconds required to smash all the potatoes following the process described in the problem statement.
5 6 3 5 4 3 2 1
5
5 6 3 5 5 5 5 5
10
5 6 3 1 2 1 1 1
2
Consider the first sample.
- First Vanya puts the piece of potato of height 5 into processor. At the end of the second there is only amount of height 2 remaining inside.
- Now Vanya puts the piece of potato of height 4. At the end of the second there is amount of height 3 remaining.
- Vanya puts the piece of height 3 inside and again there are only 3 centimeters remaining at the end of this second.
- Vanya finally puts the pieces of height 2 and 1 inside. At the end of the second the height of potato in the processor is equal to 3.
- During this second processor finally smashes all the remaining potato and the process finishes.
In the second sample, Vanya puts the piece of height 5 inside and waits for 2 seconds while it is completely smashed. Then he repeats the same process for 4 other pieces. The total time is equal to 2·5 = 10 seconds.
In the third sample, Vanya simply puts all the potato inside the processor and waits 2 seconds.
题解:如果直接模拟会超时,n=100,h=10^9,k=1, 每个a[i]=10^9, ans=10^11 ,数据超int,CF 必须用__int64.。所以优化就是每个a[i]加入桶之前就先对k取模,并记录下ans.最后的结果是一样的。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cctype>
#include<queue>
#include<cstdlib>
#include<string>
#include<cstdlib>
using namespace std;
#define LL __int64
#define maxn 100010
#define PI 3.1415926
LL ans;
int a[100005];
int main()
{
int n,h,k,x;
while(~scanf("%d%d%d",&n,&h,&k))
{
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
x=0;
ans=0;
for(int i=0;i<n;i++)
{
if(x+a[i]>h)
{
x=0;
ans++;
}
x+=a[i]%k;
ans+=a[i]/k;
if(x>=k)
{
x-=k;
ans++;
}
}
if(x!=0)
ans+=1;
printf("%I64d\n",ans);
}
}