Universal driving range - greedy -P1181 Sequence segments Section I

Description Title
for a given length N is a positive integer number of columns A [i], is now divided into several sections which want continuous, and each segment, and not more than M (may be equal to M), Q is divided into many segments which can happened so as to satisfy the requirements.

Input and output format
input format:

The first line input file contains two positive integers N, M, represents the maximum length of each segment and the number of columns in A [i], the second row comprising N non-negative integers separated by a space A [i], as the subject.

Output formats:

Output file contains only a positive integer, the divided output minimum number of segments.

Input Output Sample
Input Sample # 1:

5 6
4 2 4 5 1

Output Sample # 1:

3
----------------
ideas: the first few records this and, if more than m, which cut section.

#include<iostream>
#include<cstdio> 
using namespace std;
int main(){
	int n;
	cin>>n;
	int a[n+1];
	int m,ans = 1, num = 0;
	cin>>m;
	for(int i = 0; i<n;i++)
	{
		scanf("%d",&a[i]);
		if(num +a[i] <= m)
		{
			num += a[i];
		}
		else
		{
			ans++;
			num = a[i];
		}
	}
	printf("%d\n",ans);
	return 0;
}
Published 80 original articles · won praise 1 · views 1474

Guess you like

Origin blog.csdn.net/zqhf123/article/details/104370548