当前位置: 代码迷 >> 综合 >> B. Diagonal Walking v.2
  详细解决方案

B. Diagonal Walking v.2

热度:26   发布时间:2023-11-22 13:53:13.0

题目链接
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 ni, mi and ki (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.

input
3
2 2 3
4 3 7
10 1 9

output
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>
using namespace std;
typedef long long LL;
int main(){
    ios :: sync_with_stdio(false);cin.tie(0);LL t,n,m,k;cin >> t;while(t--){
    cin >> n >> m >> k;if(k < max(n,m)) cout << -1 << endl;else{
    if((max(n,m) - min(n,m)) % 2 == 1) cout << k - 1 << endl;else{
    if((k - min(n,m)) % 2 == 0) cout << k << endl;else cout << k - 2 << endl;}}		}return 0;
}