1716A - 2-3 Moves - CodeForces Solution


greedy math

Please click on ads to support us..

Python Code:

for t in range(int(input())):
    n = int(input())
    print(n//3 + (n%3!=0) + (n==1))

C++ Code:

#pragma GCC optimize("O3")
#include <iostream>
// #include <vector>
// #include <cstring>
#include <algorithm>
// #include <iomanip>
// #include <math.h>
// #include <iomanip>
// #include <map>
// #include <unordered_map>
// #include <set>
// #include <unordered_set>
// #include <stack>
// #include <queue>
// #include <deque>
using namespace std;

template < typename A, typename B > ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template < typename T_container, typename T = typename enable_if < !is_same<T_container, string>::value, typename T_container::value_type >::type > ostream & operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << '\n'; }
template<typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)

#define PI 3.141592653589793116
// PI = acos(-1)
#define ll long long
#define ld long double
#define fi first
#define se second
#define nl '\n'
#define all(x) x.begin(), x.end()
#define gg(t) cout << "Case #"<< t << ": ";
#define line cout << nl;
#define pline cout << nl << "______________________" << nl;
#define f(i, k, n) for(int i = k; i <= n; i++)
#define ms(x, a, n) f(i, 0, n-1) x[i] = a;

#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vvi vector<vector<int>>
#define vll vector<ll>
#define vvl vector<vector<ll>>

#define cia(a, n, s) for (int i = s; i < n + s; ++i) cin >> a[i];
#define ciap(a, n, s) for (int i = s; i < n + s; ++i) cin >> a[i].fi >> a[i].se;
#define ciab(a, b, n, s) for (int i = s; i < n + s; ++i) cin >> a[i] >> b[i];
#define cim(a, m, n, s) for (int i = s; i < m + s; ++i) for (int j = s; j < n + s; ++j) cin >> a[i][j];
#define tr(it, a) for(auto &it : a)
#define sz(x) int((x).size())

const ll mod = 1e9 + 7;
const int inf = 2e9;
const int lim = 1e6 + 5;

ll n, cnt1, cnt2, cnt3;

void run_case()
{
    cnt1 = cnt2 = cnt3 = inf;
    cin >> n;
    if (n % 2 == 0)
        cnt1 = n / 2;
    if (n % 3 == 0)
        cnt2 = n / 3;
    else if (n % 3 == 1)
    {
        cnt2 = 2;
        n -= 4;
        cnt2 += abs(n / 3);
        n += 4;
        cnt3 = 1;
        n += 2;
        cnt3 += abs(n / 3);
    }
    else if (n % 3 == 2)
    {
        cnt2 = 1;
        n -= 2;
        cnt2 += (n / 3);
    }
    cout << min({cnt1, cnt2, cnt3}) << nl;
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    // freopen("A.INP", "r", stdin);
    // freopen("A.OUT", "w", stdout);
    int t = 1;
    cin >> t;
    f(i, 1, t)
    {
        // gg(i);
        run_case();
    }
}


Comments

Submit
0 Comments
More Questions

628. Maximum Product of Three Numbers
1526A - Mean Inequality
1526B - I Hate 1111
1881. Maximum Value after Insertion
237. Delete Node in a Linked List
27. Remove Element
39. Combination Sum
378. Kth Smallest Element in a Sorted Matrix
162. Find Peak Element
1529A - Eshag Loves Big Arrays
19. Remove Nth Node From End of List
925. Long Pressed Name
1051. Height Checker
695. Max Area of Island
402. Remove K Digits
97. Interleaving String
543. Diameter of Binary Tree
124. Binary Tree Maximum Path Sum
1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts
501A - Contest
160A- Twins
752. Open the Lock
1535A - Fair Playoff
1538F - Interesting Function
1920. Build Array from Permutation
494. Target Sum
797. All Paths From Source to Target
1547B - Alphabetical Strings
1550A - Find The Array
118B - Present from Lena