题目地址:https://codeforces.com/problemset/problem/709/A
Kolya is going to make fresh orange juice. He has n oranges of sizes a1, a2, …, an. Kolya will put them in the juicer in the fixed order, starting with orange of size a1, then orange of size a2 and so on. To be put in the juicer the orange must have size not exceeding b, so if Kolya sees an orange that is strictly greater he throws it away and continues with the next one.
The juicer has a special section to collect waste. It overflows if Kolya squeezes oranges of the total size strictly greater than d. When it happens Kolya empties the waste section (even if there are no more oranges) and continues to squeeze the juice. How many times will he have to empty the waste section?
Input
The first line of the input contains three integers n, b and d (1 ≤ n ≤ 100 000, 1 ≤ b ≤ d ≤ 1 000 000) — the number of oranges, the maximum size of the orange that fits in the juicer and the value d, which determines the condition when the waste section should be emptied.
The second line contains n integers a1, a2, …, an (1 ≤ ai ≤ 1 000 000) — sizes of the oranges listed in the order Kolya is going to try to put them in the juicer.
Output
Print one integer — the number of times Kolya will have to empty the waste section.
Note
In the first sample, Kolya will squeeze the juice from two oranges and empty the waste section afterwards.
In the second sample, the orange won’t fit in the juicer so Kolya will have no juice at all.
科亚要做新鲜的橙汁。他有n个大小为a1 a2…an的橘子。科亚会把它们按固定的顺序放入榨汁机,从a1大小的橙子开始,然后是a2大小的橙子,以此类推。要放入榨汁机,橙子的大小必须不超过b,所以如果科里亚看到一个严格意义上更大的橙子,他就把它扔掉,继续下一个。
榨汁机有一个专门的区域来收集废物。如果Kolya严格地挤压总大小大于d的橙子,就会溢出来。当这种情况发生时,Kolya会清空废物区(即使没有更多的橙子),并继续挤压果汁。他要清空废物区多少次?
错误代码:
#include<iostream>
using namespace std;
int main()
{
long long int n, b, d;
long long int i, dd = 0, sum = 0, a[100000];
cin >> n >> b >> d;
for (i = 0; i < n; i++) {
cin >> a[i];
if (a[i] > b) {
dd = 0;
}
else dd += a[i];
if (dd > d) {
sum++;
dd = 0;
}
}
cout << sum << endl;
//system("pause");
return 0;
}
原因是:使用了
if (a[i] > b) {
dd = 0;
}
else dd += a[i];
直接改成
if (a[i] <= b)
dd += a[i];
就可以了,
哎,多此一举了。。。。。
正确代码:
#include<iostream>
using namespace std;
int main()
{
long long int n, b, d;
long long int i, dd = 0, sum = 0, a[100000];
cin >> n >> b >> d;
for (i = 0; i < n; i++) {
cin >> a[i];
if (a[i] <= b)
dd += a[i];
if (dd > d) {
sum++;
dd = 0;
}
}
cout << sum << endl;
//system("pause");
return 0;
}