fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int n, k;
  6. cin >> n >> k;
  7. cin.ignore(); // important to consume the newline after k
  8.  
  9. vector<string> arr(n);
  10. vector<int> t(1440, 0); // minutes in a day
  11.  
  12. // Read each line properly
  13. for (int i = 0; i < n; i++) {
  14. getline(cin, arr[i]);
  15. while (arr[i].empty()) getline(cin, arr[i]); // skip empty lines
  16. }
  17.  
  18. // Parse and mark time intervals
  19. for (string s : arr) {
  20. istringstream ss(s);
  21. string name, activity, start, end;
  22. ss >> name >> activity >> start >> end;
  23.  
  24. if (start.size() < 5 || end.size() < 5) continue; // skip bad lines
  25.  
  26. int lhh = stoi(start.substr(0, 2));
  27. int lmm = stoi(start.substr(3, 2));
  28. int rhh = stoi(end.substr(0, 2));
  29. int rmm = stoi(end.substr(3, 2));
  30. int L = 60 * lhh + lmm;
  31. int R = 60 * rhh + rmm;
  32.  
  33. t[L]++;
  34. if (R != 1439) t[R + 1]--;
  35. }
  36.  
  37. // Build prefix sum
  38. for (int i = 1; i < 1440; i++) {
  39. t[i] += t[i - 1];
  40. }
  41.  
  42. // Find k-length zero interval
  43. int length = 0, time = -1;
  44. for (int i = 0; i < 1440; i++) {
  45. if (t[i] == 0) {
  46. length++;
  47. if (length == k) {
  48. time = i - k + 1;
  49. break;
  50. }
  51. } else {
  52. length = 0;
  53. }
  54. }
  55.  
  56. if (time == -1)
  57. cout << "No free slot" << endl;
  58. else
  59. cout << setw(2) << setfill('0') << time / 60 << ":"
  60. << setw(2) << setfill('0') << time % 60 << endl;
  61.  
  62. return 0;
  63. }
  64.  
Success #stdin #stdout 0.01s 5328KB
stdin
3 60 
Alex sleep 00:00 08:00
Sam sleep 07:00 13:00
Alex lunch 12:30 13:59

stdout
14:00