子段和(所谓前缀和 + 模拟 plus)

11 篇文章 0 订阅
1 篇文章 0 订阅
文章讲述了如何使用前缀和算法解决找出数组中和大于等于给定值k的连续非空子段数量的问题。提供了两种方法,虽然看似不同,但实质都是利用前缀和优化时间复杂度。
摘要由CSDN通过智能技术生成

题目描述

给出一个长度为n的数组和一个整数k。

对于数字中连续的某一段,我们称之为子段。

请找到数组中有多少个连续的非空子段,使得这段数字的和大于等于k。

输入格式

第一行两个整数nn和kk。

第二行nn个整数aiai

1≤n≤1051≤n≤105

1≤ai≤1051≤ai≤105

1≤k≤10101≤k≤1010

输出格式

输出一个整数

样例输入

4 10

6 1 2 7

样例输出

2

提示

对于样例的解释:

从第1个数,到第4个数的和为16

从第2个数,到第4个数的和为10

16和10都大于等于10,因此答案为2

法一:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector> 
#include <map>
#include <stack>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
const int maxx = 1000050;
ll n, m, k, t, now, p, ans;
const double pi = acos(-1.0);
ll a[1000050], w[1000050];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> k;
    ll sum = 0;
    for (int i = 0; i < n; i++)//存入数组 
    {
        cin >> a[i];
    }
    int j = 0;
    for (int i = 0; i < n; i++)//依次开始遍历
    {
        sum += a[i];//sum这里代表的是所遍历到的数的和
        while (sum >= k && j <= i)//子段的遍历
        {
            sum -= a[j];//挨个进行删除观察是否符合
            ans += n - i;//用n - i 替代了一个数组的作用
            j++;//子段记录+1
        }
    }
    cout << ans << endl;
    return 0;
}

法二:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector> 
#include <map>
#include <stack>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
const int maxx = 1000050;
ll n, m, k, t, now, p , ans;
const double pi = acos(-1.0);
ll fa[1000050], w[1000050];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        int a;
        cin >> a;
        fa[i] = fa[i - 1] + a;//前缀和
    }
    for (int i = 1; i <= n; i++)
    {
        if (fa[i] < m)//当其前缀和都小于m时直接跳过
        {
            continue;
        }
        ans++;//否则就记录一次
        w[i] = w[i - 1];//为上一次记录的个数(至少有这么多)
        for (int j = w[i] + 1; j < i; j++)//之所以+1是因为不+1的那部分子段已经在上一轮被计算过了
        {
            if (fa[i] - fa[j] >= m)//进行子段的查找(呈收缩状
            {
                w[i] ++;
            }
            else//如果不满足的就退出
            {
                break;
            }
        }
        ans += w[i];//加上子段的个数
    }
    cout << ans << endl;
    return 0;
}

个人感觉法一法二无实质区别,均是采用前缀和的思想进行时间和空间的节省

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tang_7777777

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值