B. Diagonal Walking v.2

B. Diagonal Walking v.2

Mikhail walks on a Cartesian plane. He starts at the point (0,0), and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point (0,0), he can go to any of the following points in one move:(1,0);(1,1);(0,1);(−1,1);(−1,0);(−1,−1);(0,−1);(1,−1).
If Mikhail goes from the point (x1,y1) to the point (x2,y2) in one move, and x1≠x2 and y1≠y2, then such a move is called a diagonal move.

Mikhail has q queries. For the i-th query Mikhail’s target is to go to the point (ni,mi) from the point (0,0) in exactly ki moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point (0,0) to the point (ni,mi) in ki moves.

Note that Mikhail can visit any point any number of times (even the destination point!).

Input
The first line of the input contains one integer q (1≤q≤104) — the number of queries.

Then q lines follow. The i-th of these q lines contains three integers 1≤ni,mi,ki≤1018) — x-coordinate of the destination point of the query, y-coordinate of the destination point of the query and the number of moves in the query, correspondingly.

Output
Print q integers. The i-th integer should be equal to -1 if Mikhail cannot go from the point (0,0) to the point (ni,mi) in exactly ki moves described above. Otherwise the i-th integer should be equal to the the maximum number of diagonal moves among all possible movements.

Example
inputCopy
3
2 2 3
4 3 7
10 1 9
outputCopy
1
6
-1
Note
One of the possible answers to the first test case: (0,0)→(1,0)→(1,1)→(2,2).

One of the possible answers to the second test case (0,0)→(0,1)→(1,2)→(0,3)→(1,4)→(2,3)→(3,2)→(4,3).

In the third test case Mikhail cannot reach the point (10,1) in 9 moves.

#include<bits/stdc++.h>
#define endl '\n'
#define pb push_back
#define mp make_pair
#define _ ios::sync_with_stdio(false)
bool SUBMIT = 1;
typedef long long ll;
using namespace std;
const double PI = acos(-1);
int n;
int main()
{
    if(!SUBMIT)freopen("i.txt","r",stdin);else _;
    cin>>n;
    while(n--)
    {
        ll a,b,c;cin>>a>>b>>c;
        a=abs(a);b=abs(b);
        ll h=max(a,b),g=min(a,b);
        if(h>c){
            cout<<-1<<endl;continue;
        }
        ll ans=h;c-=h;
        h-=g;
        if(h&1){
            c--;
            ans+=c;
        }else
        {
            ans+=c;
            if(c&1)ans-=2;
        }
        cout<<ans<<endl;
    }
    return 0;
}
posted @ 2018-09-09 17:13 i-Curve 阅读( ...) 评论( ...) 编辑 收藏
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值