Chef and Meetings Solution Codechef

Chef and Meetings Solution February Challenge 2021

time is a string in the format "HH:MM AM" or "HH:MM PM" (without quotes), where HH and MM are always two-digit numbers. A day starts at 12:00 AM and ends at 11:59 PM. You may refer here for understanding the 12-hour clock format.

Chef has scheduled a meeting with his friends at a time PP. He has NN friends (numbered 11 through NN); for each valid ii, the ii-th friend is available from a time LiLi to a time RiRi (both inclusive). For each friend, can you help Chef find out if this friend will be able to attend the meeting? More formally, check if Li≤P≤RiLi≤P≤Ri for each valid ii.

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 time PP.
  • The second line contains a single integer NN.
  • NN lines follow. For each valid ii, the ii-th of these lines contains two space-separated times LiLi and RiRi.

Output

For each test case, print a single line containing one string with length NN. For each valid ii, the ii-th character of this string should be ‘1’ if ii-th friend will be able to attend the meeting or ‘0’ otherwise.

Constraints

  • 1≤T≤5001≤T≤500
  • 1≤N≤5001≤N≤500
  • each time is valid in the 12-hour clock format
  • for each valid ii, the time RiRi is greater or equal to LiLi

Subtasks

Subtask #1 (100 points): original constraints

Example Input

2
12:01 AM
4
12:00 AM 11:42 PM
12:01 AM 11:59 AM
12:30 AM 12:00 PM
11:59 AM 11:59 PM
04:12 PM
5
12:00 AM 11:59 PM
01:00 PM 04:12 PM
04:12 PM 04:12 PM
04:12 AM 04:12 AM
12:00 PM 11:59 PM

Example Output

1100
11101

Explanation

Example case 1:

  • Friend 11: 12:01 AM lies between 12:00 AM and 11:42 PM (that is, between 00:00 and 23:42 in the 24-hour clock format), so this friend will be able to attend the meeting.
  • Friend 22: 12:01 AM lies between 12:01 AM and 11:59 AM (between 00:01 and 11:59 in the 24-hour clock format).
  • Friend 33: 12:01 AM does not lie between 12:30 AM and 12:30 PM (between 00:30 and 12:30 in the 24-hour clock format), so this friend will not be able to attend the meeting.
  • Friend 44: 12:01 AM does not lie between 11:59 AM and 11:59 PM (between 11:59 and 23:59 in the 24-hour clock format).

Example case 2: For friend 33, 04:12 PM lies between 04:12 PM and 04:12 PM (inclusive) and hence this friend will be able to attend the meeting.

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

Solution

Program C:

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

#define NR_MIN_IN_HOUR 60
#define NR_HOURS_TILL_PM 12

int conversion(int hh, int mm, int tip){
  int minutes;
  minutes = mm;
  minutes = minutes + hh * NR_MIN_IN_HOUR;
  if(tip == 1){
    minutes = minutes + NR_HOURS_TILL_PM * NR_MIN_IN_HOUR;
  }
  if(hh == 12){
    minutes = minutes - NR_HOURS_TILL_PM * NR_MIN_IN_HOUR;
  }
  return minutes;
}

int main()
{
    int t, p, n, i, j, hh, mm, l, r, tip;
    char ch;
    scanf("%d", &t);
    for(i = 0; i < t; i++){
      scanf("%d:%d", &hh, &mm);
      ch = getc(stdin);
      ch = getc(stdin);
      if(ch == 'A'){
        tip = 0;
      }else{
        tip = 1;
      }
      ch = getc(stdin);
      ch = getc(stdin);
      p = conversion(hh, mm, tip);
      scanf("%d", &n);
      for(j = 0; j < n; j++){
        scanf("%d:%d", &hh, &mm);
        ch = getc(stdin);
        ch = getc(stdin);
        if(ch == 'A'){
          tip = 0;
        }else{
          tip = 1;
        }
        ch = getc(stdin);
        ch = getc(stdin);
        l = conversion(hh, mm, tip);
        scanf("%d:%d", &hh, &mm);
        ch = getc(stdin);
        ch = getc(stdin);
        if(ch == 'A'){
          tip = 0;
        }else{
          tip = 1;
        }
        ch = getc(stdin);
        ch = getc(stdin);
        r = conversion(hh, mm, tip);
        if((l <= p) && (p <= r)){
          printf("1");
        }else{
          printf("0");
        }
      }
      printf("\n");
    }
    return 0;
}

Program C++:

#include <iostream>
#include <string.h>
using namespace std;

int convert(string S){
    if(S[6]=='A' && S[0]=='1' && S[1]=='2'){
        return 10*(S[3]-'0')+(S[4]-'0');
    }
    if(S[6]=='P' && (10*(S[0]-'0')+(S[1]-'0'))<12){
        return 1000*((S[0]-'0')+1) + 100*((S[1]-'0')+2) + 10*(S[3]-'0')+(S[4]-'0');
    }
    return 1000*(S[0]-'0') + 100*(S[1]-'0')+ 10*(S[3]-'0')+(S[4]-'0');
   
}
int main() {
	int T;
	cin>>T;
	cin.ignore();
	while(T--){
	    string P;
	    getline(cin,P);
	    int N;
	    cin>>N;
	    cin.ignore();
	    string LR[N],L[N],R[N];
	    for(int i=0;i<N;i++){
	        getline(cin,LR[i]);
	        L[i]=LR[i].substr(0,8);
	        R[i]=LR[i].substr(9,8);
	    }
	    int p = convert(P);
	    for(int i=0;i<N;i++){
	        int l = convert(L[i]);
	        int r = convert(R[i]);
	        if(l<=p && p<=r) cout<<'1';
	        else cout<<'0';
	    }
	    cout<<"\n";
	}
	return 0;
}

Program Java:

import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
	public static void main (String[] args) throws IOException
	{
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		try{
		    int T = Integer.parseInt(br.readLine());
		    while(T-->0){
		        String P = br.readLine();
		        int N = Integer.parseInt(br.readLine());
		        ArrayList<Integer> req = new ArrayList<Integer>();
		        while(N-->0){
		            String time = br.readLine();
		            int Hi = calculateHour(time,0);
		            int Hf = calculateHour(time,9);
		            int Mi = calculateMinute(time,0);
		            int Mf = calculateMinute(time,9);
		            
		            int CH = calculateHour(P,0);
		            int CM = calculateMinute(P,0);
		            
		            int timeIn = (Hi * 100) + Mi;
		            int timeFin = (Hf * 100) + Mf;
		            
		            int CT = (CH * 100) + CM;
		            
		            if(CT>=timeIn && CT<=timeFin){
		                req.add(1);
		            }
		            else{
		                req.add(0);
		            }
		        }
		        String result = req.toString().replace("[", "").replace("]", "").replace(",","").replace(" ","");
		        System.out.println(result);
		    }
		}
		catch(Exception e){
		    return;
		}
	}
	public static int calculateHour(String time, int t){
	    int hour;
	    hour = 10*(time.charAt(0+t)-'0') + 1*(time.charAt(1+t)-'0');
	    
	    if(time.charAt(6+t)=='P'){
	        if(hour!=12)
	            hour+=12;
	    }
	    if(time.charAt(6+t)=='A'){
	        if(hour==12)
	            hour-=12;
	    }
	    return hour;
	}
	public static int calculateMinute(String time, int t){
	    int minute;
	    minute = 10*(time.charAt(3+t)-'0') + 1*(time.charAt(4+t) - '0');
	    return minute;
	}
}

Program Python:

def timetomin(s):
    ans=0
    if "AM" in s:
        if s[:2]!="12":
            ans=int(s[:2])*60 + int(s[3:5])
        else :
            ans=int(s[3:5])
    else :
        if s[:2]!="12":
            ans=(int(s[:2])+12)*60 + int(s[3:5])
        else:
            ans=720+int(s[3:5])
    return ans
for t in range(int(input())):
    s=''
    p=input()
    p=timetomin(p)
    n=int(input())
    for i in range(n):
        time=input()
        time1=timetomin(time[:8])
        time2=timetomin(time[9:])
        if (p>=time1 and p<=time2):
            s+='1'
        elif time2<time1 and (p>=time2 or p<=time1):
            s+='1'
        else :
            s+='0'
    print(s)

Codechef Long Challenges

September Long Challenge 2021 Solution

February Long Challenge 2021

Leave a Comment