Codeforces 1197D Yet Another Subarray Problem (prefix + and violence statistics)

Topic link: http: //codeforces.com/problemset/problem/1197/D

Title effect: a given array and two constants m, k, and define the cost of some sequence elements equal to the sequence of each piece minus the sum of the length's direction and take the product of m and k is an integer value. Seeking the largest cost take any period of continuous sequences can get. (Ps: you can take an empty set, then the sub-sequence is zero cost)

Ideas: because it involves intervals and operation, you can use the prefix and pre-processing, the query is simplified On O1. Special Notes piece by the subject can be the final answer must be greater than or equal to 0, because when taking cost nonempty obtained are less than zero, we can take a null cost set as the maximum value is 0, so it is not necessary at the time of initialization the array is initialized to some trouble -inf such numbers.

Then, the cost calculation formula (r-l + 1) / m of the rounding up approximates the sequence is divided into blocks, all by less than one m is calculated. Therefore, we can m is less than or equal to a certain point statistical maximum before (inclusive) , and the topic is not a large amount of data, we can direct violence statistics.

#include<iostream>
#include<cstdio>
#include<set>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<stdlib.h>
#include<queue>
#include<algorithm>
#include<map>
#include<stack>
using namespace std;
long long a[300005];
long long f[300005];
int n,m,k;
int main()
{
    scanf("%d%d%d",&n,&m,&k);
    for(int i=1;i<=n;i++)
    {
        scanf("%I64d",&a[i]);
        a[i]+=a[i-1];
    }
    long long ans=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=i;j>=1&&j>=i-m+1;j--)
        {
            F [I] = max (A [I] -a [J- . 1 ] - K, F [I]); 
        } 
        IF (IM> 0 ) 
        { 
            F [I] = max (A [I] -a [IM ] + F [im] -k, F [I]); // if the previous point is defined im and adding a larger value, then it (i.e. two could be combined to obtain larger cost), the two the combined value of the segment to the rear passage continues statistical 
        } 
        ANS = max (ANS, F [I]); 
    } 
    the printf ( " % I64d \ n- " , ANS);
     return  0 ; 
}

 

Guess you like

Origin www.cnblogs.com/forever3329/p/11231679.html