Frog Sort Solution Codechef

Frog Sort Solution February Challenge 2021

There are NN frogs (numbered 11 through NN) in a line. For each valid ii, the ii-th frog is initially at the position ii, it has weight WiWi, and whenever you hit its back, it jumps a distance LiLi to the right, i.e. its position increases by LiLi. The weights of the frogs are pairwise distinct.

You can hit the back of each frog any number of times (possibly zero, not necessarily the same for all frogs) in any order. The frogs do not intefere with each other, so there can be any number of frogs at the same time at each position.

Your task is to sort the frogs in the increasing order of weight using the smallest possible number of hits. In other words, after all the hits are performed, then for each pair of frogs (i,j)(i,j) such that Wi<WjWi<Wj, the position of the ii-th frog should be strictly smaller than the position of the jj-th frog. Find the smallest number of hits needed to achieve such a state.

Also See: February Long Challenge 2021 Solutions

Input

  • The first line of the input contains a single integer TT denoting the number of test cases. The description of TT test cases follows.
  • The first line of each test case contains a single integer NN.
  • The second line contains NN space-separated integers W1,W2,…,WNW1,W2,…,WN.
  • The third line contains NN space-separated integers L1,L2,…,LNL1,L2,…,LN.

Output

For each test case, print a single line containing one integer ― the smallest number of times you need to hit the backs of the frogs.

Constraints

  • 1≤T≤2⋅1041≤T≤2⋅104
  • 2≤N≤42≤N≤4
  • 1≤Wi≤N1≤Wi≤N for each valid ii
  • 1≤Li≤51≤Li≤5 for each valid ii
  • no two frogs have the same weight

Subtasks

Subtask #1 (50 points):

  • T=50T=50
  • N=2N=2

Subtask #2 (50 points): original constraints

Example Input

3
3
3 1 2
1 4 5
3
3 2 1
1 1 1
4
2 1 4 3
4 1 2 4

Example Output

3
6
5

Explanation

Example case 1: We can hit the back of the first frog three times.

Example case 2: We can hit the back of the first frog four times, then hit the back of the second frog two times.

Follow us on telegram for quick update an abundance of free knowledge: Click Here

Solution

Program C:

#include<stdio.h>
#include<string.h>

int main()
{

    int test;
    scanf("%d",&test);
    for(int i=0;i<test;i++)
    {
        int n;
        scanf("%d",&n);
        int w[n];
        int l[n];
        for(int j=0;j<n;j++)
        {
            scanf("%d",&w[j]);
        }
        for(int j=0;j<n;j++)
        {
            scanf("%d",&l[j]);
        }
        int a[n];

        for(int j=0;j<n;j++)
        {
            a[j]=w[j];
        }

        for(int j=0;j<n-1;j++)
        {
            for(int k=0;k<n-1-j;k++)
            {
                if(a[k]>a[k+1])
                {
                    int temp=a[k];
                    a[k]=a[k+1];
                    a[k+1]=temp;
                }
            }
        }
        int oldpos;
        for(int j=0;j<n;j++)
        {
            if(a[0]==w[j])
            {
                oldpos=j;
            }
        }
        int step=0;
        for(int j=1;j<n;j++)
        {
            int tempos,tempjump;
            for(int k=0;k<n;k++)
            {
                if(a[j]==w[k])
                {
                    tempos=k;
                    tempjump=l[k];
                }
            }
            int k;
            for(k=tempos;k<=oldpos;k=k+tempjump)
            {
                step++;
            }
            oldpos=k;

        }
        printf("%d\n",step);

    }

}

Program C++:

#include <bits/stdc++.h>
using namespace std;

int main() {
	int t; cin>>t;
	while(t--){
	    int n; cin>>n;
	    int k;
	    vector<int> w,l;
	    for(int i=0;i<n;i++){
	        cin>>k;
	        w.push_back(k);
	    }
	    
	    for(int i=0;i<n;i++){
	        cin>>k;
	        l.push_back(k);
	    }
	    map<int,pair<int,int>> mp;
	    for(int i=0;i<n;i++){
	        mp[w[i]] = {l[i],i};
	    }
	    int count = 0;
	    int curr;
	    for(auto it = mp.begin();it!=mp.end();it++){
	        if(it == mp.begin())
	            curr = it->second.second;
	        else{
	            int index = it->second.second;
	            while(curr >= index)
	            {
	                count++;
	                index += it->second.first;
	            }
	            curr = index;
	        }
	    }
	    cout<<count<<endl;
	}
	return 0;
}

Program Java:

import java.util.*;
class Solution {
	
    public static void main(String args[] ) throws Exception {
    	
    	 Scanner input=new Scanner(System.in);
    	 if(input.hasNext()) {
    	  int t=input.nextInt();
    	  for(int i=0;i<t;i++) {
    		int n=input.nextInt();
    		ArrayList<Integer> weights=new ArrayList<>();
    		ArrayList<Integer> jumps=new ArrayList<>();
    	
    		int pos[]=new int[n+1];
    		weights.add(0);
    		jumps.add(0);
    		for(int j=1;j<=n;j++) {
    			weights.add(input.nextInt());
    		}
    		for(int j=1;j<=n;j++) {
    			jumps.add(input.nextInt());
    			
    		}
    		int count=0;
    		for(int j=1;j<=n;j++) {
    			pos[j]=weights.indexOf(j);
    		}
    		for(int j=2;j<=n;j++) {
    			int temp1=pos[j];
    			int temp2=pos[j-1];
    			int temp=0;
    			
    			if(temp1<=temp2) {
    				
    			temp= (int) Math.ceil((float)(temp2+1-temp1)/(float)jumps.get(temp1));}
    			
    			count+=temp;
    			pos[j]+=(temp*jumps.get(temp1));
    			
    			
    		}
    		System.out.println(count);
    		
    	 }
    }
    	
    }
}

Program Python:

import math
for t in range(int(input())):
    n=int(input())
    weight=list(map(int,input().split()))
    lenght=list(map(int,input().split()))
    pak={}
    s=0
    for i in range(1,n+1):
        pak[i]=weight.index(i)
    for i in range(2,n+1):
            temp1=pak[i]
            temp2=pak[i-1]
            temp=0
            if temp1<=temp2:
                temp=(math.ceil((temp2+1-temp1)/lenght[temp1]))
            s+=temp
            pak[i]+=temp*(lenght[temp1])
                
    print(s)

Codechef Long Challenges

September Long Challenge 2021 Solution

February Long Challenge 2021

Leave a Comment