College Life 5 Codechef Solution

There is only 1 TV in the common room, and as luck would have it, both the El Clasico football match and the India-Pakistan cricket match are happening simultaneously.

There is one football fan who is looking at the live commentary online and will scream whenever an ‘important’ event takes place in the El Clasico. Similarly, there is a cricket fan who will do so for every important event in the cricket match.

You are given two arrays F1,F2,…,Fn, which denote the times when an important event happens in the football match. And similarly C1,C2,…,Cm for cricket. You sadly have the remote in hand. You start out by watching the El Clasico. But whenever an Important event happens in the sport which isn’t on the TV right now, you will be forced to switch to that sport’s channel, and this continues, i.e., you switch channels if and only if when an important event in the other sport happens. Find the total number of times you will have to switch between the channels.

Input:

  • First line will contain TT, number of testcases. Then the testcases follow.
  • Each testcase contains of 33 lines of input.
  • First line contain 22 space separated integers NN and MM, number of important events in football and cricket match respectively.
  • Second line contain NN space separated integers FiFi, where the ithith index represents ithith important event in the football match.
  • Third line contain MM space separated integers CiCi, where the ithith index represents ithith important event in the cricket match.

Output:

For each testcase, output in a single line answer to the problem.

Constraints

  • 1≤N,M≤2.5∗1041≤N,M≤2.5∗104
  • 1≤Fi,Ci≤1091≤Fi,Ci≤109
  • Fi>Fi−1Fi>Fi−1 ∀i>0∀i>0
  • Ci>Ci−1Ci>Ci−1 ∀i>0∀i>0
  • FiFi != CjCj ∀i,j∀i,j
  • Sum of NN over all tests is atmost 2∗1052∗105
  • Sum of MM over all tests is atmost 2∗1052∗105

Sample Input 1 

3
2 2
1 3
2 4
3 1
100 200 300
1
1 2
1
100 200

Sample Output 1 

3
2
1

Explanation

Case 1: At T=0T=0, we are watching El Clasico. At T=1T=1 there is an important event in football match and since we are already watching the same channel we won’t switch. At T=2T=2, there is an important event in cricket match and we switch the channel for the first time. At T=3T=3, there is an important event in football match and we switch the channel for the second time. At T=4T=4, there is an important event in cricket match and we switch the channel for the third time. So in total we switch three times.

Case 2: At T=0T=0, we are watching El Clasico. At T=1T=1 there is an important event in cricket match and we switch the channel for the first time. At T=100T=100, there is an important event in football match and we switch the channel for the second time. At T=200T=200, there is an important event in football match and since we are on the same channel, we don’t switch. At T=300T=300, there is an important event in football match and since we are on the same channel, we don’t switch. So in total we switch two times.

Case 3: At T=0T=0, we are watching El Clasico. At T=1T=1 there is an important event in football match and since we are on the same channel, we don’t switch. At T=100T=100, there is an important event in cricket match and we switch the channel for the first time. At T=200T=200, there is an important event in cricket match and since we are on the same channel, we don’t switch. So in total we switch only one time.

College Life 5   – CodeChef Solution in JAVA

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		FastReader sc = new FastReader();
		int t = sc.nextInt();
		
		while(t-->0){
		    int  n = sc.nextInt();
		    int m = sc.nextInt();
		    long foot[] = new long[n];
		    long cric[] = new long[m];
		    for(int i=0;i<n;i++){
		        foot[i] = sc.nextLong();
		    }
		    for(int i=0;i<m;i++){
		        cric[i] = sc.nextLong();
		    }
		    int i=0;
		    int j=0;
		    boolean curr = true;
		    int ans = 1;
		    while(i<n && j<m){
		        if(foot[i]>cric[j]){
		            if(curr == true){
		                curr = false;
		                ans++;
		                j++;
		            }else{
		                j++;
		            }
		        }else if(foot[i]<cric[j]){
		            if(curr == false){
		                curr = true;
		                ans++;
		                i++;
		            }else{
		                i++;
		            }
		        }
		    }
		    System.out.println(ans);
		}
	}
	static class FastReader {
        BufferedReader br;
        StringTokenizer st;
 
        public FastReader()
        {
            br = new BufferedReader(
                new InputStreamReader(System.in));
        }
 
        String next()
        {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
 
        int nextInt() { return Integer.parseInt(next()); }
 
        long nextLong() { return Long.parseLong(next()); }
 
        double nextDouble()
        {
            return Double.parseDouble(next());
        }
 
        String nextLine()
        {
            String str = "";
            try {
                str = br.readLine();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }}

College Life 5  – CodeChef Solution in CPP

#include<bits/stdc++.h>
#define in(arr, n) for (int i=0; i<n; i++) cin >> arr[i]
#define out(arr, n) for (int i=0; i<n; i++) cout << arr[i] << "\n"
#define ll long long int
using namespace std;

int main () {

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("input1.trt", "w", stdin);
    freopen("output1.trt", "w", stdout);
    #endif

    int t;
    cin >> t;

    
    while (t--) {
        ll n,m,count=0;
	    int pre=1,count2=0;
	    cin>>n>>m;
	    ll z;

	    set<pair<long int,int>> t;

	    for(int i=0;i<n;i++){
	        cin>>z;
	        t.insert({z,1});
	    }
	    for(int i=0;i<m;i++){
	        cin>>z;
	        t.insert({z,0});
	    }
	    for(auto it:t){
	        if(it.second==0) count2=1;
	        if(count2==1){
	            if(pre!=it.second){
	                count++;}
	            pre=it.second;
	        }}
	        cout<<count<<"\n";
    }

    



    return 0;
}

College Life 5 -CodeChef Solution in Python

for _ in range(int(input())):
    N, M = map(int, input().split())
    football = list(map(int, input().split()))
    cricket = list(map(int, input().split()))
    count = 0
    f = 0
    c = 0
    curren = 'F'
    while(f < N and c < M):
        if(football[f] < cricket[c]):
            if(curren != "F"):
                count += 1
                curren = "F"
            f += 1
        elif(cricket[c] < football[f]):
            if(curren != "C"):
                count += 1
                curren = "C"
            c += 1
    if(f != N and curren == 'C'):
        count += 1
    if(c != M and curren == 'F'):
        count += 1
    print(count)


Disclaimer: The above Problem (College Life 5) is generated by CodeChef but the solution is provided by Codeworld19.This tutorial is only for Educational and Learning purpose.

Leave a Reply

Your email address will not be published. Required fields are marked *