Diagonal Walking v.2

CodeForces - 1036B

Mikhail walks on a Cartesian plane. He starts at the point (0,0)(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)(0,0), he can go to any of the following points in one move:

  • (1,0)(1,0);
  • (1,1)(1,1);
  • (0,1)(0,1);
  • (−1,1)(−1,1);
  • (−1,0)(−1,0);
  • (−1,−1)(−1,−1);
  • (0,−1)(0,−1);
  • (1,−1)(1,−1).

If Mikhail goes from the point (x1,y1)(x1,y1) to the point (x2,y2)(x2,y2) in one move, and x1≠x2x1≠x2 and y1≠y2y1≠y2, then such a move is called a diagonal move.

Mikhail has qq queries. For the ii-th query Mikhail's target is to go to the point (ni,mi)(ni,mi) from the point (0,0)(0,0) in exactly kiki 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)(0,0) to the point (ni,mi)(ni,mi) in kiki 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 qq (1≤q≤1041≤q≤104) — the number of queries.

Then qq lines follow. The ii-th of these qq lines contains three integers nini, mimi and kiki (1≤ni,mi,ki≤10181≤ni,mi,ki≤1018) — xx-coordinate of the destination point of the query, yy-coordinate of the destination point of the query and the number of moves in the query, correspondingly.

Output

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

Example

Input

  1. 32 2 34 3 710 1 9

Output

  1. 16-1

Note

One of the possible answers to the first test case: (0,0)→(1,0)→(1,1)→(2,2)(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)(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)(10,1) in 9 moves.

题意:

为了防止比赛被ak!为了守护世界的和平!我们!贯彻爱与真实的险恶!恩爱又迷人的出题组!!决定!!!把zzq抓起来,放到一个荒无人烟岛上。zzq所在的位置是(0,0),而离开荒岛的传送阵在(n,m),zzq的体力值只够他走k步,zzq每次可以走8个方向。

(1,0)

(1,1)

(0,1)

(−1,1)

(−1,0)

(−1,−1)

(0,−1)

(1,−1)

但是温柔善良的大魔王SYH怎么会让zzq轻易的逃离荒岛,所以她希望zzq尽量多地往斜方向走,传送阵仅在第k秒开启,口令就是zzq最多可以往斜方向走的步数。

可怜的zzq被土拨鼠吸走了所有的脑细胞,于是他打电话给你想让你帮他解出口令。

思路:

如果 x > y 先swap(x,y),交换xy并不影响答案。

然后 先从( 0 ,0 )走到(x,x)

然后再竖直向上走,

我们令z=k-x,

如果剩下的路程 y=(y-x)

那么接下来

如果y和z都是奇数,用z中的一个1,走y中的一个单位。

两者都变成偶数,而偶数可以通过这样的走法使剩下的全部z都走歇着的。

否则如果y和z中只有一个是奇数,用z中的偶数部分去全走斜的,答案再必须减去1.

细节见代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <queue>
  7. #include <stack>
  8. #include <map>
  9. #include <set>
  10. #include <vector>
  11. #include <iomanip>
  12. #define ALL(x) (x).begin(), (x).end()
  13. #define sz(a) int(a.size())
  14. #define all(a) a.begin(), a.end()
  15. #define rep(i,x,n) for(int i=x;i<n;i++)
  16. #define repd(i,x,n) for(int i=x;i<=n;i++)
  17. #define pii pair<int,int>
  18. #define pll pair<long long ,long long>
  19. #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
  20. #define MS0(X) memset((X), 0, sizeof((X)))
  21. #define MSC0(X) memset((X), '\0', sizeof((X)))
  22. #define pb push_back
  23. #define mp make_pair
  24. #define fi first
  25. #define se second
  26. #define eps 1e-6
  27. #define gg(x) getInt(&x)
  28. #define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
  29. using namespace std;
  30. typedef long long ll;
  31. ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
  32. ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
  33. ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
  34. inline void getInt(int* p);
  35. const int maxn = 1000010;
  36. const int inf = 0x3f3f3f3f;
  37. /*** TEMPLATE CODE * * STARTS HERE ***/
  38. int q;
  39. ll x, y, k;
  40. int main()
  41. {
  42. //freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
  43. //freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
  44. scanf("%d", &q);
  45. while (q--)
  46. {
  47. scanf("%lld %lld %lld", &x, &y, &k);
  48. if (x > y)
  49. {
  50. swap(x, y);
  51. }
  52. ll z = k - x;
  53. y -= x;
  54. if (z < y)
  55. {
  56. printf("-1\n");
  57. } else
  58. {
  59. if (z & 1)
  60. x += z - 1;
  61. else
  62. x += z;
  63. if (y & 1)
  64. {
  65. y = 1;
  66. } else
  67. {
  68. y = 0;
  69. }
  70. if (z & 1)
  71. {
  72. z = 1;
  73. } else
  74. {
  75. z = 0;
  76. }
  77. if (z & y)
  78. {
  79. } else if (z + y)
  80. {
  81. x--;
  82. }
  83. printf("%lld\n", x );
  84. }
  85. }
  86. return 0;
  87. }
  88. inline void getInt(int* p) {
  89. char ch;
  90. do {
  91. ch = getchar();
  92. } while (ch == ' ' || ch == '\n');
  93. if (ch == '-') {
  94. *p = -(getchar() - '0');
  95. while ((ch = getchar()) >= '0' && ch <= '9') {
  96. *p = *p * 10 - ch + '0';
  97. }
  98. }
  99. else {
  100. *p = ch - '0';
  101. while ((ch = getchar()) >= '0' && ch <= '9') {
  102. *p = *p * 10 + ch - '0';
  103. }
  104. }
  105. }

Diagonal Walking v.2 CodeForces - 1036B (思维,贪心)的更多相关文章

  1. codeforces 1036B - Diagonal Walking v.2【思维+构造】

    题目:戳这里 题意:起点(0,0),终点(n,m),走k步,可以走8个方向,问能不能走到,能走到的话最多能走多少个斜步. 解题思路:起点是固定的,我们主要分析终点.题目要求走最多的斜步,斜步很明显有一 ...

  2. CF 1036B Diagonal Walking v.2——思路

    题目:http://codeforces.com/contest/1036/problem/B 比赛时只能想出不合法的情况还有走到终点附近的方式. 设n<m,不合法就是m<k.走到终点方式 ...

  3. Buy Low Sell High CodeForces - 867E (思维,贪心)

    大意: 第i天可以花$a_i$元买入或卖出一股或者什么也不干, 初始没钱, 求i天后最大收益 考虑贪心, 对于第$x$股, 如果$x$之前有比它便宜的, 就在之前的那一天买, 直接将$x$卖掉. 并不 ...

  4. CF 1036 B Diagonal Walking v.2 —— 思路

    题目:http://codeforces.com/contest/1036/problem/B 题意:从 (0,0) 走到 (n,m),每一步可以向八个方向走一格,问恰好走 k 步能否到达,能到达则输 ...

  5. Codeforces 922 思维贪心 变种背包DP 质因数质数结论

    A #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #def ...

  6. Codeforces 1093C (思维+贪心)

    题面 传送门 题目大意: 有一个长n(n为偶数)的序列a 已知a满足 \(a_1≤a_2≤⋯≤a_n\) 给出一个长度为\(\frac{n}{2}\) 的序列b,定义\(b_i=a_i+a_{n-i+ ...

  7. B. Diagonal Walking v.2

    链接 [https://i.cnblogs.com/EditPosts.aspx?opt=1] 题意 二维平面从原点出发k步,要到达的点(x,y),每个位置可以往8个方位移动,问到达目的地最多可以走多 ...

  8. Sorted Adjacent Differences(CodeForces - 1339B)【思维+贪心】

    B - Sorted Adjacent Differences(CodeForces - 1339B) 题目链接 算法 思维+贪心 时间复杂度O(nlogn) 1.这道题的题意主要就是让你对一个数组进 ...

  9. Codeforces Round #768 (Div. 2) D. Range and Partition // 思维 + 贪心 + 二分查找

    The link to problem:Problem - D - Codeforces   D. Range and Partition  time limit per test: 2 second ...

随机推荐

  1. 2.React 生命周期函数

    什么是生命周期函数:在某一时刻组件会自动调用执行的函数. import React,{ Component,Fragment } from 'react' class Note extends Com ...

  2. 【计算机视觉】基于样本一致性的背景减除运动目标检测算法(SACON)

    SACON(SAmple CONsensus)算法是基于样本一致性的运动目标检测算法.该算法通过对每个像素进行样本一致性判断来判定像素是否为背景. 算法框架图 由上图可知,该算法主要分为四个主要部分, ...

  3. python 指定文件夹下所有文件(包括子目录下的文件)拷贝到目标文件夹下

    #!/usr/bin/env python3 # -*- coding:utf8 -*- # @TIME :2018/9/17 9:02 # @Author:dazhan # @File :copyf ...

  4. Codeforces Round #581(Div. 2)

    Codeforces Round #581(Div. 2) CF 1204 A. BowWow and the Timetable 题解:发现,$4$的幂次的二进制就是一个$1$后面跟偶数个$0$. ...

  5. windows 安装jenkins

    本文简单记录 windows 安装 jenkins. 1. 下载jenkins安装包,下载地址:https://jenkins.io/index.html 2. 选择下载windows版 3. 解压, ...

  6. redis发布订阅者

    发布者pub.py import redis conn = redis.Redis(host='127.0.0.1', decode_responses=True) conn.publish(') 订 ...

  7. Java反射理解(四)-- 获取成员变量构造函数信息

    Java反射理解(四)-- 获取成员变量构造函数信息 步骤 获取成员变量信息: obj.getClass() 获取类类型对象 成员变量也是对象,java.lang.reflect.Field 类中封装 ...

  8. linq to sql之Distinct

    Distict用来排除相同序列中元素的,对于基础类型,可以直接使用Distinct,如:int[] a = {1, 2, 2, 3, 3, 3, 4};var reslut = a.Distinct( ...

  9. MongoDB 基础增删改查

    增删改查 基础操作 use show dbs show collections db[当前所在数据库] 插入文档 db.collection.insert() db.collection.insert ...

  10. ztree树id、pid转成children格式的(待整理完整)

    山铝菜单 因为菜单选用了bootstrap treeview ,而格式需要是children类似的格式 var nodes = [ {name: "父节点1", children: ...