xlbfxx

题目链接:http://codeforces.com/problemset/problem/1183/C

思路

这个题就是尽量玩a类型,就是求如果能玩这个游戏,玩a多少次,一开始我是从最大的值到0遍历,发现超时,然后我就用二分法求,二分果然快。

AC代码

#include<iostream>
using namespace std;
long long k,n,a,b;
long long hanshu(long long l,long long r);
int main()
{
    int q;
    cin>>q;
    while(q--)
    {
        
        cin>>k>>n>>a>>b;
        
        if(a*n<k)
        {
            cout<<n<<endl;
            continue;
        }
        long long num=k/a;
        cout<<hanshu(0,num)<<endl;
    }
    return 0;
 }
long long hanshu(long long l,long long r)
{
    if(l==r||l+1==r)
    {
        if(r*a+(n-r)*b<k)return r;
        else if(l*a+(n-l)*b<k)return l;
        else return -1;
    }
    long long mid=(l+r)/2;
    if(mid*a+(n-mid)*b>=k)return hanshu(l,mid-1);
    else return hanshu(mid,r);
}

 

分类:

技术点:

相关文章:

  • 2021-09-08
  • 2022-12-23
  • 2021-06-09
  • 2021-12-27
  • 2022-12-23
  • 2022-12-23
  • 2021-07-23
  • 2022-12-23
猜你喜欢
  • 2022-03-07
  • 2021-05-19
  • 2021-11-11
  • 2022-02-14
  • 2021-07-22
  • 2021-06-12
  • 2021-07-06
相关资源
相似解决方案