Algorithms: prefix and use

On the use of the prefix and the two topics:

Topic 1: Suppose there are n individuals in a row, the value of everyone's knowledge is an integer, and now we want to select k consecutive people, making it a k personal knowledge and the maximum value, the maximum value of your output.

Input format
input line 2 comprising:

  • The first line has two integers n, k (1 <= k <= n <= 10 ^ 5), as a continuous number and the total number of selected human
  • The second row of n space-separated integers ti (1 <= ti <= 10 ^ 9), the value of each person's knowledge

Output format of
the output is an integer, and the maximum value of k is a continuous knowledge of the value of the individual

Sample input
. 5. 3
. 1. 3. 1 2 2
Sample Output
7

Method One: prefix and

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
long long t[100005], s[100005], ss = -1;
int main() {
    int n, k;
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        cin >> t[i];
        s[i] = s[i-1] + t[i];//s数组:前缀和
    }
    for(int i=k; i<=n; i++){//这里直接从k开始
        if(s[i] - s[i-k] > ss)
        	ss = s[i] - s[i-k];
	}
    cout << ss;
    return 0;
}

Method two: and no prefix, pointer movement can be achieved in a manner equivalent to pure enumeration!

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
long long t[100005],ss = -1;
int main() {
    int n, k;
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        cin >> t[i];
    }
    long long now = 0, ans = 0;
    for(int i=1; i<=n; i++){
        if(i <= k ){//now:连续k个数的和
            now += t[i];
		}else{
            now += t[i];
            now -= t[i-k];
		}
        if(i >= k){//ans:连续k个数的最大值
            ans = max(ans, now);
		}
	}
    cout << ans;
    return 0;
}

Problem 2: it is known by a ribbon of n different colors formed sequentially engaged, and the decorative effect of each color represented by an integer (including a positive integer, zero or negative integer), from left to right a1, a2, a3, ..., an, Xiaoming from which cut a continuous section used to decorate greeting cards, the decorative effect is this section of the sum of the respective colors decorative effect, Xiaoming need to select a decorative best period colors to create greeting cards (whichever and the color value of the maximum section), of course, if all colors decorative effects can only have a negative effect (i.e., ai <0), Bob can bring abandon decorative greeting card (decorative effect is obtained with the color 0 ).

Input format
input data consists of two lines:
the first line is an integer n, the number of colors on the ribbon, the second row there are n integers which were a1, a2, a3, ..., an (1 <= n < = 1000).

Output format
output data is only one integer representing the maximum number of greeting cards decorative effect can be obtained.

Sample input
. 5
-1 2 -1 2 -1
Sample Output
3

Method One: prefix and

#include<bits/stdc++.h>
using namespace std;
#define N 1005
int number[N], sum[N];
int main() {
    int n, mmax = -10001;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> number[i];
        sum[i] = sum[i-1] + number[i];
    }
    for(int i=1; i<=n; i++){
        for(int j=i; j<=n; j++){
            int result = sum[j] - sum[i-1];
            mmax = max(mmax, result);
		}
	}
    cout << mmax;
    return 0;
}

Method two: and no prefix, pointer movement can be achieved in a manner equivalent to pure enumeration!

#include<bits/stdc++.h>
using namespace std;
#define N 1005
int a[N];
int main(){
    int n, ans = 0;
    cin >> n;
    for(int i=1; i<=n; i++)
        cin >> a[i];
	for(int i=1; i<=n; i++){//i:枚举的左端点
        int s = 0;
        for(int j=i; j<=n; j++){//j:枚举右端点
            s += a[j];//s:左右断点的和
            ans  = max(ans, s);
		}
	}
    cout << ans;
    return 0;
}
Published 15 original articles · won praise 10 · views 212

Guess you like

Origin blog.csdn.net/qq_39053800/article/details/104244458