Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. long long x;
  7.  
  8. long long nedge(long long m)
  9. {
  10.   return x-m + min((m*(m-1))/2, x-m);
  11. }
  12.  
  13. long long ts(long long low, long long high)
  14. {
  15.   long long m1,m2;
  16.   long long ne1,ne2,nel=-1;
  17.   while(high>low)
  18.   {
  19.     m1=low+(high-low)/3;
  20.     m2=high-(high-low)/3;
  21.     //cout << "low high " <<  low << " " << high << endl;
  22.     //cout << "m1 m2 " << m1 << " " << m2 << endl;
  23.  
  24.     ne1=nedge(m1);
  25.     ne2=nedge(m2);
  26.  
  27.     //cout << "ne1 ne2 " << ne1 << " " << ne2 << endl;
  28.  
  29.     if(ne2<ne1)
  30.       high=m2-1;
  31.     else if(ne1<ne2)
  32.       low=m1+1;
  33.     else
  34.       low=m1+1,high=m2-1;
  35.  
  36.     if(nel<ne1)
  37.       nel=ne1;
  38.     if(nel<ne2)
  39.       nel=ne2;
  40.  
  41.     //cout << endl;
  42.   }
  43.   //cout << "##############" << endl;
  44.   return nel;
  45. }
  46.  
  47. int main() {
  48.  
  49.   int T;
  50.   cin >> T;
  51.   while(T--)
  52.   {
  53.     cin >> x;
  54.  
  55.     cout << ts(1, 2*x) << endl;
  56.   }
  57.   return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement