Pipeline(codeforces)

来源:互联网 发布:fc2免费视频域名 编辑:程序博客网 时间:2024/05/17 08:17
题目:http://codeforces.com/contest/287/problem/B
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. Asthere are exactly n housesin Ultimate Thule, Vova wants the city to haveexactly n pipes,each such pipe should be connected to the water supply. A pipe canbe connected to the water supply if there's water flowing out ofit. 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 beconnected to a water pipe) and x outputpipes. When a splitter is connected to a water pipe, water flowsfrom each output pipe. You can assume that the output pipes areordinary pipes. For example, you can connect water supply to suchpipe if there's water flowing out from it. At most one splitter canbe connected to any water pipe.

 

Pipeline(codeforces)Thefigure shows a 4-outputsplitter

 

Vova has one splitter of each kind:with 234,..., k outputs.Help Vova use the minimum number of splitters to build the requiredpipeline or otherwise state that it's impossible.

Vova needs the pipeline to haveexactly n pipeswith flowing out water. Note that some of those pipes can be theoutput pipes of the splitters.

Input

The first line contains two space-separatedintegers n and k (1 ≤ n ≤ 10182 ≤ k ≤ 109).

Please, do not use the %lld specifierto read or write 64-bit integers in С++. It is preferred to usethe cincout streamsor the %I64dspecifier.

Output

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

Sample test(s)
input
4 3
output
2
input
5 5
output
1
input
8 4
output
-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;

       lllow=0,high=k,mid;

       while(low<=high)

       {

          mid=(low+high)>>1;

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

          if(n>s)

              low=mid+1;

          else

              high=mid-1;

       }

       if(low> k)

          cout<<"-1"<<endl;

       else

          cout<<low<<endl;

    }

return 0;

}

0 0
原创粉丝点击