洛谷 P1181数列分段Section I

星爆气流(弃疗)斩!                                            ——《刀剑神域》

题目:https://www.luogu.org/problem/P1181

这题真的是水题啊。不过他的提示略微具有迷惑性。

它把4 2 4 5 1分成三段[4][2 4][5 1],但是按我的做法,他应该这么分:[4 2][4][5 2]

这么看做法就很明显了,从头向后扫描,当前面的数的和超过了m就把当前的和变成0,让段数加一。

废话不多说,上代码。

#include<bits/stdc++.h>
using namespace std;
int n,m;
int a[100010];
int ans=1;//这里是1,因为就算不去分,也有1段
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    int p=1,sum=0;
    while(p<=n)
    {
        sum+=a[p];
        if(sum<=m) p++;
        else
        {
            ans++;
            sum=0;
        }
    }
    printf("%d",ans);
    return 0;
}

 

posted @ 2019-08-14 16:45  crxscp-173  阅读(84)  评论(0编辑  收藏  举报