欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【二分答案】爬山

程序员文章站 2022-06-02 16:14:13
...

很明显的二分…


题目

【二分答案】爬山


思路

二分上升用的时间,


#include<cstdio>
long long m,d,a,b,l,r,ups,downs,uup,ddown;
int main(){
 scanf("%lld%lld%lld%lld",&m,&d,&a,&b);
 --m;
 l = 0; r = m; 
 while(l<r){
  ups = (l+r+1)/2;  //往上用时
  downs = m-ups;  //往下用时
  uup = a+ups*d;  //往上时结束的最高高度
  ddown = b+downs*d;  //往下时开始的高度
  if(uup<ddown) l = ups;  //判断
  if(uup>ddown){
   if(uup-d<=ddown){
    printf("%lld",ddown);
    return 0;
   }
   else r = ups-1;
  }
  if(uup==ddown){
   printf("%lld",uup);
   return 0;
  }
 }
 ups = l;
 downs = m-ups;
 uup = a+ups*d;
 ddown = b+downs*d;
 if(uup>ddown) printf("%lld",ddown);
 else printf("%lld",uup);
}
相关标签: 暴力/模拟