And the difference with the prefix

And the difference with the prefix

The difference with the prefix and two complementary? the concept of

Prefix and prefix differential values ​​of the original sequence difference and a prosequence also

This is very basic, I believe we will.

First, in terms of prefix and this thing and I am sure you will use prefix here mention only two-dimensional and how we should use

Order \ (val [i] [j ] \) is the current value of the node (generally the \ (1 * 1 \) grid), \ ([I] SUM [J] \) represents the prefix of the current point and

So according to the inclusion-exclusion principle (the principle of inclusion and exclusion really so hard, but play with the hand you can find)

\(sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+val[i][j]\)

If required \ (r * r \) grid value it

\(val[i][j]=sum[i][j]-s[i-r][j]-s[i][j-r]+s[i-r][j-r]\)

This will make the board issues a laser bombs!

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
int val[5010][5010],n,r,ans=0;
int main(){
    scanf("%d %d",&n,&r);
    for(int i=1,x,y,z;i<=n;i++){
        scanf("%d %d %d",&x,&y,&z);
        val[x+1][y+1]=z;
    }
    for(int i=1;i<=5001;i++)
        for(int j=1;j<=5001;j++)
            val[i][j]+=val[i-1][j]+val[i][j-1]-val[i-1][j-1];
    for(int i=0;i+r<=5000;i++)
        for(int j=0;j+r<=5000;j++)
            ans=max(ans,val[i+r][j+r]-val[i+r][j]-val[i][j+r]+val[i][j]);
    printf("%d",ans);
}

Finished prefix and then we'll talk about the difference

First modify the differential during the whole interval of time is very easy to use

If the use of violence, then this will all sections of the total number of modifications

Whereas if the difference that the sequence of words to the interval \ ([L, R & lt] \) \ (D + \)

We need to make difference sequence \ (B [L] + D \) , \ (B [R & lt +. 1] -d \) will be able to operate so that the single-point operation section to reduce the complexity and difficulty

IncDec Sequence

This question is to make all the same number, except such a difference corresponds to the sequence \ (b [1] \) the remainder are all 0

So this time, if we consider the number of operations to make a few first clear each operation will cause a +1 and a -1 operation

So we just have to claim \ ([2, n] \ ) the absolute value of p, and the interval of positive and negative and q

If \ (p! = Q \) obviously needs \ (b [1] \) differential

Then \ (ans = min (p, q) + | pq | \)

The possibility of their value as to ensure that all sequences have the same number of words corresponding to find \ (b [1] \) possibilities. So \ (b [1] \) the likelihood of \ (| pq | +1 \)

Put the code

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
long long ans,fangan,n,a[100010],b[100010],zheng,fu;
int main(){
    scanf("%lld",&n);
    for(long long i=1;i<=n;i++){
        scanf("%d",&a[i]);
        b[i]=a[i]-a[i-1];
    }
    b[n+1]=0;
    for(long long i=2;i<=n;i++){
        if(b[i]>0) zheng+=b[i];
        else fu-=b[i];  
    }
    ans=min(zheng,fu)+abs(zheng-fu);
    fangan=abs(zheng-fu)+1;
    printf("%lld\n%lld",ans,fangan);
}

Guess you like

Origin www.cnblogs.com/mendessy/p/11755885.html