Advertisement
GilsonMuniz

1586 - Cabo de Guerra

May 15th, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int i, j;
  6. int n;
  7. string nome[100010];
  8. long long int forca[100010];
  9.  
  10. int binaria()
  11. {
  12.     bool achou = false;
  13.     int baixo = 0;
  14.     int alto = n - 1;
  15.     int meio;
  16.     long long int fa, fb;
  17.     int pos;
  18.  
  19.     while(baixo <= alto && achou == false)
  20.     {
  21.         meio = (baixo + alto) / 2;
  22.  
  23.         fa = 0;
  24.         for(i = 0; i <= meio; i++)
  25.             fa += forca[i] * (meio - i + 1);
  26.         fb = 0;
  27.         for(i = meio + 1; i < n; i++)
  28.             fb += forca[i] * (i - meio);
  29.  
  30.         if(fa == fb)
  31.         {
  32.             pos = meio;
  33.             achou = true;
  34.         }
  35.         if(fa > fb)
  36.             alto = meio - 1;
  37.         if(fa < fb)
  38.             baixo = meio + 1;
  39.     }
  40.  
  41.     if(achou)
  42.         return pos;
  43.     else
  44.         return -1;
  45. }
  46.  
  47. int main ()
  48. {
  49.     int pos;
  50.  
  51.     cin >> n;
  52.     while(n != 0)
  53.     {
  54.         for(i = 0; i < n; i++)
  55.         {
  56.             cin >> nome[i];
  57.  
  58.             forca[i] = 0;
  59.             for(j = 0; j < (int)nome[i].size(); j++)
  60.                 forca[i] += (int)nome[i][j];
  61.         }
  62.  
  63.         pos = binaria();
  64.  
  65.         if(pos != -1)
  66.             cout << nome[pos] << '\n';
  67.         else
  68.             cout << "Impossibilidade de empate." << '\n';
  69.  
  70.         cin >> n;
  71.     }
  72.  
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement