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 exactly@H_404_31@n@H_404_31@houses in Ultimate Thule,Vova wants the city to have exactly@H_404_31@n@H_404_31@pipes,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) and@H_404_31@x@H_404_31@output 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.

@H_404_31@

The figure shows a@H_404_31@4-output splitter

@H_404_31@

Vova has one splitter of each kind: with@H_404_31@2,@H_404_31@3,sans-serif">4,...,sans-serif">k@H_404_31@outputs. 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 exactly@H_404_31@n@H_404_31@pipes 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 integers@H_404_31@n@H_404_31@and@H_404_31@k@H_404_31@(1 ≤ n ≤ 1018,sans-serif">2 ≤ k ≤ 109).

Please,do not use the@H_404_31@%lld@H_404_31@specifier to read or write 64-bit integers in С++. It is preferred to use the@H_404_31@cin,@H_404_31@cout@H_404_31@streams or the@H_404_31@%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>

@H_404_31@

using namespace std;

typedef long long ll;


@H_301_219@ int main(){

@H_301_219@ @H_404_31@ @H_404_31@ ll n,k;

@H_301_219@ @H_404_31@ @H_404_31@ while (cin>>n>>k) {

@H_404_31@ @H_404_31@ @H_404_31@ @H_404_31@ //int flag = 1;

@H_301_219@ @H_404_31@ @H_404_31@ @H_404_31@ @H_404_31@ ll low=0,high=k,mid;

@H_301_219@ @H_404_31@ @H_404_31@ @H_404_31@ @H_404_31@ while(low<=high)

@H_301_219@ @H_404_31@ @H_404_31@ @H_404_31@ @H_404_31@ {

@H_301_219@ @H_404_31@ @H_404_31@ @H_404_31@ @H_404_31@ @H_404_31@ @H_404_31@ mid=(low+high)>>1;

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

@H_301_219@ @H_404_31@ @H_404_31@ @H_404_31@ @H_404_31@ @H_404_31@ @H_404_31@ if(n>s)

@H_301_219@ @H_404_31@ @H_404_31@ @H_404_31@ @H_404_31@ @H_404_31@ @H_404_31@ @H_404_31@ @H_404_31@ low=mid+1;

@H_301_219@ @H_404_31@ @H_404_31@ @H_404_31@ @H_404_31@ @H_404_31@ @H_404_31@ else

@H_301_219@ @H_404_31@ @H_404_31@ @H_404_31@ @H_404_31@ @H_404_31@ @H_404_31@ @H_404_31@ @H_404_31@ high=mid-1;

@H_301_219@ @H_404_31@ @H_404_31@ @H_404_31@ @H_404_31@ }

@H_301_219@ @H_404_31@ @H_404_31@ @H_404_31@ @H_404_31@ if(low > k)

@H_301_219@ @H_404_31@ @H_404_31@ @H_404_31@ @H_404_31@ @H_404_31@ @H_404_31@ cout<<"-1"<<endl;

@H_301_219@ @H_404_31@ @H_404_31@ @H_404_31@ @H_404_31@ else

@H_301_219@ @H_404_31@ @H_404_31@ @H_404_31@ @H_404_31@ @H_404_31@ @H_404_31@ cout<<low<<endl;

@H_301_219@ @H_404_31@ @H_404_31@ }

return 0;

@H_301_219@ }

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