CF 287B(Pipeline-二分)

前端之家收集整理的这篇文章主要介绍了CF 287B(Pipeline-二分)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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


这题显然优先找大的

直到找不下去了,判断无解或者拿个与需要的分水器相等的(显然有)

因为一个k的分水器只能增加k-1条通道

所以列方程


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<iostream>
using namespace std;
#define MAXN (1000000000000000000)
#define MAXK (1000000000)
long long n,k;
long long bin_s(long long l,long long r)
{
	if (n==1) return 0;
	while (r-l>1)
	{
		long long  m=(l+r)/2;
		if ((m+k-1)*(k-m)>2*(n-1)) l=m;
		else r=m;
	}
	if ((l+k-1)*(k-l)>2*(n-1)) l=r;
	if ((l+k-1)*(k-l)<2*(n-1)) l--;
	
	if (l==0) return -1;
	return k-l;
}
int main()
{
	cin>>n>>k;
	cout<<bin_s(1,k-1)<<endl;
	
	
	return 0;
}
原文链接:https://www.f2er.com/javaschema/286378.html

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