zoukankan      html  css  js  c++  java
  • 洛谷 P1181 数列分段Section I

    题目描述

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

    输入输出格式

    输入格式:

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

    输出格式:

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

    输入输出样例

    输入样例#1:
    5 6
    4 2 4 5 1
    输出样例#1:
    3

    说明

    对于20%的数据,有N≤10;

    对于40%的数据,有N≤1000;

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

    将数列如下划分:

    [4][2 4][5 1]

    第一段和为4,第2段和为6,第3段和为6均满足和不超过M=6,并可以证明3是最少划分的段数。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cstring>
     5 #include <string>
     6 
     7 using namespace std;
     8 const int N=100010;
     9 
    10 inline void read(int & x)
    11 {
    12     char c=getchar();
    13     x=0;
    14     while(c<'0'&&c>'9')c=getchar();
    15     while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();
    16 }
    17 
    18 int a[N];
    19 
    20 int main()
    21 {
    22     int n,m;
    23     read(n);
    24     read(m);
    25     for(int i=1;i<=n;i++)
    26     {
    27         read(a[i]);
    28     }
    29     int ans(0);
    30     for(int i=1;i<=n;i++)
    31     {
    32         int j=i;
    33         while(a[j]<=m)
    34         {
    35             a[j]+=a[i+1];
    36             i++;
    37         }
    38         ans++;
    39         i--;
    40     }
    41     printf("%d",ans);
    42 }
  • 相关阅读:
    有关HL7 的C# 源码
    EF中调整字段的顺序
    xml schema 中如何定义类似Map的结构
    js将时间转换为时间戳
    postman使用
    H5拖拽
    读取XML文件中获取特定值
    读取XML文件
    owin使用
    C#跨线程访问
  • 原文地址:https://www.cnblogs.com/lyqlyq/p/6875723.html
Copyright © 2011-2022 走看看