Educational Codeforces Round 133 (Rated for Div. 2) A. 2-3 Moves

该篇博客探讨了一个编程挑战,涉及在坐标线上从点0到达点n的最短时间路径。每分钟可以移动2或3单位,路径可能经过负数坐标。博主通过分析给出了求解此问题的算法,并提供了代码实现,包括特殊情况的处理和优化。文章着重于算法设计和复杂度分析,适合对算法感兴趣的读者。
摘要由CSDN通过智能技术生成

Problem - A - Codeforces

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.


你站在坐标线上的 0 点。 你的目标是到达点 n。 在一分钟内,您可以向左或向右移动 2 或 3(即,如果您当前的坐标是 x,它可以变为 x−3、x−2、x+2 或 x+3 )。 请注意,新坐标可能变为负数。

你的任务是找出从点 0 到点 n 所需的最少分钟数。

你必须回答 t 个独立的测试用例。

输入
输入的第一行包含一个整数 t (1≤t≤104) — 测试用例的数量。 然后是描述测试用例的 t 行。

这些行的第 i 行包含一个整数 n (1≤n≤109)——第 i 个测试用例的目标。

输出
对于每个测试用例,打印一个整数——对应测试用例从点 0 到点 n 所需的最小分钟数。

例子
输入复制
4
1
3
4
12
输出复制
2
1
2
4

#include<iostream>
using namespace std;
typedef long long ll ;
int t;
ll n;
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n;
        ll bns=0;
        if(n==1)cout<<2<<endl;
        else
        {
            if(n%3==0&&n%2==0)
          {
            bns=n/3;
          }
           else  if(n%2==0&&n%3!=0)
          {
             bns=n/2;
          }
           else if(n%3==0&&n%2!=0)
          {
              bns=n/3;
          }
          
              ll ans=((n-2)/3);
                ans+=1;
               if((n-2)%3==2)
               {
                   ans+=1;
               }
               if((n-2)%3==1)
               {
                   ans+=2;
               }
               ll cns=(n-3)/2;
               cns+=1;
               if((n-3)%2)
               {
                   cns+=2;
               }
               if(bns!=0)
               cout<<min(min(cns,ans),bns)<<endl;
               else 
                cout<<min(cns,ans)<<endl;
        }
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值