B. Diagonal Walking v.2 time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output 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. Example Input Copy 3 2 2 3 4 3 7 10 1 9 Output Copy 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.
大概就是从(0,0)可以走直线可以走斜线,给定步数k,问能否k步正好到达终点,可以的话输出最多的斜线步数 。
在纸上乱画,发现可以把坐标系画成类似楷书格子的横竖斜交叉的格子
发现如果重点在斜线交叉内,在min(xd,yd)之步定可以到终点,且可以绕圈增步数,每次可增2步
然后k的大小奇偶判断
不再在写交叉内的话化未知为已知问题,即在k-1步内到达(x-1,y)或(y,x-1)
在同第一部判断
中间有的情况可以简化合并
#include<cstdio> #include<algorithm> using namespace std; int main(){ int t; scanf("%d",&t); long long n,m,k,a,b,kmin; while(t--){ scanf("%lld%lld%lld",&n,&m,&k); if(n%2==m%2){ kmin=max(n,m); if(k<kmin)printf("-1\n"); else if(k%2==kmin%2)printf("%lld\n",k); else printf("%lld\n",k-); } else{ kmin=max(n-1,m-1); if(k-1<kmin)printf("-1\n"); else printf("%lld\n",k-1); } } return 0; }