【寒假任务】 洛谷1181 数列分段section I

问题描述
给出一段数列,要求将他们分段,每段之和不得超过m,问最少要分几段。
样例输入
5 6
4 2 4 5 1
样例输出
3
算法讨论
因为是要连着分段的,所以没那么难,贪心即可。

const
  maxn=100000;
var
  a:array[1..maxn] of longint;
  i,j,n,m,ans:longint;
  s:int64;
begin
  read(n,m);
  for i:=1 to n do
    read(a[i]);
  for i:=1 to n do
    begin
      inc(s,a[i]);
      if s>m
        then begin
               inc(ans);
               s:=a[i]
             end;
    end;
  write(ans+1) 
end.

【寒假任务】 洛谷1181 数列分段section I_第1张图片
Pixiv ID:42149642

你可能感兴趣的:(寒假任务,贪心)