fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int convert(string s){
  6. int u1 = int(s[0])-48;
  7. int u5 = int(s[1])-48;
  8.  
  9. int v = (u1*10 + u5)*60;
  10.  
  11. u1 = int(s[3])-48;
  12. u5 = int(s[4])-48;
  13.  
  14. v = v + (u1*10 + u5);
  15.  
  16. return v;
  17. }
  18.  
  19. void ck(int g){
  20. int u = g/60;
  21. int y = g%60;
  22.  
  23. if(u<=9){
  24. cout<<"0";
  25. cout<<u;
  26. }
  27. else{
  28. cout<<u;
  29. }
  30. cout<<":";
  31.  
  32. if(y<=9){
  33. cout<<"0";
  34. cout<<y;
  35. }
  36. else{
  37. cout<<y;
  38. }
  39. }
  40.  
  41. int main() {
  42. int n;
  43. cin>>n;
  44.  
  45. int k ;
  46. cin>>k;
  47.  
  48. int y[1445] = {0}; // increased size slightly to prevent overflow
  49.  
  50. for(int i=0;i<n;i++){
  51. string a,b,v1,v5;
  52. cin>>a>>b>>v1>>v5;
  53.  
  54. int t1 = convert(v1);
  55. int t5 = convert(v5);
  56.  
  57. y[t1] = y[t1] + 1;
  58. if(t5 + 1 < 1440) { // boundary check added
  59. y[t5+1] = y[t5+1] - 1;
  60. }
  61. }
  62.  
  63. int i=1;
  64. while(i<1440){
  65. y[i] = y[i] + y[i-1];
  66. i++;
  67. }
  68.  
  69. int c = 0 ; int g = 0 ;
  70. for(int i=0;i<1440;i++){
  71. if(y[i]==0){
  72. c++;
  73. if(c==k)
  74. {
  75. ck(i-k+1);
  76. g = 1 ;
  77. break ;
  78. }
  79. }
  80. else{
  81. c = 0 ;
  82. }
  83. }
  84.  
  85. if(g==0){
  86. cout<<"-1";
  87. }
  88. }
Success #stdin #stdout 0s 5328KB
stdin
Standard input is empty
stdout
-1