Subsequence Weighting


Problem Statement :


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) = v1 + v2 + ... + vM.
Task:
Given a sequence, output the maximum weight formed by an increasing subsequence.

Input:
The first line of input contains a single integer T. T test-cases follow. The first line of each test-case contains an integer N. The next line contains a1, a2 ,... , aN separated by a single space. The next line contains w1, w2, ..., wN separated by a single space.

Output:
For each test-case output a single integer: The maximum weight of increasing subsequences of the given sequence.

Constraints:
1 <= T <= 5
1 <= N <= 150000
1 <= ai <= 109, where i ∈ [1..N]
1 <= wi <= 109, where i ∈ [1..N]

Sample Input:

2  
4  
1 2 3 4  
10 20 30 40  
8  
1 2 3 4 1 2 3 4  
10 20 30 40 15 15 15 50
Sample Output:

100  
110



Solution :



title-img


                            Solution in C :

In  C++ :





#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <climits>

using namespace std;

#define GI ({int new_input;scanf("%d",&new_input);new_input;})
typedef unsigned long long ll;



ll Tree[800000];
void updateTree(int b, int e, int p, ll  val, int idx=1) {
	if(p < b || p > e) return ;
	if(p == b && p == e){ 
		Tree[idx] = max(Tree[idx],val);
		return ;
	}
	int mid = (b+e)/2;
	int lt = (idx<<1);
	int rt = ((idx<<1)+1);
	updateTree(b, mid, p, val, lt);
	updateTree(mid+1, e, p, val, rt);
	Tree[idx] = max(Tree[lt], Tree[rt]);
	return ;
}
ll query(int b,int e,int start,int end,int node){
	if(e<start || b>end)return 0;
	if(b<=start && e>=end)return Tree[node];
	int mid=(start+end)>>1;
	return max(query(b,e,start,mid,node*2),query(b,e,mid+1,end,node*2+1));	
}
ll input[200000];
ll w[200000];
map<ll,int>m;
set<ll>s;
int main() {
    int t=GI;
    while(t--){
		m.clear();s.clear();
		s.empty();
		memset(Tree,0,sizeof Tree);
		int n=GI;
		for(int i=0;i<n;i++){
			scanf("%lld",&input[i]);
			s.insert(input[i]);
		}
		for(int i=0;i<n;i++){
			scanf("%lld",&w[i]);
		}
		int in=1;
		set<ll>::iterator it;
		for(it=s.begin();it!=s.end();it++){
			m[*it]=in;
			in++;
		}in--;
		ll ans=0;
		for(int i=0;i<n;i++){
			int mapped=m[input[i]];
			if(mapped==1){
				updateTree(1,in,mapped,w[i],1);
				ans=max(ans,w[i]);
			}
			else{
				ll get=query(1,mapped-1,1,in,1);
				ans=max(ans,get+w[i]);
				updateTree(1,in,mapped,w[i]+get,1);
			}
		}
		cout<<ans<<endl;
	}
    return  0;
}







In Java :







import java.util.Map.Entry;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.SortedMap;
import java.util.TreeMap;

public class Solution {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int  nProb = sc.nextInt();
		for(int k = 1; k <= nProb; ++k) {
			int n = sc.nextInt();
			int[] a = new int[n];
			int[] w = new int[n];
			for(int i = 0; i < n; ++i)
				a[i] = sc.nextInt();
			for(int i = 0; i < n; ++i)
				w[i] = sc.nextInt();
			long bestW = solve(a, w);
			System.out.println(bestW);
		}
	}

	private static long solve(int[] a, int[] w) {
		int n = a.length;
		long best = 0;
		TreeMap<Integer, Long> map = new TreeMap<Integer, Long>();
		for(int k = 0; k < n; ++k) {
			Entry<Integer, Long> e = map.lowerEntry(a[k]);
			long b = (e == null ? 0 : e.getValue()) + w[k];
			SortedMap<Integer, Long> tail = map.tailMap(a[k]);
			List<Integer> del = new ArrayList<Integer>();
			for(Entry<Integer, Long> x : tail.entrySet()) {
				if(x.getValue().longValue() > b)
					break;
				del.add(x.getKey());
			}
			for(Integer i : del) {
				map.remove(i);
			}
			if(!map.containsKey(a[k]))
				map.put(a[k], b);
			if(best < b)
				best = b;
		}
		return best;
	}
}









In C :






#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct treap {
    int x, p;
    long long y;
    struct treap *l, *r;
}* Treap;

Treap td = NULL;

Treap newTreap(int x, long long y) {
    Treap t;
    if (td) {
        t = td;
        td = td->r;
    }
    else
        t = (Treap) malloc(sizeof(struct treap));
    t->x = x;
    t->y = y;
    t->p = rand();
    t->l = t->r = NULL;
    return t;
}

void dump(Treap t) {
    if (t) {
        dump(t->l);
        dump(t->r);
        t->r = td;
        td = t;
    }
}

Treap merge(Treap l, Treap r) {
    if (!l)
        return r;
    if (!r)
        return l;
    if (l->p > r->p) {
        l->r = merge(l->r, r);
        return l;
    }
    r->l = merge(l, r->l);
    return r;
}

void split(Treap t, Treap *l, Treap *r, long long v, int d) {
    if (!t)
        *l = *r = NULL;
    else if (d ? (t->x < v) : (t->y <= v)) {
        split(t->r, &t->r, r, v, d);
        *l = t;
    }
    else {
        split(t->l, l, &t->l, v, d);
        *r = t;
    }
}

Treap rightmost(Treap t) {
    if (t) {
        while (t->r)
            t = t->r;
    }
    return t;
}

Treap leftmost(Treap t) {
    if (t) {
        while (t->l)
            t = t->l;
    }
    return t;
}

long long solve() {
    int n, i;
    long long v;
    scanf("%d", &n);
    int a[n], w[n];
    for (i = -1; ++i < n; scanf("%d", a + i));
    for (i = -1; ++i < n; scanf("%d", w + i));
    Treap r = newTreap(0, 0), l, m;
    for (i = -1; ++i < n;) {
        split(r, &l, &r, a[i], 1);
        m = rightmost(l);
        v = w[i] + m->y;
        split(r, &m, &r, v, 0);
        if (m) {
            dump(m);
            m = newTreap(a[i], v);
        }
        else {
            m = leftmost(r);
            if (!m || m->x > a[i])
                m = newTreap(a[i], v);
            else
                m = NULL;
        }
        l = merge(l, m);
        r = merge(l, r);
    }
    v = rightmost(r)->y;
    dump(r);
    return v;
}

int main() {
    srand(time(NULL));
    int t;
    scanf("%d", &t);
    while (t--)
        printf("%lld\n", solve());
    return 0;
}









In Python3 :






import os
import sys
import bisect
# Complete the solve function below.
def solve(a, w):
    b = [[0,0],[10000000000,10000000000]]
    for i in range(len(a)):
        g = [a[i],w[i]]
        bisect.insort(b,g)
        ind = b.index(g)
        if b[ind+1][0] != b[ind][0] and b[ind-1][0] != b[ind][0]:
            b[ind][1]+=b[ind-1][1]
            for j in range(ind+1,len(b)):
                if b[j][1] >b[ind][1]:
                    break
            b = b[:ind+1] + b[j:]
        elif b[ind+1][0] == b[ind][0]:
            b[ind][1]+=b[ind-1][1]
            if b[ind+1][1]>=b[ind][1]:
                b.remove(b[ind])
            else:
                b.remove(b[ind+1])
                for j in range(ind+1,len(b)):
                    if b[j][1]>b[ind][1]:
                        break
                b = b[: ind+1] + b[j: ]
        elif b[ind-1][0] ==b[ind][0]:
            b[ind][1] += b[ind-2][1]
            if b[ind-1][1] >= b[ind][1]:
                b.remove(b[ind])
            else:
                for j in range(ind+1,len(b)):
                     if b[j][1]>b[ind][1]:
                        break
                b = b[: ind+1] + b[j: ]
                b.remove(b[ind-1])
    return b[-2][1]
if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')
    t = int(input())
    for t_itr in range(t):
        n = int(input())
        a = list(map(int, input().rstrip().split()))
        w = list(map(int, input().rstrip().split()))
        result = solve(a, w)
        fptr.write(str(result) + '\n')
    fptr.close()
                        








View More Similar Problems

Costly Intervals

Given an array, your goal is to find, for each element, the largest subarray containing it whose cost is at least k. Specifically, let A = [A1, A2, . . . , An ] be an array of length n, and let be the subarray from index l to index r. Also, Let MAX( l, r ) be the largest number in Al. . . r. Let MIN( l, r ) be the smallest number in Al . . .r . Let OR( l , r ) be the bitwise OR of the

View Solution →

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 →