Advertisement
mateuspl

c343B

Feb 21st, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     int nFriends;
  8.     cin >> nFriends;
  9.  
  10.     // 0: male
  11.     // 1: female
  12.     // 358 days:
  13.     //      366 + 1 (367) to access 1 to 366 by index and
  14.     //      367 + 1 (368) to do not overflow when do "to + 1" at line 22
  15.     //          ~ if "to" equals 366, it will access days[x][367]!
  16.     int days[2][358] = {0};
  17.  
  18.     for (int i = 0; i < nFriends; i++)
  19.     {
  20.         char gender;
  21.         int from, to;
  22.         cin >> gender >> from >> to;
  23.  
  24.         days[gender == 'M'][from]++;
  25.  
  26.         // subtract 1 person from the next day
  27.         // at day "to", the person can still be counted, but can not be at day "to + 1"
  28.         days[gender == 'M'][to + 1]--;
  29.     }
  30.  
  31.     int friendsCounter[2] = {0};
  32.     int friendsComming = 0;
  33.  
  34.     for (int i = 1; i <= 366; i++)
  35.     {
  36.         friendsCounter[0] += days[0][i];
  37.         friendsCounter[1] += days[1][i];
  38.  
  39.         // if the number of male and female friends invited are equal
  40.         if (friendsCounter[0] == friendsCounter[1])
  41.         {
  42.             friendsCounter[0] += friendsCounter[1];
  43.  
  44.             if (friendsCounter[0] > friendsComming)
  45.                 friendsComming = friendsCounter[0];
  46.         }
  47.     }
  48.  
  49.     cout << friendsComming << endl;
  50.  
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement