2022.4.18讲课——基础算法前缀和和差分

目录

795. 前缀和 - AcWing题库基础知识

797. 差分 - AcWing题库基础知识

前缀和应用

差分应用


795. 前缀和 - AcWing题库基础知识

2022.4.18讲课——基础算法前缀和和差分_第1张图片

 

#include 
#include 
#include 

using namespace std;

int main(){
    int n, m;
    cin >> n >> m;
    
    int *p = new int[n],i = 0;
    int *s = new int[n];
    while(i < n && cin >> p[i++]);
    for (int j = 1; j <= n; j ++ ) s[j] = s[j - 1] + p[j - 1];
    int l,r;
    while(cin >> l >> r){
        cout << s[r] - s[l - 1] << endl;
    }
}

796. 子矩阵的和 - AcWing题库

2022.4.18讲课——基础算法前缀和和差分_第2张图片

#include 

using namespace std;

const int N = 1010;

int n, m, q;
int s[N][N];

int main()
{
    cin >> n >> m >> q;

    for (int i = 1; i <= n; i ++ )
        for (int j = 1; j <= m; j ++ )
            cin >> s[i][j];

    for (int i = 1; i <= n; i ++ )
        for (int j = 1; j <= m; j ++ )
            s[i][j] += s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1];

    while (q -- )
    {
        int x1, y1, x2, y2;
        cin >> x1 >> y1 >> x2 >> y2;
        cout << s[x2][y2] - s[x1 - 1][y2] - s[x2][y1 - 1] + s[x1 - 1][y1 - 1] << endl;
    }

    return 0;
}

 

797. 差分 - AcWing题库基础知识

2022.4.18讲课——基础算法前缀和和差分_第3张图片

#include
using namespace std;
const int N = 1e5+10;
int a[N], b[N];
int main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
    {
        scanf("%d", &a[i]);
        b[i] = a[i] - a[i - 1];
    }
    int l, r, c;
    while (m--)
    {
        scanf("%d%d%d", &l, &r, &c);
        b[l] += c;
        b[r + 1] -= c;
    }
    for (int i = 1; i <= n; i++)
    {
        a[i] = b[i] + a[i - 1];
        printf("%d ", a[i]);
    }
    return 0;
}

​​​​​​798. 差分矩阵 - AcWing题库

 2022.4.18讲课——基础算法前缀和和差分_第4张图片

#include/*矩阵的每一项是差分矩阵的前缀和*/
using namespace std;
const int N = 1010;
int b[N][N];
void insert(int x1, int y1, int x2, int y2, int c)/*矩阵*/
{
    b[x1][y1] += c;
    b[x2 + 1][y1] -= c;
    b[x1][y2 + 1] -= c;
    b[x2 + 1][y2 + 1] += c;
}
int main()
{
    int n, m, q, t;
    scanf("%d%d%d", &n, &m, &q);
    for (int i = 1; i <= n; i ++ )
        for(int j = 1;j <= m;j ++){
            scanf("%d", &t);
            insert(i,j,i,j,t);
        }
    
    int x1,y1,x2,y2,w;
    for(int i = 0;i < q;i ++){
        scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&w);
        insert(x1,y1,x2,y2,w);
    }
    
    for (int i = 1; i <= n; i ++ )
        for(int j = 1;j <= m;j ++){
            b[i][j] += b[i-1][j] + b[i][j-1]-b[i-1][j-1];
        }
    
    for (int i = 1; i <= n; i++){
            for (int j = 1; j <= m; j ++ ){
            printf("%d ",b[i][j]);
        }
        printf("\n");
    }
    return 0;
}

 

前缀和应用

1230. K倍区间 - AcWing题库(一维数组前缀和)

2022.4.18讲课——基础算法前缀和和差分_第5张图片

//前缀和
//区间i~j的和为a[j] - a[i-1],是k的倍数
//所以(a[j] - a[i-1]) % k == 0
//转化为a[i-1] % k == a[j] % k
//强调区间顺序,从左往右在一边前缀和累加的同时,一边计算答案

#include 
#include 
#include 
using namespace std;
const int N = 1e5+10;
int a[N],has[N];
int main()
{
    int n,k;cin >> n >> k;
    for (int i = 1; i <= n; i ++ )scanf("%d",a+i);
    
    for (int i = 1; i <= n; i ++ )a[i] = (a[i-1] + a[i]) % k;
    
    long long ans = 0;
    for (int i = 0; i <= n; i ++ ){
        ans += has[a[i]];
        has[a[i]]++;//只要余数相等,那就是一种可能的答案
    }
    cout << ans << endl;
    return 0;
}

99. 激光炸弹 - AcWing题库(二维数组前缀和)

2022.4.18讲课——基础算法前缀和和差分_第6张图片

#include 
#include 
#include 
using namespace std;
const int N = 5010;
int w[N][N];
int main()
{
    int n,r;cin >> n >> r;
    int x,y,k;
    for (int i = 0; i < n; i ++ ){
        cin >> x >> y >> k;
        w[x+1][y+1] += k;
    }
    for(int i = 1;i < 5010;i++){
        for(int j = 1;j < 5010;j ++){
            w[i][j] += w[i][j-1] + w[i-1][j] - w[i-1][j-1];
        }
    }
    if(r >= 5000){
        cout << w[5001][5001] << endl;
        return 0;
    }
    
    int ans = 0;
    
    
    
    for (int i = r; i <= 5000; i ++ ){
        for (int j = r; j <= 5000; j ++ ){
            ans = max(ans, w[i][j] - w[i - r][j] - w[i][j - r] + w[i - r][j - r]);
        }
    }
    cout << ans << endl;
    return 0;
}

 

差分应用

507. 积木大赛 - AcWing题库(一维数组差分)

2022.4.18讲课——基础算法前缀和和差分_第7张图片

#include 
#include 

using namespace std;

const int N = 100010;

int n;
int h[N];

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i ++ ) scanf("%d", &h[i]);

    int res = 0;
    for (int i = n; i; i -- ) res += max(0, h[i] - h[i - 1]);

    cout << res << endl;

    return 0;
}

 

P5542 [USACO19FEB]Painting The Barn S - 洛谷​​​​​​(二维数组差分)

2022.4.18讲课——基础算法前缀和和差分_第8张图片

#include
#include
using namespace std;
const int N = 1010;
int a[N][N];
int main(){
    int n,k;cin >> n >> k;
    for(int i = 0;i < n;i ++){
        int x1,y1,x2,y2;
        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
        x1++;x2++;y1++;y2++;
        a[x1][y1]++;//a数组表示对应点的坐标,不是方格坐标
        a[x1][y2]--;//因为面积是从端点计算来的
        a[x2][y1]--;//比如涂1 1 1 1,等于没图
        a[x2][y2]++;
    }
    int ans = 0;
    for(int i = 1;i <= 1000;i ++){
        for(int j = 1;j <= 1000;j ++){
            a[i][j] = a[i][j] + a[i-1][j] + a[i][j-1] - a[i-1][j-1];
        }
    }
    for(int i = 1;i <= 1000;i ++){
        for(int j = 1;j <= 1000;j ++){
            if(a[i][j] == k)ans++;
        }
        //puts("");
    }
    cout << ans << endl;
    return 0;
}

谢谢观看!!!

你可能感兴趣的:(c++)