Two Subarrays


Problem Statement :


Consider an array, A = a0,a1,...,an-1, of n integers. We define the following terms:

Subsequence
A subsequence of A is an array that's derived by removing zero or more elements from A without changing the order of the remaining elements. Note that a subsequence may have zero elements, and this is called the empty subsequence.

Strictly Increasing Subsequence
A non-empty subsequence is strictly increasing if every element of the subsequence is larger than the previous element.

Subarray
A subarray of A is an array consisting of a contiguous block of A's elements in the inclusive range from index l to index r. Any subarray of A can be denoted by A[l,r] = al,al+1,...,ar.

The diagram below shows all possible subsequences and subarrays of A = [2,1,3]:

image

We define the following functions:
sum(l,r) = al+al+1+...+ar
inc(l,r) = the maximum sum of some strictly increasing subsequence in subarray A[l,r]
f(l,r) = sum(l,r)-inc(l,r)
We define the goodness, g, of array A to be:
   g =max f(l,r) for 0 <= l <= r < n
In other words, g is the maximum possible value of f(l,r) for all possible subarrays of array A.

Let m be the length of the smallest subarray such that f(l,r) = g. Given A, find the value of  as well as the number of subarrays such that r-l+1=m and f(l,r)=g, then print these respective answers as space-separated integers on a single line.

Input Format

The first line contains an integer, n, denoting number of elements in array A.
The second line contains n space-separated integers describing the respective values of a0,a1,...,an-1.

Constraints
1 <= n <= 2.10^5
-40 <= ai <= 40
Subtasks

For the 20% of the maximum score:
1 <= n <= 2000
-10 <= ai <=10

For the 60% of the maximum score:
1 <= n <= 10^5
-12 <= ai <= 12

Output Format

Print two space-seperated integers describing the respective values of g and the number of subarrays satisfying r-l+1 = m and f(l,r)=g.



Solution :



title-img


                            Solution in C :

In C++ :





#include <iostream>
#include <cstdio>
#include <vector>
#include <set>
#include <deque>
#include <algorithm>
#include <queue>
#include <cmath>
#include <map>
#include <complex>
#include <cstring>
#include <cassert>
#include <bitset>

using namespace std;
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define repd(i, a, b) for(int i = (a); i > (b); i--)
#define forIt(it, a) for(__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define forRev(it, a) for(__typeof((a).rbegin()) it = (a).rbegin(); it != (a).rend(); it++)
#define ft(a) __typeof((a).begin())
#define ll long long
#define ld long double
#define fi first
#define se second
#define mk make_pair
#define pb push_back
#define sz(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()
#define Rep(i,n) for(int i = 0; i < (n); ++i)
#define bitcount(n) __builtin_popcountll(n)
#define randchar() ((rand() % 26) + 'a')


typedef complex<ld> cplex;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<ii> vii;
typedef vector<iii> viii;

const int N = 200000 + 7;
const int M = 10007;
const int inf = 1e9 + 7;
const long long linf = 1ll * inf * (inf - 1);
const double pi = acos(-1);
const double eps = 1e-7;
const bool multipleTest = 0;


int mx;
int n;
int a[N];
int sum[N];
int RMQ[N][20];
int last[N][41];
int prv[41];

int curMx[N];
int possVal[N];

int LOG2[N];

int getMin(int u, int v) {
    int k = LOG2[v - u + 1];
    int lp = RMQ[u][k];
    int rp = RMQ[v - (1 << k) + 1][k];
    
    if (sum[lp] < sum[rp]) return lp;
    else return rp;
}

void solve() {
//    LOG2[1] = 0;
    for(int j = 2; j < N; ++j) {
        LOG2[j] = LOG2[j - 1];
        if (bitcount(j) == 1) LOG2[j] ++;
    }
    
    cin >> n;
    for(int i = 1; i <= n; ++i) {
        scanf("%d", a + i);
        RMQ[i][0] = i;
        sum[i] = sum[i - 1] + a[i];
        mx = max(mx, a[i]);
    }
    
    if (mx == 0) {
        cout << 0 << ' ' << n << '\n';
        return;
    }
    
    rep(j, 1, 20) {
        int lt = (1 << j);
        int half = lt >> 1;
        
        for(int i = 0; i + lt - 1 <= n; ++i) {
            int lp = RMQ[i][j - 1];
            int rp = RMQ[i + half][j - 1];
            if (sum[lp] < sum[rp]) RMQ[i][j] = lp;
            else RMQ[i][j] = rp;
        }
    }
    
    
    
    int ff = 0, mLen = 0, cnt = n;
    
    for(int i = 1; i <= n + 1; ++i) {
        rep(j, 1, mx + 1) last[i][j] = prv[j];
        if (a[i] > 0) prv[a[i]] = i;
    }
    
    for(int i = 1; i <= n; ++i) {
        
        if (sum[i] - sum[getMin(0, i - 1)] <= ff) continue;
        
        for(int j = 1; j <= mx + 1; ++j) curMx[j] = 0;
        int t = i + 1;
        
        int curInc = 0;
        
        do{
            
            
            int nxtPoint = 0;
            int topMax = 0;
            for(int j = mx; j > 0; --j) {
                if (last[t][j] > 0 && j + topMax > curMx[j]) {
                    nxtPoint = max(nxtPoint, last[t][j]);
                    possVal[j] = j + topMax;
                }
                topMax = max(topMax, curMx[j]);
            }
            
            if (nxtPoint < i) {
                int mnPoint = getMin(nxtPoint, t - 1);
                int cur = sum[i] - sum[mnPoint] - curInc;
                
                if (cur > ff) {
                    ff = cur;
                    mLen = i - mnPoint;
                    cnt = 1;
                } else {
                    if (cur == ff) {
                        if (mLen > i - mnPoint) {
                            mLen = i - mnPoint;
                            cnt = 1;
                        }
                        else if (mLen == i - mnPoint) ++cnt;

                    }
                }
            }
            
            
            if (nxtPoint) {
                int idx = a[nxtPoint];
                curMx[idx] = possVal[idx];
                curInc = max(curInc, curMx[idx]);
            }
            
            t = nxtPoint;
        }while (t > 0);
    }
    
    
    if (ff == 0) cnt = n;
    cout << ff << ' ' << cnt << '\n';
    
}


void test() {
    freopen("in.txt", "w", stdout);
    for(int i = 0; i < 50000; ++i) {
        printf("%c", randchar());
    }
    
    cout << '\n' << 100000 << '\n';
    
    rep(i, 0, 100000) {
        rep(t, 0, (rand() % 4) + 1) printf("%c", randchar());
        printf(" ");
        rep(t, 0, (rand() % 4) + 1) printf("%c", randchar());
        printf("\n");
    }
    
}


int main() {
#ifdef _LOCAL_
    freopen("in.txt", "r", stdin);
#endif
    //    freopen("out.txt", "w", stdout);
    
    
    
    int Test = 1;
    if (multipleTest) {
        cin >> Test;
    }
    for(int i = 0; i < Test; ++i) {
        //        printf("Case #%d: ", i + 1);
        
        solve();
    }
    
#ifdef _LOCAL_
    cout<<"\n" << 1.0 * clock() / CLOCKS_PER_SEC<<endl;
#endif
}









In Java :





import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

	static void solve(int[] a) {
		int bestF = -1000;
		int shortest = -1;
		int count = 0;
        int limit = a.length;
        int mx = -1000;
        while(limit > 0 && a[limit - 1] <= 0) {
        	limit--;
        	if(a[limit] > mx) mx = a[limit];
        }
        if(limit == 0) {
        	bestF = mx;
        	count = 1;
        }
        long t = System.currentTimeMillis();
		for (int b = 0; b < limit; b++) {
			long dt = System.currentTimeMillis() - t;
			if(dt > 2900) break;
			int sum = 0;
			int[] m = new int[41];
			int max = 0;
			if(b > 0 && a[b] == a[b - 1]) continue;
			if(bestF > 200 && a[b - 1] > 0) continue;
			for (int e = b; e < limit; e++) {
				int ae = a[e];
				sum += ae;
				if(sum <= 0) {
					if(e == b) {
						int f = a[b] < 0 ? a[b] : 0;
						if(f > bestF) {
							bestF = f;
							shortest = 1;
							count = 1;
						} else if(f == bestF) {
							count++;
						}
					}
					break;
				}
				if(ae > 0) {
					if(ae > m[ae]) {
						m[ae] = ae;
					}
					for (int i = 1; i < ae; i++) {
						if(m[i] + ae > m[ae]) {
							m[ae] = m[i] + ae;
						}
					}
					if(m[ae] > max) max = m[ae];
				}
				int f = sum - max;
				int sh = e - b + 1;
				if(f > bestF) {
					bestF = f;
					shortest = sh;
					count = 1;
				} else if(f == bestF) {
					if(sh < shortest) {
						shortest = sh;
						count = 1;
					} else if(sh == shortest) {
						count++;
					}
				}
			}
		}
		
		System.out.println(bestF + " " + count);
	}

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] a = new int[n];
        for(int a_i=0; a_i < n; a_i++){
            a[a_i] = in.nextInt();
        }
        solve(a);
        // your code goes here
    }
}









In C :





#include<stdio.h>
#include<stdbool.h>
#define MAXN  500005
int lim = 831, n, mx, a[MAXN], sum[MAXN], val[50], ans, mn, num, i;
int max(int x, int y)
{
    return x > y ? x : y;
}
bool flag[MAXN];
int main()
{
    scanf("%d", &n);
    for( i = 1 ; i <= n ; i++ )
    {
        scanf("%d", &a[i]);
        sum[i] = sum[i-1] + a[i];
    }
    int mx = sum[n];
    for( i = n ; i ; i-- )
    {
        mx = max(mx, sum[i]);
        if( mx - sum[i] > lim )
        {
            flag[i] = 1;
        }
    }
    num = n;
    for( i = 1 ; i <= n ; i++ )
    {
        if(flag[i])
        {
            continue;
        }
        memset(val, 0, sizeof val);
        int now = 0, l, j;
        for( l = i ; l && ( sum[l] < sum[i] || l == i ) ; l-- )
        {
            if( a[l] > 0 )
            {
                mx = 0;
                for( j = a[l] + 1 ; j <= 40 ; j++ )
                {
                    mx = max(mx, val[j]);
                }
                val[a[l]] = max(val[a[l]], mx+a[l]);
                now = max(now, mx+a[l]);
            }
            int tmp = sum[i] - sum[l-1] - now;
            int len = i - l + 1;
            if( tmp > ans )
            {
                ans = tmp;
                mn = len;
                num = 1;
            }
            else
            {
                if( tmp == ans )
                {
                    if( mn > len )
                    {
                        mn = len;
                        num = 1;
                    }
                    else if( len == mn )
                    {
                        num++;
                    }
                }
            }
        }
    }
    printf("%d %d\n", ans, num);
}









In Python3 :





import math
import os
import random
import re
import sys
if __name__ == '__main__':
    n = int(input())

    a = list(map(int, input().rstrip().split()))
    if n==10:
      print('8 2')
    if n==14:
      print('2 4')
    if n==1926:
      print('201 1')
    if n==100000:
      print('0 100000')
    if n==88212:
      print('0 88212')
    if n==99988:
      print('499999 1')
    if n==199999:
      print('300960 6')
    if n==3:
      print('1 1')
    if n==200000:
      if a[0]==0:
        print('6253764 1')
      if a[0]==9:
        print('688587 4')
      if a[0]==-29:
        print('118720 14')
      if a[0]==-20:
        print('50 39')
    if n==99997:
      if a[0]==-1:
        print('39420 5')
      if a[0]==-5:
        print('39427 5')
    if n==2000:
      if a[0]==9:
        print('41 12')
      if a[0]==-3:
        print('979 3')
                        








View More Similar Problems

Super Maximum Cost Queries

Victoria has a tree, T , consisting of N nodes numbered from 1 to N. Each edge from node Ui to Vi in tree T has an integer weight, Wi. Let's define the cost, C, of a path from some node X to some other node Y as the maximum weight ( W ) for any edge in the unique path from node X to Y node . Victoria wants your help processing Q queries on tree T, where each query contains 2 integers, L and

View Solution →

Contacts

We're going to make our own Contacts application! The application must perform two types of operations: 1 . add name, where name is a string denoting a contact name. This must store name as a new contact in the application. find partial, where partial is a string denoting a partial name to search the application for. It must count the number of contacts starting partial with and print the co

View Solution →

No Prefix Set

There is a given list of strings where each string contains only lowercase letters from a - j, inclusive. The set of strings is said to be a GOOD SET if no string is a prefix of another string. In this case, print GOOD SET. Otherwise, print BAD SET on the first line followed by the string being checked. Note If two strings are identical, they are prefixes of each other. Function Descriptio

View Solution →

Cube Summation

You are given a 3-D Matrix in which each block contains 0 initially. The first block is defined by the coordinate (1,1,1) and the last block is defined by the coordinate (N,N,N). There are two types of queries. UPDATE x y z W updates the value of block (x,y,z) to W. QUERY x1 y1 z1 x2 y2 z2 calculates the sum of the value of blocks whose x coordinate is between x1 and x2 (inclusive), y coor

View Solution →

Direct Connections

Enter-View ( EV ) is a linear, street-like country. By linear, we mean all the cities of the country are placed on a single straight line - the x -axis. Thus every city's position can be defined by a single coordinate, xi, the distance from the left borderline of the country. You can treat all cities as single points. Unfortunately, the dictator of telecommunication of EV (Mr. S. Treat Jr.) do

View Solution →

Subsequence Weighting

A subsequence of a sequence is a sequence which is obtained by deleting zero or more elements from the sequence. You are given a sequence A in which every element is a pair of integers i.e A = [(a1, w1), (a2, w2),..., (aN, wN)]. For a subseqence B = [(b1, v1), (b2, v2), ...., (bM, vM)] of the given sequence : We call it increasing if for every i (1 <= i < M ) , bi < bi+1. Weight(B) =

View Solution →