前端之家收集整理的这篇文章主要介绍了
cf176B. Pipeline,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
- //题目连接:<a target=_blank href="http://codeforces.com/contest/287/problem/B">点击打开链接</a>
-
- #include<iostream>
- #include<cstdio>
- using namespace std;
- inline long long sum(long long a,long long k){
- return k*(k+1)/2-(k-a)*(k-a+1)/2-a+1;
- }
- int f(long long n,long long k){
- long long i,tot=1+(k-1)*k/2;
- if(n>tot) return -1;
- int toright=0;
- long long l=0,r=k,mid=(l+r)/2;//the numbers,have to take into account that l+1=r;uncontinue
- while(l<=r){
- long long tmp=sum(mid,k);
- if(tmp==n)break;
- else if(tmp<n){
- if(sum(mid+1,k)>=n){//对于mid个排水口,有2,3,...,mid+1的min,也有k,k-1,k-2,k-mid+1的max,而我的sum表示的是max,//sum(mid)和sum(mid+1)之间不是连续的,所以加上if判断,修正
- toright=1;
- break;
- }
- l=mid+1;
- mid=(l+r)/2;
- }
- else{
- if(sum(mid-1,k)<n) break;
- r=mid-1;
- mid=(l+r)/2;
- }
- }
- return mid+toright;
- }
-
- int main(){
- long long n,k;
- scanf("%I64d%I64d",&n,&k);
- cout<<f(n,k)<<endl;
- return 0;
- }