NO.59十六届蓝桥杯备战|基础算法-前缀和|一维前缀和|最大子段和|二维前缀和|激光炸弹(C++)

前缀和与差分的核⼼思想是预处理,可以在暴⼒枚举的过程中,快速给出查询的结果,从⽽优化时间复杂度。
是经典的⽤空间替换时间的做法

前缀和

解法1:暴力模拟,q有几次,就套几次for循环
解法2:前缀和,快速求出数组中,某一段的区间和,O(1)
前缀和模板题,直接套⽤「公式」创建前缀和数组,然后利⽤前缀和数组的「性质」处理q 次查询。

  1. 创建前缀和数组:f[i] = f[i - 1] + a[i]
    ![[Pasted image 20250327163557.png]]

fi里面的值表示:a数组[1,i]区间所有元素的和
f[1] = a[1]
f[2] = a[1] + a[2] = f[1] + a[2]
f[3] = a[1] + a[2] + a[3] = f[2] + a[3]

f[i] = a[1] + a[2] + ... + a[i] = f[i-1] + a[i]
2. 查询[l, r]区间和:f[r] - f[l - 1]
![[Pasted image 20250327164734.png]]

前缀和数组,下标必须从1开始计数

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N = 1e5 + 10;

int n, q;
LL a[N];
LL f[N]; //前缀和数组

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    cin >> n >> q;
    for (int i = 1; i <= n; i++) cin >> a[i];
    //处理前缀和数组
    for (int i = 1; i <= n; i++)
    {
        f[i] = f[i-1] + a[i];
    }
    //处理q次询问
    while (q--)
    {
        int l, r; cin >> l >> r;
        cout << f[r] - f[l-1] << endl;
    }
    
    return 0;
}
P1115 最大子段和 - 洛谷

考虑以i 位置的元素a[i] 「为结尾」的最⼤⼦段和:

  • 在求「区间和」时,相当于是⽤f[i]减去i位置前⾯的某⼀个f[x]
  • 如果想要「最⼤⼦段和」,也就是「最⼤区间和」,那么⽤f[i]减掉⼀个「前驱最⼩值」即可。
    因此,我们可以创建a数组的「前缀和」数组,然后在遍历前缀和数组的过程中,⼀边「更新前驱最⼩值」,⼀边「更新当前位置为结尾的最⼤⼦段和」。
#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 10;

typedef long long LL;
int n;
LL f[N];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        LL x; cin >> x;
        f[i] = f[i-1] + x;
    }
    LL ret = -1e20;
    LL prevmin = 0;

    for (int i = 1; i <= n; i++)
    {
        ret = max(ret, f[i] - prevmin);
        prevmin = min(prevmin, f[i]);
    }
    cout << ret << endl;
    
    return 0;
}
二维前缀和

解法1:暴力模拟
解法2:前缀和
⼆维前缀和模板题,直接套⽤「公式」创建前缀和矩阵,然后利⽤前缀和矩阵的「性质」处理q次询问

  1. 创建前缀和矩阵:f[i][j] = f[i - 1][j] + f[i][j - 1] - f[i - 1][j - 1] + a[i][j]
    f[i][j]:表示从[1,1][i,j]区域内,所有元素的和
    ![[Pasted image 20250327182246.png]]

  2. 查询以(x1, y1 ) 为左上⻆(x2, y2 ) 为右下⻆的⼦矩阵的和
    ![[Pasted image 20250327175541.png]]

紫色区域=全-绿-粉-黄=全-(绿+粉)-(绿+黄)+绿=
f[x2][y2]-f[x1-1][y2]-f[x2][y1-1]+f[x1-1][y1-1]

#include <bits/stdc++.h>
using namespace std;

const int N = 1010;
typedef long long LL;
int n, m, q;
LL f[N][N];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    cin >> n >> m >> q;
    //预处理前缀和矩阵
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            LL x; cin >> x;
            f[i][j] = f[i-1][j] + f[i][j-1] - f[i-1][j-1] + x;
        }
    }
    //处理q次查询
    while (q--)
    {
        int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2;
        cout << f[x2][y2] - f[x1-1][y2] - f[x2][y1-1] + f[x1-1][y1-1] << endl;
    }
    
    return 0;
}
P2280 [HNOI2003] 激光炸弹 - 洛谷

解法:暴力枚举——枚举出所有边长为m的正方形;找出正方形中目标价值最大的那个

  1. 通过枚举m正方形的右下角的坐标来枚举出所有的m边长的正方形
  2. 利用二维前缀和快速求出正方形中所有目标的价值
#include <bits/stdc++.h>
using namespace std;

const int N = 5010;
int n, m;
int a[N][N];
int f[N][N];//前缀和矩阵

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> n >> m;
    while (n--)
    {
        int x, y, v; cin >> x >> y >> v;
        x++; y++; //下标从1开始计数
        a[x][y] += v; //同一个位置有可能有多个目标
    }
    n = 5001;
    //预处理
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            f[i][j] = f[i-1][j] + f[i][j-1] -f[i-1][j-1] + a[i][j];        
        }
    }
    int ret = 0;
    m = min(m, n);
    //枚举所有边长为m的正方形
    for (int x2 = m; x2 <= n; x2++)
    {
        for (int y2 = m; y2 <= n; y2++)
        {
            int x1 = x2 - m + 1, y1 = y2 - m + 1;
            ret = max(ret, f[x2][y2]-f[x1-1][y2]-f[x2][y1-1]+f[x1-1][y1-1]);
        }
    }
    cout << ret << endl;
    
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值