「一本通 1.1 练习 2」数列分段

题目传送门

解题思路

最简单的贪心思想,装满就加一,最后判断是否背包变量中还存在值,如果存在,整体的段数+1即可。

#include <bits/stdc++.h>
#define _for(i,a,n) for(int i=a;i<n;++i)
#define rep(i,a,n)for(int i=a;i<=n;++i)
#define input() int t;cin>>t;while(t--)
#define close() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int maxn = 1e5;
int arr[maxn + 5];

int main()
{
    int n, m;
    cin >> n >> m;
    _for(i, 0, n) cin >> arr[i];
    int tot = 0;
    int sum = 0;
    _for(i, 0, n) {
        sum += arr[i];
        if(sum > m) sum = arr[i], tot ++;
    }
    if(sum) tot ++;
    cout << tot <<endl;

    return 0;
}
posted @ 2018-12-25 09:53  schrodingercatss  阅读(209)  评论(0编辑  收藏  举报