Animal Transport


Problem Statement :


Capeta is working part-time for an animal shipping company. He needs to pick up animals from various zoos and drop them to other zoos. The company ships four kinds of animals: elephants, dogs, cats, and mice.

There are m zoos, numbered 1 to m. Also, there are n animals. For each animal i, Capeta knows its type ti (E for elephant, D for dog, C for cat and M for mouse), source zoo si where Capeta has to pick it up from, and destination zoo di where Capeta needs to deliver it to.

image

Capeta is given a truck with a huge capacity where n animals can easily fit. He is also given additional instructions:

1.He must visit the zoos in increasing order. He also cannot skip zoos.

2.Dogs are scared of elephants, so he is not allowed to bring them together at the same time.

3.Cats are scared of dogs, so he is not allowed to bring them together at the same time.
4.Mice are scared of cats, so he is not allowed to bring them together at the same time.
5.Elephants are scared of mice, so he is not allowed to bring them together at the same time.
Also, loading and unloading animals are complicated, so once an animal is loaded onto the truck, that animal will only be unloaded at its destination.

Because of these reasons, Capeta might not be able to transport all animals. He will need to ignore some animals. Which ones? The company decided to leave that decision for Capeta. He is asked to prepare a report and present it at a board meeting of the company.

Capeta needs to report the minimum number of zoos that must be reached so that she is able to transport x animals, for each x from 1 to n.

Complete the function minimumZooNumbers and return an integer array where the xth integer is the minimum number of zoos that Capeta needs to reach so that she is able to transport x animals, or -1 if it is impossible to transport x animals.

He is good at driving, but not so much at planning. Hence, he needs your help.

Input Format

The first line contains a single integer t, the number of test cases.

Each test case consists of four lines. The first line contains two space-separated integers m and n. The second line contains n space-separated characters t1,t2,...,tn. The third line contains n space-separated integers s1,s2,..,sn. The fourth line contains n space-separated integers d1,d2,...,dn.

ti, si and di are the details for the th animal, as described in the problem statement.

Constraints
1 <= t <= 10
1 <= m,n <= 5.10^4
1 <= si,di <= m
si != di
ti is either E, D, C or M

Subtasks

For 30% of the total score, m,n <=10^3
Output Format

For each case, print a single line containing n space-separated integers, where the xth integer is the minimum number of zoos that Capeta needs to reach so that she is able to transport x animals. If it is not possible to transport x animals at all, then put -1 instead.



Solution :



title-img


                            Solution in C :

In C++ :




#include"bits/stdc++.h"
#define F(i,j,n) for(register int i=j;i<=n;i++)
#define D(i,j,n) for(register int i=j;i>=n;i--)
#define ll long long
#define lc (k<<1)
#define rc (k<<1|1)
#define N 50010
using namespace std;
namespace io{
	const int L=(1<<19)+1;
	char ibuf[L],*iS,*iT,c;int f;
	char gc(){
		if(iS==iT){
			iT=(iS=ibuf)+fread(ibuf,1,L,stdin);
			return iS==iT?EOF:*iS++;
		}
		return*iS++;
	}
	template<class I>void gi(I&x){
		for(f=1,c=gc();c<'0'||c>'9';c=gc())if(c=='-')f=-1;
		for(x=0;c<='9'&&c>='0';c=gc())x=x*10+(c&15);x*=f;
	}
};
using io::gi;
using io::gc;
int t,n,m,p,f[N],h[N];char c;
struct no{
	int l,r,t; 
	bool operator<(const no&b)const{return r<b.r;}
}x[N];
struct qwq{
	int mx[N*4],tg[N*4];
	void clear(){
		memset(mx,0,sizeof(mx));
		memset(tg,0,sizeof(tg));
	}
	void add(int k,int x){
		mx[k]+=x;tg[k]+=x; 
	}
	void add(int k,int l,int r,int p,int q,int x){
//		printf("%d %d %d %d %d %d\n",k,l,r,p,q,x); 
		if(p<=l&&r<=q){add(k,x);return;}
		add(lc,tg[k]);add(rc,tg[k]);tg[k]=0;
		int m=l+r>>1;
		if(p<=m)add(lc,l,m,p,q,x);
		if(q>m)add(rc,m+1,r,p,q,x);
		mx[k]=max(mx[lc],mx[rc]);
	}
}a[2];
int main(){
	gi(t);
	while(t--){
		gi(n);gi(m);p=1;
		a[0].clear();a[1].clear();
		F(i,1,m){
			c=0;while(c<'A'||c>'Z')c=gc();
			x[i].t=(c=='E'||c=='C');
		}
		F(i,1,m)gi(x[i].l);
		F(i,1,m)gi(x[i].r);
		sort(x+1,x+m+1);
		F(i,1,m)h[i]=n+1;
		F(i,1,n){
			while(p<=m&&x[p].r==i)
                         {if(x[p].l<x[p].r)a[x[p].t].add(1,1,n,1,x[p].l,1);p++;}
			f[i]=max(a[0].mx[1],a[1].mx[1]);
			f[i]=max(f[i],f[i-1]);
			h[f[i]]=min(h[f[i]],i);
			a[0].add(1,1,n,i,i,f[i]);a[1].add(1,1,n,i,i,f[i]);
//			printf("%d %d\n",i,f[i]); 
		}
		D(i,m,1)h[i-1]=min(h[i-1],h[i]);
		F(i,1,m)printf("%d ",h[i]<=n?h[i]:-1);puts("");
	}
	return 0;
}








In Java :





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

public class Solution {

	static int[] minimumZooNumbers(int m, int n, char[] t, int[] s, int[] d) {
		int[] counter = new int[m + 1];

		for (int i = 0; i < n; i++)
			if (s[i] < d[i])
				counter[d[i]]++;

		int[][] ind = new int[m + 1][];

		for (int i = 1; i <= m; i++)
		{
			ind[i] = new int[counter[i]];
			counter[i] = 0;
		}

		for (int i = 0; i < n; i++)
			if (s[i] < d[i])
			{
				ind[d[i]][counter[d[i]]] = i;
				counter[d[i]]++;
			}

		Node whiteRoot = buildTree(1, m);
		Node blackRoot = buildTree(1, m);

		int[] maxAnimals = new int[m + 1];

		for (int i = 1; i <= m; i++)
		{
			for (int j = 0; j < counter[i]; j++)
			{
				int k = ind[i][j];

				if (t[k] == 'E' || t[k] == 'C')
				{
					andOne(blackRoot, s[k]);
				}
				else
				{
					andOne(whiteRoot, s[k]);
				}
			}

			int max =
				Math.max(blackRoot.value + blackRoot.plus,
					whiteRoot.value + whiteRoot.plus);

			insert(blackRoot, i, max);
			insert(whiteRoot, i, max);
			maxAnimals[i] = max;
		}

		int[] res = new int[n];
		int next = 0;

		for (int i = 1; i <= m; i++)
		{
			while (next < maxAnimals[i])
			{
				res[next] = i;
				next++;
			}
		}

		while (next < n)
		{
			res[next++] = -1;
		}

		return res;
	}

	static class Node
	{
		Node left;
		Node right;
		int leftBound;
		int rightBound;
		int value = 0;
		int plus = 0;
	}

	static Node buildTree(int left, int right)
	{
		Node node = new Node();
		node.leftBound = left;
		node.rightBound = right;

		if (left == right)
		{
			return node;
		}

		int mid = (left + right) / 2;

		node.left = buildTree(left, mid);
		node.right = buildTree(mid + 1, right);

		return node;
	}

	static void andOne(Node node, int r)
	{
		if (r >= node.rightBound)
		{
			node.plus++;
			return;
		}

		if (node.plus > 0)
		{
			node.left.plus += node.plus;
			node.right.plus += node.plus;
		}

		andOne(node.left, r);

		if (r >= node.right.leftBound)
		{
			andOne(node.right, r);
		}

		updateNode(node);
	}

	static void updateNode(Node node)
	{
		node.value = Math.max(node.left.value + node.left.plus,
			node.right.value + node.right.plus);
		node.plus = 0;
	}

	static void insert(Node node, int ind, int v)
	{
		if (node.leftBound == ind && node.rightBound == ind)
		{
			node.value = v;
			node.plus = 0;
			return;
		}

		if (node.plus > 0)
		{
			node.left.plus += node.plus;
			node.right.plus += node.plus;
		}

		if (node.left.rightBound >= ind)
		{
			insert(node.left, ind, v);
		}
		else
		{
			insert(node.right, ind, v);
		}

		updateNode(node);
	}

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int cases = in.nextInt();
		for(int a0 = 0; a0 < cases; a0++){
			int m = in.nextInt();
			int n = in.nextInt();
			char[] t = new char[n];
			for(int t_i = 0; t_i < n; t_i++){
				t[t_i] = in.next().charAt(0);
			}
			int[] s = new int[n];
			for(int s_i = 0; s_i < n; s_i++){
				s[s_i] = in.nextInt();
			}
			int[] d = new int[n];
			for(int d_i = 0; d_i < n; d_i++){
				d[d_i] = in.nextInt();
			}
			int[] result = minimumZooNumbers(m, n, t, s, d);
			for (int i = 0; i < result.length; i++) {
				System.out.print(result[i] + (i != result.length - 1 ? " " : ""));
			}
			System.out.println("");


		}
		in.close();
	}
}
                        








View More Similar Problems

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 →

Ticket to Ride

Simon received the board game Ticket to Ride as a birthday present. After playing it with his friends, he decides to come up with a strategy for the game. There are n cities on the map and n - 1 road plans. Each road plan consists of the following: Two cities which can be directly connected by a road. The length of the proposed road. The entire road plan is designed in such a way that if o

View Solution →

Heavy Light White Falcon

Our lazy white falcon finally decided to learn heavy-light decomposition. Her teacher gave an assignment for her to practice this new technique. Please help her by solving this problem. You are given a tree with N nodes and each node's value is initially 0. The problem asks you to operate the following two types of queries: "1 u x" assign x to the value of the node . "2 u v" print the maxim

View Solution →