Advertisement
Guest User

swimming_pool_2

a guest
Jun 23rd, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. typedef vector<int> VE;
  7. int n, m;
  8. VE slots;
  9.  
  10. int convert(string s) {
  11.     int H = 0, M = 0, S = 0;
  12.     H = ((int)s[0] - 48)*10 + ((int)s[1] - 48);
  13.     M = ((int)s[3] - 48)*10 + ((int)s[4] - 48);
  14.     S = ((int)s[6] - 48)*10 + ((int)s[7] - 48);
  15.     return H*3600 + M*60 + S;
  16.    
  17. }
  18.  
  19. void read() {
  20.     string s;
  21.     slots = VE(n, 0);
  22.     for (int i = 0; i < n; i++) {
  23.         cin >> s;
  24.         int t = convert(s);
  25.         slots[i] = t;
  26.     }
  27.     sort(slots.begin(), slots.end());
  28. }
  29.  
  30. bool enough(int x) {
  31.     int k = 1, i = 1;
  32.     int N = slots[0] + 60 + x;
  33.     while (i < n) {
  34.         while (slots[i] < N) {
  35.             i++;
  36.             if (i == n) return false;
  37.         }
  38.         k++;
  39.         N = slots[i] + 60 + x;
  40.         if (k == m) return true;
  41.     }
  42.     return false;
  43. }
  44.  
  45. int bin_search(int a, int b) {
  46.     if (b - a == 1) {
  47.         if (enough(b)) return b;
  48.         else return a;
  49.     }
  50.     else {
  51.         int c = a + (b - a)/2;
  52.         if (enough(c)) return bin_search(c, b);
  53.         else return bin_search(a, c);
  54.     }
  55. }
  56.  
  57. int main() {
  58.     cin >> m >> n;
  59.     while (m != 0 or n != 0) {
  60.         read();
  61.         cout << bin_search(0, 86400 - 120) << endl;
  62.         cin >> m >> n;
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement