hdu6653 Halt Hater

题目描述

Cuber QQ has recently won a million dollars and bought a new car. He is now driving in the Infinite City and he doesn't want his car to stop, ever!

The Infinite City, looks like an infinite grid, with infinite streets built at all possible x's and y's when they are integers. The following picture, looks like part of this city. (The picture is downloaded from a webpage, which is certainly not describing the Infinite City.)

There are traffic lights installed on every crossings. Just like what Chinese people usually do, the traffic lights only show signals for left and straight and you can always turn right regardless of the traffic light. The estimate time of waiting for a left turn is a, at all crossings; and the time of waiting for a straight is b.

Cuber QQ is now driving from (0,−1) to (0,0), about to entering the crossing at (0,0). At a crossing, he can choose to go straight, turn left or turn right. He wishes to go to (x,y), but here is a weird request: he wants to have the least estimated traffic light waiting time. In other words, he doesn't care whether he has to drive a long way, or he has to burn a lot of gas, all he wants is to wait as little as possible.

Note that Cuber QQ doesn't have to wait for the traffic light at (x,y); he also doesn't care from which direction he enters (x,y).

 

输入

The first line of the input contains an integer t (1≤t≤100 000).

Then follows t test cases, each containing a, b, x, y (1≤a,b≤109, −109≤x,y≤109, |x|+|y|>0), space separated. 

 

输出

For each test case, output one integer as the answer.

 

 

样例输入

复制样例数据

4
9 1 0 2
2 1 8 0
2 3 3 3
1 1 -1 -1

样例输出

2
7
6
1

 

参考:https://blog.csdn.net/qq_40791842/article/details/99499963

官方题解要枚举转折点,和两侧端点,即确定折线的三个点,而打表后可以发现,所有折线要么经过x-1,要么经过-x。。。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int T;
    cin>>T;
    while(T--)
    {
        ll a,b,x,y;
        cin>>a>>b>>x>>y;
        if(b>a)
        {
            b=a;
        }
        a=min(a,2*b);
        ll ans;
        if(x>0)
        {
            if(y>=0)
            {
                if(y<=x-1)
                {
                    ans= a*y+b*(x-y-1);
                }
                else
                {
                    ans= a*(x-1)+b*(y-x+1);
                }
            }
            else
            {
                if(y>=-x)
                {
                    ans= -a*(y+1)+b*(x+y);
                }
                else
                {
                    ans= a*(x-1)+b*(-y-x);
                }
            }
        }
        else
        {
            if(y>=0)
            {
                if(y<=-x)
                {
                    ans= a*y+b*(-x-y);
                }
                else
                {
                    ans= -a*x+b*(y+x);
                }
            }
            else
            {
                if(y<=x-1)
                {
                    ans= -a*x+b*(x-y-1);
                }
                else
                {
                    ans= a*(-y-1)+b*(y-x+1);
                }
            }
        }
        cout<<ans<<'\n';
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值