hdoj 5477 A Sweet Journey (二分查找)

A Sweet Journey

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 729    Accepted Submission(s): 377


Problem Description
Master Di plans to take his girlfriend for a travel by bike. Their journey, which can be seen as a line segment of length L, is a road of swamps and flats. In the swamp, it takes A point strengths per meter for Master Di to ride; In the flats, Master Di will regain B point strengths per meter when riding. Master Di wonders:In the beginning, he needs to prepare how much minimum strengths. (Except riding all the time,Master Di has no other choice) 

 

Input
In the first line there is an integer t ( 1t50 ), indicating the number of test cases.
For each test case:
The first line contains four integers, n, A, B, L.
Next n lines, each line contains two integers:  Li,Ri , which represents the interval  [Li,Ri]  is swamp.
1n100,1L105,1A10,1B101Li<RiL .
Make sure intervals are not overlapped which means  Ri<Li+1  for each i ( 1i<n ).
Others are all flats except the swamps.
 

Output
For each text case:
Please output “Case #k: answer”(without quotes) one line, where k means the case number counting from 1, and the answer is his minimum strengths in the beginning.
 

Sample Input
  
  
1 2 2 2 5 1 2 3 4
 

Sample Output
  
  
Case #1: 0
 

Source

2015 ACM/ICPC Asia Regional Shanghai Online

慢慢的,对二分有了认识,二分就是先确定一个最小的可能范围 (但要满足条件),然后进行一分为二,判断中间值是否满足题意,

代码:

#include <iostream>
#include <cstdio>
using namespace std;
int t,k,n,a,b,l;
int s[100010],e[100010];
int judge(int sum)//判断中间值是否可行
{
    int per=0;
    for(int i=1;i<=n;i++)
    {
        sum+=(s[i]-per)*b;
        sum-=(e[i]-s[i])*a;
        if(sum<0)
            return false;
        else
            per=e[i];
    }
    return true;
}
int main()
{

    k=1;
    int left,right,mid,ans;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d%d",&n,&a,&b,&l);
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&s[i],&e[i]);
        }
        left=0;
        right=l*10+1;//开始脑子短路,写成了2,而a b的值最大为10呢
        while(right>=left)
        {
            mid=(left+right)>>1;
            if(judge(mid))
            {
                ans=mid;
                right=mid-1;
            }
            else
                left=mid+1;
        }
        printf("Case #%d: %d\n",k++,ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值