The first day of learning python --- prefix and

1. 3956. Truncated array (prefix sum)

insert image description here
*** Question meaning: ***
It is required to cut an array twice and divide it into three parts, and then the array elements of these three parts are the same.
*** Idea: ***
Use the prefix sum to enumerate the position where the first knife appears. If the prefix sum of the position that appears is 2/3sum, then it means that one knife can be cut here, which is the second knife, and the first knife is added How many cutting methods are there; if the prefix sum is 1/3sum, then it means that one can be cut, which is the first cut.

n=int(input())#input的返回类型是str
a=list(map(int,input().split()))
s=sum(a)
if s%3:#不能被3整除说明不行
    print(0)
else:
    p=ans=pre=0
    for i in range(n-1):#i是第三段的起点,cnt表示有多少个j满足要求
        pre+=a[i]
        if pre==s*2//3:#'//'除完之后只取整数部分
            ans+=p
        if pre==s//3:
            p+=1
    print(ans)

Two, prefix and (prefix and)

insert image description here
This question is a typical prefix sum, remember this board.

[0]+list(map(int,input().split()))

The expression [0] in Python is used to create a list with a single element, which is the integer 0. When you combine [0] with the + operator, it concatenates this list with the result of the list(map(int,input().split()) expression.

In Python, the input() function reads a line of text from standard input (usually the keyboard), and the string's split() method splits the string into a list of substrings based on delimiters. The delimiter used by split() defaults to spaces, so input().split() splits the input line into a list of strings separated by spaces.

The map() function applies the int() function to each string in the list returned by split(), producing a list of integers.

So when you combine [0] with the + operator, you create a new list consisting of the integer 0 and the list of integers produced by map(int, input().split()) . The purpose of adding the integer 0 at the beginning of the list may depend on the context of the code, but it may be used as a placeholder or a tagged value.

n,m=map(int,input().split())
pre=[0]+list(map(int,input().split()))#一维数组的读取
for i in range(1,n+1):
    pre[i]+=pre[i-1]
for _ in range(m):
    l,r=map(int,input().split())
    print(pre[r]-pre[l-1])

3. The sum of sub-matrices (prefix sum)

insert image description here
Typical two-dimensional prefixes and

range(1,n+1)

When using the range() function in Python, it returns a sequence of numbers, starting at 0 and incrementing by 1 by default, until a specified number is stopped.
So when range(1, n+1) is used, it will generate a sequence of numbers starting from 1 and up to and including n, i.e. it will generate the numbers 1, 2, 3, ..., n.
Note that the upper bound specified in range() is always excluded in the sequence, which means that the last number generated will be n, not n+1.

n,m,q=map(int,input().split())#n行m列的矩阵,q次询问
a=[[0] for i in range(n+1)]#先构造一个一维数组
a[0]=[0]*(m+1)#m行矩阵
for i in range(1,n+1):
    a[i]+=list(map(int,input().split()))#读入
for i in range(1,n+1):
    for j in range(1,m+1):
        a[i][j]+=a[i-1][j]+a[i][j-1]-a[i-1][j-1]#初始化
for _ in range(q):
    x1,y1,x2,y2=map(int,input().split())
    print(a[x2][y2]+a[x1-1][y1-1]-a[x2][y1-1]-a[x1-1][y2])#二维前缀和

Four, K times interval (prefix sum)

insert image description here
If (a[i]-a[j])%k=0, it means that the remainders of the two %k are equal.

n,k=map(int,input().split())#n行m列的矩阵,q次询问
s=[0]
for i in range(1,n+1):
    s.append(int(input()))
    s[i]+=s[i-1]
cnt=[0]*k
cnt[0]+=1#处理s[0]=0的这种情况,后面是从1开始的,就要先把s[0]放进去
res=0
for i in range(1,n+1):
    res+=cnt[s[i]%k]
    cnt[s[i]%k]+=1
print(res)

5. Laser bomb (prefix and)

insert image description here

a=[[0]*5002 for _ in range(5002)]

This line of code creates a two-dimensional list named a with 5002 rows and 5002 columns, and each element is initialized to 0. In other words, this list is a 5002x5002 matrix that can be used to store two-dimensional data.

(n,r),N,res=map(int,input().split(' ')),0,0
a=[[0]*5002 for _ in range(5002)]
for _ in range(n):
    x,y,w=map(int,input().split(' '))
    N=max(N,x+1,y+1)
    a[x+1][y+1]+=w

for i in range(1,N+1):#预处理前缀和数组
    for j in range(1,N+1):
        a[i][j]+=a[i-1][j]+a[i][j-1]-a[i-1][j-1]

if r<N:#枚举所有边长是R的矩形,枚举(i,j)为右下角
    for i in range(r,N+1):
        for j in range(r,N+1):
            res=max(res,a[i][j]-a[i-r][j]-a[i][j-r]+a[i-r][j-r])
else:
    res=a[N][N]

print(res)

Guess you like

Origin blog.csdn.net/qq_51408826/article/details/129189489