sntmessages's blog

By sntmessages, 9 years ago, In English

i have some doubt about this problem. it is written in the problem statement that "Vova needs the pipeline to have exactly n pipes with flowing out water."

considering this statement the answer for test case : 21 8 is '-1' . since for k=8 number of pipes can be 1,8,14,19,23,26,28,29 and it is written that pipeline should have exactly n pipes with flowing out water, therefore, 21 cant be possible.

i have run custom test and it is showing me o/p as '4'. am i missing something? please help.

 
 
 
 
  • Vote: I like it
  • 0
  • Vote: I do not like it

»
9 years ago, # |
  Vote: I like it 0 Vote: I do not like it

You can take the 8-way, 7-way, 6-way, and 3-way splitters to get 21 pipes in total.

»
11 days ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it
long long  n,k,mid,x,c,ans;
cin>>n>>k;
long long total=((k*(k-1))/2)+1;

long long low=0,high=k-1;
if(total<n)
{
    cout<<-1<<endl;
}
else
{


while(low<=high)
{
  mid=(low+high)/2;
     c=k-mid;
    x=total-(((c*(c-1))/2)+1)+1;
    if(x>=n)
    {
        ans=min(mid,LLONG_MAX);
           high=mid-1;

    }
    else
    {
        low=mid+1;
    }

}
cout<<ans<<endl;

binary search easy version