Pipeline(codeforces)

前端之家收集整理的这篇文章主要介绍了Pipeline(codeforces)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
B. Pipeline
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Vova,the Ultimate Thule new shaman,wants to build a pipeline. As there are exactlynhouses in Ultimate Thule,Vova wants the city to have exactlynpipes,each such pipe should be connected to the water supply. A pipe can be connected to the water supply if there's water flowing out of it. Initially Vova has only one pipe with flowing water. Besides,Vova has several splitters.

A splitter is a construction that consists of one input (it can be connected to a water pipe) andxoutput pipes. When a splitter is connected to a water pipe,water flows from each output pipe. You can assume that the output pipes are ordinary pipes. For example,you can connect water supply to such pipe if there's water flowing out from it. At most one splitter can be connected to any water pipe.

The figure shows a4-output splitter

Vova has one splitter of each kind: with2,3,sans-serif">4,...,sans-serif">koutputs. Help Vova use the minimum number of splitters to build the required pipeline or otherwise state that it's impossible.

Vova needs the pipeline to have exactlynpipes with flowing out water. Note that some of those pipes can be the output pipes of the splitters.

Input

The first line contains two space-separated integersnandk(1 ≤ n ≤ 1018,sans-serif">2 ≤ k ≤ 109).

Please,do not use the%lldspecifier to read or write 64-bit integers in С++. It is preferred to use thecin,coutstreams or the%I64dspecifier.

Output

Print a single integer — the minimum number of splitters needed to build the pipeline. If it is impossible to build a pipeline with the given splitters,print -1.

Sample test(s)
input

4 3
output

2
5 5
1
8 4
-1



数据量很大,二分查找是个不错的选择。

#include <stdio.h>
#include <iostream>
#include <string>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <map>

using namespace std;

typedef long long ll;


int main(){

ll n,k;

while (cin>>n>>k) {

//int flag = 1;

ll low=@H_404_256@0,high=k,mid;

while(low<=high)

{

mid=(low+high)>>@H_404_256@1;

ll s = mid*k-(mid-@H_404_256@1)*(mid+@H_404_256@2)>>@H_404_256@1;

if(n>s)

low=mid+@H_404_256@1;

else

high=mid-@H_404_256@1;

}

if(low > k)

cout<<"-1"<<endl;

else

cout<<low<<endl;

}

return @H_404_256@0;

}

原文链接:https://www.f2er.com/javaschema/285745.html

猜你在找的设计模式相关文章