codeforces 382B B. Number Busters [二分答案+数学]【思维】

66 篇文章 0 订阅
33 篇文章 0 订阅

题目链接:http://codeforces.com/contest/382/problem/B

—————————————————————————————————-.
B. Number Busters
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Arthur and Alexander are number busters. Today they’ve got a competition.

Arthur took a group of four integers a, b, w, x (0 ≤ b < w, 0 < x < w) and Alexander took integer с. Arthur and Alexander use distinct approaches to number bustings. Alexander is just a regular guy. Each second, he subtracts one from his number. In other words, he performs the assignment: c = c - 1. Arthur is a sophisticated guy. Each second Arthur performs a complex operation, described as follows: if b ≥ x, perform the assignment b = b - x, if b < x, then perform two consecutive assignments a = a - 1; b = w - (x - b).

You’ve got numbers a, b, w, x, c. Determine when Alexander gets ahead of Arthur if both guys start performing the operations at the same time. Assume that Alexander got ahead of Arthur if c ≤ a.

Input
The first line contains integers a, b, w, x, c (1 ≤ a ≤ 2·109, 1 ≤ w ≤ 1000, 0 ≤ b < w, 0 < x < w, 1 ≤ c ≤ 2·109).

Output
Print a single integer — the minimum time in seconds Alexander needs to get ahead of Arthur. You can prove that the described situation always occurs within the problem’s limits.

Examples
input
4 2 3 1 6
output
2
input
4 2 3 1 7
output
4
input
1 2 3 2 6
output
13
input
1 1 2 1 1
output
0

—————————————————————————————————-.
题目大意:
就是两个人,一个人有4个数字 a,b,w,x ,另一个人有1个数字 c <script type="math/tex" id="MathJax-Element-406">c</script>,每一秒第二个人的数字减一c-- ,每一秒第一个人的数字变化如下,if(b>=x) b=b-x;else a--,b=w-(x-b);现在问你最少多长时间之后a<=c?

解题思路:
很明显的二分答案,但是对于check部分还是要推敲一下,
首先if(b>=x) b=b-x;else a--,b=w-(x-b);
可以改变成 if(b>=x) b=b-x;else a--,b=b-x+w;
显然就是 在b<0的时候将+w,与此同时a–,
那么在c减少n的时候a减少的数m就满足 b=b-n*x+m*w&&b>0,(m取最小值);

与此同时注意a,c可能小于0,所以二分答案的边界要大一点.

附本题代码
—————————————————————————————————-.

#include <bits/stdc++.h>
using namespace std;
#define abs(x)  (((x)>0)?(x):-(x))
const int N = 500000+5;
typedef long long int LL;
/***************************************/
LL ta,tb,tw,tx,tc;
bool check(LL n){
    LL a=ta,b=tb,w=tw,x=tx,c=tc;
    c-=n;
    b-=x*n;
    LL num = abs(b/w);
    b+=num*w;
    if(b>=0);
    else num++;
    a-=num;
    if(c<=a) return true;
    else return false;
}

int main(){
    ios::sync_with_stdio(false);

    cin>>ta>>tb>>tw>>tx>>tc;

    LL l=0,r=2e14,mid,ans;
    while(l<=r){
        mid=(l+r)>>1;
        if(check(mid)) ans=mid,r=mid-1;
        else l=mid+1;

    }
    cout<<ans <<endl;

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值