You are standing at the point 00 on a coordinate line. Your goal is to reach the point nn. In one minute, you can move by 22 or by 33 to the left or to the right (i. e., if your current coordinate is xx, it can become x−3x−3, x−2x−2, x+2x+2 or x+3x+3). Note that the new coordinate can become negative.

Your task is to find the minimum number of minutes required to get from the point 00 to the point nn.

You have to answer tt independent test cases.

Input

The first line of the input contains one integer tt (1≤t≤1041≤t≤104) — the number of test cases. Then tt lines describing the test cases follow.

The ii-th of these lines contains one integer nn (1≤n≤1091≤n≤109) — the goal of the ii-th test case.

Output

For each test case, print one integer — the minimum number of minutes required to get from the point 00 to the point nn for the corresponding test case.

Example

input

Copy

4

1

3

4

12

output

Copy

2
1
2
4

翻译一下:给你一个数,判断一个数通过-2 +2 -3 +3这四种运算,至少要多少次才能到0

比较简单的dp,定义一个dp数组,数组的第n个表示,第n个数至少要通过多少次

这个地方我们完全可以不用看+2 +3这两种,因为完全可以用-2 -3的数来代替

如果算出来的是负数,莫非就是-3 -2 -1,其实结果和他们的绝对值也是相同的,写一个函数来获取数组就行

f(x)-> return dp[abs(x)]

然后从第4位开始递推,比较x-2和x-3谁更小,然后再+1,就可以得到结果

但是这里的数据量比较大,所以可以找找规律,刚好发现从第四位开始,每三位的结果都是相同的

2 2 2 3 3 3 4 4 4 5 5 5 ...

然后前面的0 1 2 3的结果我们如果忽略的话(因为可以直接通过数组返回),那相当于从第四位开始就是每三位递增的,如果觉得规律有点难写的话,你就想,1 2 3所对应的结果都是1,代码不久好写了?

直接上代码

#include <iostream>
using namespace std;int arr[4]={0,2,1,1};int f(int x){if (x<4)return arr[abs(x)];//如果小于4,直接返回else{if ((float)x/3==x/3)return x/3;//做一个处理,如果刚刚整除,直接返回,如果有余数就+1else return x/3+1;//这里可以直接写成 return x/3+(x%3?1:0);}
}int main(){int T;cin>>T;while(T--){int n;cin>>n;cout<<f(n)<<endl;}
}

CF1716 A. 2-3 Moves相关推荐

  1. [Educational Codeforces Round 16]A. King Moves

    [Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...

  2. poj - 2243 Knight Moves

    这题和poj 1915一样,用bfs做走马步.现在再看当时的代码,真是好幼稚啊. 1 #include <stdio.h> 2 #include <string.h> 3 in ...

  3. 1 Knight Moves

    Problem F- Knight Moves 题目来源:https://vjudge.net/contest/207868#problem/F Problem description: 题意概括:中 ...

  4. LeetCode Minimum Moves to Equal Array Elements II

    原题链接在这里:https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/ 题目: Given a non-empt ...

  5. java求最小步数,使数组值相等的最小步数 Minimum Moves to Equal Array Elements

    问题: Given a non-empty integer array of size n, find the minimum number of moves required to make all ...

  6. ubuntu16安装pylearn2 出现错误提示importerror:no module named six.moves

    由于市面上的一些教程时间比较早,入门学习时跟随教程安装容易出现各种错误,这些错误基本都是版本不同导致的 所以,我们安装过程中一定要指出包的版本,如果你已经遇到no module named six.m ...

  7. HDU 1372 Knight Moves

    最近在学习广搜  这道题同样是一道简单广搜题=0= 题意:(百度复制粘贴0.0) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...

  8. leetcode 453,462. Minimum Moves to Equal Array Elements I, II | 453, 462. 最少移动次数使数组元素相等(图解)

    453. Minimum Moves to Equal Array Elements https://leetcode.com/problems/minimum-moves-to-equal-arra ...

  9. OpenJudge/Poj 1915 Knight Moves

    1.链接地址: http://bailian.openjudge.cn/practice/1915 http://poj.org/problem?id=1915 2.题目: 总Time Limit: ...

最新文章

  1. Firefox显示 您的链接不安全 的解决办法
  2. Python学习笔记-EXCEL操作
  3. LeetCode-剑指 Offer 22. 链表中倒数第k个节点
  4. Invalid URI
  5. WebRTC/Chromium在2020年的更新
  6. 分区格式化并给用户设定配额
  7. linux cpu核数查看_Linux日常必备的 8 个小技能
  8. python 控制有线网卡_Python 使用指定的网卡发送HTTP请求的实例
  9. android数据存放map_Android存储数据到本地文件
  10. java token redis生成算法_如何访问 Redis 中的海量数据,服务才不会挂掉?
  11. 读取xml并转换为Java对象
  12. Android4.4 Sensor APP--HAL代码流程
  13. MySQL客户端使用
  14. python notebook软件_anaconda3 notebook for python数据分析-环境搭建
  15. 【学习笔记】数据分析师相关岗位招聘情况分析
  16. 三极管饱和状态下增益
  17. 将自己开发的vue组件库发布到npm
  18. 松翰单片机之M2ASM创建新工程
  19. Juniper防火墙配置备份
  20. JAVA8新特性的总结

热门文章

  1. xps15java_工作一整天都不需要充电 XPS 15二合一本超长续航
  2. Dos命令大全【转载】
  3. d3dx9_34.dll丢失,找不到(解决方案)
  4. 数据同步智能化!利用ETLCloud自动化流程实现钉钉OA系统数据自动同步至数仓
  5. 财神:如何赚更多的钱?
  6. python numpy opencv调整图片亮度对比度饱和度
  7. 教你免费安装官方 Apple Remote Desktop 洗白官方Remote Desktop for Mac
  8. Jmeter超高并发解决方案
  9. 深入理解 D3.js 可视化库之力导向图原理与实现
  10. 又到年底了,明年该学点啥呢?