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

The Strange Function

One of the most important skills a programmer needs to learn early on is the ability to pose a problem in an abstract way. This skill is important not just for researchers but also in applied fields like software engineering and web development. You are able to solve most of a problem, except for one last subproblem, which you have posed in an abstract way as follows: Given an array consisting

View Solution →

Self-Driving Bus

Treeland is a country with n cities and n - 1 roads. There is exactly one path between any two cities. The ruler of Treeland wants to implement a self-driving bus system and asks tree-loving Alex to plan the bus routes. Alex decides that each route must contain a subset of connected cities; a subset of cities is connected if the following two conditions are true: There is a path between ever

View Solution →

Unique Colors

You are given an unrooted tree of n nodes numbered from 1 to n . Each node i has a color, ci. Let d( i , j ) be the number of different colors in the path between node i and node j. For each node i, calculate the value of sum, defined as follows: Your task is to print the value of sumi for each node 1 <= i <= n. Input Format The first line contains a single integer, n, denoti

View Solution →

Fibonacci Numbers Tree

Shashank loves trees and math. He has a rooted tree, T , consisting of N nodes uniquely labeled with integers in the inclusive range [1 , N ]. The node labeled as 1 is the root node of tree , and each node in is associated with some positive integer value (all values are initially ). Let's define Fk as the Kth Fibonacci number. Shashank wants to perform 22 types of operations over his tree, T

View Solution →

Pair Sums

Given an array, we define its value to be the value obtained by following these instructions: Write down all pairs of numbers from this array. Compute the product of each pair. Find the sum of all the products. For example, for a given array, for a given array [7,2 ,-1 ,2 ] Note that ( 7 , 2 ) is listed twice, one for each occurrence of 2. Given an array of integers, find the largest v

View Solution →

Lazy White Falcon

White Falcon just solved the data structure problem below using heavy-light decomposition. Can you help her find a new solution that doesn't require implementing any fancy techniques? There are 2 types of query operations that can be performed on a tree: 1 u x: Assign x as the value of node u. 2 u v: Print the sum of the node values in the unique path from node u to node v. Given a tree wi

View Solution →