【u101】数列分段1

阅读: 评论:0

【u101】数列分段1

【u101】数列分段1

Time Limit: 1 second
Memory Limit: 128 MB

【问题描述】

对于给定的一个长度为N的正整数数列A[i],现要将其分成连续的若干段,并且每段和不超过M(可以等于M),问最少能将其分成多少段使得满足要求。

【输入格式】

输入文件divide_a.in的第1行包含两个正整数N,M,表示了数列A[i]的长度与每段和的最大值,第2行包含N个空格隔开的非负整数A[i],如题目所述。

【输出格式】

输出文件divide_a.out仅包含一个正整数,输出最少划分的段数。

【数据规模】

对于20%的数据,有N≤10; 对于40%的数据,有N≤1000; 对于100%的数据,有N≤100000,M≤109,M大于所有数的最小值,A[i]之和不超过109。

Sample Input1

5 6
4 2 4 5 1

Sample Output1

3

【样例说明】

将数列如下划分:
[4][2 4][5 1]
第一段和为4,第2段和为6,第3段和为6均满足和不超过M=6,并可以证明3是最少划分的段数。

【题目链接】:.asp?id=u101

【题解】

每一段尽量多地放元素就好->能放就放满(因为是连续的,所以也没有说像背包问题那样有后效性);
注意开LL吧。

【完整代码】

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se secondtypedef pair<int,int> pii;
typedef pair<LL,LL> pll;void rel(LL &r)
{r = 0;char t = getchar();while (!isdigit(t) && t!='-') t = getchar();LL sign = 1;if (t == '-')sign = -1;while (!isdigit(t)) t = getchar();while (isdigit(t)) r = r * 10 + t - '0', t = getchar();r = r*sign;
}void rei(int &r)
{r = 0;char t = getchar();while (!isdigit(t)&&t!='-') t = getchar();int sign = 1;if (t == '-')sign = -1;while (!isdigit(t)) t = getchar();while (isdigit(t)) r = r * 10 + t - '0', t = getchar();r = r*sign;
}const int MAXN = 1e5+100;
const int dx[5] = {0,1,-1,0,0};
const int dy[5] = {0,0,0,-1,1};
const double pi = acos(-1.0);int n;
LL m;
LL a[MAXN];int main()
{//freopen("F:\","r",stdin);rei(n);rel(m);rep1(i,1,n)rel(a[i]);int now = 1;LL temp = a[1];rep1(i,2,n)if (temp+a[i]>m){temp = a[i];now++;}elsetemp+=a[i];cout << now<<endl;return 0;
}

本文发布于:2024-01-31 17:30:30,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170669343130201.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:数列
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23