B-Queue on Bus Stop
2016-02-26 16:45
531 查看
Description
It's that time of the year when the Russians flood their countryside summer cottages (dachas) and the bus stop has a lot of people. People rarely go to the dacha on their own, it's usually a group, so the people stand in queue by groups.
The bus stop queue has n groups of people. The
i-th group from the beginning has
ai people. Every
30 minutes an empty bus arrives at the bus stop, it can carry at most
m people. Naturally, the people from the first group enter the bus first. Then go the people from the second group and so on. Note that the order of groups in the queue never changes. Moreover, if some group cannot fit all of its members into
the current bus, it waits for the next bus together with other groups standing after it in the queue.
Your task is to determine how many buses is needed to transport all
n groups to the dacha countryside.
Input
The first line contains two integers n and
m(1 ≤ n, m ≤ 100). The next line contains
n integers: a1, a2, ..., an(1 ≤ ai ≤ m).
Output
Print a single integer — the number of buses that is needed to transport all
n groups to the dacha countryside.
Sample Input
Input
Output
Input
Output
1
It's that time of the year when the Russians flood their countryside summer cottages (dachas) and the bus stop has a lot of people. People rarely go to the dacha on their own, it's usually a group, so the people stand in queue by groups.
The bus stop queue has n groups of people. The
i-th group from the beginning has
ai people. Every
30 minutes an empty bus arrives at the bus stop, it can carry at most
m people. Naturally, the people from the first group enter the bus first. Then go the people from the second group and so on. Note that the order of groups in the queue never changes. Moreover, if some group cannot fit all of its members into
the current bus, it waits for the next bus together with other groups standing after it in the queue.
Your task is to determine how many buses is needed to transport all
n groups to the dacha countryside.
Input
The first line contains two integers n and
m(1 ≤ n, m ≤ 100). The next line contains
n integers: a1, a2, ..., an(1 ≤ ai ≤ m).
Output
Print a single integer — the number of buses that is needed to transport all
n groups to the dacha countryside.
Sample Input
Input
4 3 2 3 2 1
Output
3
Input
3 4 1 2 1
Output
1
#include <iostream> #include<algorithm> #include<cstdio> #include<cmath> #include<cstring> #include<cstdlib> using namespace std; int main() { int n, m, i, x = 0, y = 0; int a[100]; scanf("%d%d", &n, &m); for (i = 0; i < n; i++) { scanf("%d", &a[i]); } for (i = 0; i < n; i++) { x += a[i]; if (x == m) { x = 0; y++; } else if (x > m) { x = 0; y++; i--; } else if (i == n - 1 && x < m) y++; } printf("%d", y); return 0; }
相关文章推荐
- 修改Android Studio Build生成apk的默认名称
- 详释(常见UITextView 输入之字数限制)之一---固定长度
- iOS--Core Bluetooth
- LAoj 2678 - Subsequence (DP&技巧&排序)
- Guess the Permutation
- easyui datagrid 部分参数整理
- C# 项目迁移 Microsoft.VisualStudio.Tools.Office.BuildTasks 生成解决方法报错:请确认 <UsingTask> 声明正确,该程序集及其所有依赖项都可用
- Win8.1+Ubuntu双系统(UEFI+GPT)
- 取消UISearchbar背景色
- Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
- 给UIImage添加生成圆角图片的扩展API:
- iOS 键盘挡住UITextField
- iOS 事件响应者链的学习(也有叫 UI连锁链)
- Java中建造者(Builder)设计
- UINavigationController使用的注意事项
- android下Invalid layout of java.lang.String at value解决方法
- iOS之修改UIStatusbar 文字颜色
- win7 64位 旗舰版 右键菜单增加UE选项
- 详解 CALayer 和 UIView 的区别和联系
- 当键盘出现的时候,如何让UITextField自动上移