fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. inline int power(int a, int b) {
  6. int x = 1;
  7. while (b) {
  8. if (b & 1) x *= a;
  9. a *= a;
  10. b >>= 1;
  11. }
  12. return x;
  13. }
  14.  
  15.  
  16. const int M = 1000000007;
  17. const int N = 3e5+9;
  18. const int INF = 2e9+1;
  19. const int LINF = 2000000000000000001;
  20.  
  21. //_ ***************************** START Below *******************************
  22.  
  23.  
  24.  
  25.  
  26. vector<string> events;
  27.  
  28. string formatTime(int time){
  29. string hr = time/60 < 10 ? "0" + to_string(time/60) : to_string(time/60);
  30. string min = time%60 < 10 ? "0" + to_string(time%60) : to_string(time%60);
  31.  
  32. return hr + ":" + min;
  33. }
  34.  
  35. void consistency(int n, int k) {
  36. int m = 1440;
  37.  
  38. vector<int> dif(m, 0);
  39.  
  40. for(auto& e : events){
  41. stringstream str(e);
  42. string name, action, start, end;
  43. str >> name >> action >> start >> end;
  44.  
  45. string startHr = start.substr(0, 2);
  46. string startMin = start.substr(3);
  47. string endHr = end.substr(0, 2);
  48. string endMin = end.substr(3);
  49.  
  50. int startTime = stoi(startHr)*60 + stoi(startMin);
  51. int endTime = stoi(endHr)*60 + stoi(endMin);
  52.  
  53. dif[startTime]++;
  54. if(endTime+1<m) dif[endTime+1]--;
  55. }
  56.  
  57. for(int i=1; i<m; i++){
  58. dif[i] += dif[i-1];
  59. }
  60.  
  61.  
  62. int last = -1;
  63. for(int i=0; i<m; i++){
  64.  
  65. if(dif[i] == 0 ){
  66. if(i-last >= k){
  67. cout << formatTime(last+1) << endl;
  68. return;
  69. }
  70.  
  71. }
  72. else last = i;
  73. }
  74.  
  75.  
  76. cout << -1 << endl;
  77.  
  78.  
  79. }
  80.  
  81. void solve() {
  82.  
  83. int n, k;
  84. cin >> n >> k;
  85.  
  86. cin.ignore();
  87. events.resize(n);
  88. for(int i=0; i<n; i++){
  89. string line;
  90. getline(cin, line);
  91.  
  92. events[i] = line;
  93. }
  94.  
  95. consistency(n, k);
  96. }
  97.  
  98.  
  99.  
  100.  
  101.  
  102. int32_t main() {
  103. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  104.  
  105. int t = 1;
  106. cin >> t;
  107.  
  108. cin.ignore();
  109.  
  110. while (t--) {
  111. solve();
  112. }
  113.  
  114. return 0;
  115. }
Success #stdin #stdout 0.01s 5288KB
stdin
1
2 60
sam sleep 12:00 23:59
al lunch 00:00 08:03
stdout
08:04