加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 程序设计 > 正文

CF 287B(Pipeline-二分)

发布时间:2020-05-22 20:45:51 所属栏目:程序设计 来源:互联网
导读: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 e

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;
}

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读