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

Game of Two Stacks

Alexa has two stacks of non-negative integers, stack A = [a0, a1, . . . , an-1 ] and stack B = [b0, b1, . . . , b m-1] where index 0 denotes the top of the stack. Alexa challenges Nick to play the following game: In each move, Nick can remove one integer from the top of either stack A or stack B. Nick keeps a running sum of the integers he removes from the two stacks. Nick is disqualified f

View Solution →

Largest Rectangle

Skyline Real Estate Developers is planning to demolish a number of old, unoccupied buildings and construct a shopping mall in their place. Your task is to find the largest solid area in which the mall can be constructed. There are a number of buildings in a certain two-dimensional landscape. Each building has a height, given by . If you join adjacent buildings, they will form a solid rectangle

View Solution →

Simple Text Editor

In this challenge, you must implement a simple text editor. Initially, your editor contains an empty string, S. You must perform Q operations of the following 4 types: 1. append(W) - Append W string to the end of S. 2 . delete( k ) - Delete the last k characters of S. 3 .print( k ) - Print the kth character of S. 4 . undo( ) - Undo the last (not previously undone) operation of type 1 or 2,

View Solution →

Poisonous Plants

There are a number of plants in a garden. Each of the plants has been treated with some amount of pesticide. After each day, if any plant has more pesticide than the plant on its left, being weaker than the left one, it dies. You are given the initial values of the pesticide in each of the plants. Determine the number of days after which no plant dies, i.e. the time after which there is no plan

View Solution →

AND xor OR

Given an array of distinct elements. Let and be the smallest and the next smallest element in the interval where . . where , are the bitwise operators , and respectively. Your task is to find the maximum possible value of . Input Format First line contains integer N. Second line contains N integers, representing elements of the array A[] . Output Format Print the value

View Solution →

Waiter

You are a waiter at a party. There is a pile of numbered plates. Create an empty answers array. At each iteration, i, remove each plate from the top of the stack in order. Determine if the number on the plate is evenly divisible ith the prime number. If it is, stack it in pile Bi. Otherwise, stack it in stack Ai. Store the values Bi in from top to bottom in answers. In the next iteration, do the

View Solution →