abc146_c - Buy Integer

Click to show code.


using namespace std;
int const NMAX = 1e5 + 10;
int const PTWO = 30;
int const BITS = sizeof(int) * 8;
int _succ[NMAX][PTWO];
int succ(int u, int k)
{
    if (k == 0)
        return u;
    do
    {
        int i = BITS - __builtin_clz(k) - 1;
        u = _succ[u][i];
        k ^= 1 << i;
    } while (k > 0);
    return u;
}
int main(void)
{
    int n;
    string s;
    cin >> s;
    n = s.size();
    for (int u = 1; u <= n; ++u)
        _succ[u][0] = (s[u - 1] == 'R' ? u + 1 : u - 1);
    for (int p = 1; p < PTWO; ++p)
        for (int u = 1; u <= n; ++u)
            _succ[u][p] = _succ[_succ[u][p - 1]][p - 1];
    vector<int> ans(n, 0);
    for (int u = 1; u <= n; ++u)
        ans[succ(u, NMAX) - 1]++;
    for (auto x : ans)
        cout << x << " ";
    cout << endl;
    return 0;
}


abc146_a - Cant Wait For Holiday

Click to show code.


using namespace std;
vector<string> days = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
int main(void)
{
    string s;
    cin >> s;
    int n = days.size();
    int pos = distance(days.begin(), find(days.begin(), days.end(), s));
    cout << n - pos << endl;
    return 0;
}


abc144_e - Gluttony

Click to show code.


using namespace std;
using ll = long long;
using predicate = function<bool(ll)>;
int const NMAX = 2e5 + 11;
ll n, k, a[NMAX], f[NMAX];
bool ok(ll x)
{
    ll tk = k;
    for (int i = 0; i < n; ++i)
    {
        tk -= max((ll)0, a[i] - x / f[i]);
        if (tk < 0)
            return false;
    }
    return true;
}
ll bs(ll l, ll r, predicate p)
{
    while (l < r)
    {
        ll m = l + (r - l) / 2;
        if (p(m))
            r = m;
        else
            l = m + 1;
    }
    return l;
}
int main(void)
{
    cin >> n >> k;
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    for (int i = 0; i < n; ++i)
        cin >> f[i];
    sort(a, a + n);
    sort(f, f + n, greater<int>());
    cout << bs(0, 1e12 + 11, ok) << endl;
    return 0;
}


abc144_c - Walk Multiplication Table

Click to show code.


using namespace std;
using ll = long long;
int main(void)
{
    ll n;
    cin >> n;
    ll d0 = 1;
    for (int i = 1, m = sqrt(n); i <= m; ++i)
    {
        if (n % i == 0)
        {
            d0 = i;
        }
    }
    cout << d0 + n / d0 - 2 << endl;
    return 0;
}


abc144_b - 81

Click to show code.


using namespace std;
bool ok(int n)
{
    for (int i = 1; i <= 9; ++i)
    {
        for (int j = 1; j <= 9; ++j)
        {
            if (n == i * j)
                return true;
        }
    }
    return false;
}
int main(void)
{
    int n;
    cin >> n;
    cout << (ok(n) ? "Yes" : "No") << endl;
    return 0;
}


abc144_a - 9X9

Click to show code.


using namespace std;
inline bool in_range(int x) { return 1 <= x and x <= 9; }
int main(void)
{
    int a, b;
    cin >> a >> b;
    if (in_range(a) and in_range(b))
    {
        cout << a * b << endl;
    }
    else
        cout << -1 << endl;
    return 0;
}


abc141_d - Powerful Discount Tickets

Click to show code.


using namespace std;
using ll = long long;
int main(void)
{
    int n, m, ai;
    multiset<int, greater<int>> s;
    cin >> n >> m;
    for (int i = 0; i < n; ++i)
        cin >> ai, s.insert(ai);
    while (m--)
    {
        int biggest = *s.begin();
        if (biggest == 0)
            break;
        s.erase(s.begin());
        s.insert(biggest / 2);
    }
    cout << accumulate(s.begin(), s.end(), (ll)0) << endl;
    return 0;
}


abc139_b - Power Socket

Click to show code.


using namespace std;
using ll = long long;
using vi = vector<int>;
int main(void)
{
    int a, b;
    cin >> a >> b;
    b--;
    a--;
    cout << (a + b - 1) / a << endl;
    return 0;
}


abc138_d - Ki

Click to show code.


using namespace std;
using vi = vector<int>;
int const NMAX = 2e5 + 11;
int n, ans[NMAX];
vi g[NMAX];
void dfs(int u, int p = -1)
{
    for (auto v : g[u])
    {
        if (v == p)
            continue;
        ans[v] += ans[u];
        dfs(v, u);
    }
}
int main(void)
{
    int q, u, v, p, x;
    cin >> n >> q;
    for (int i = 0; i < n - 1; ++i)
    {
        cin >> u >> v, u--, v--;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    while (q--)
    {
        cin >> p >> x, p--;
        ans[p] += x;
    }
    dfs(0);
    for_each(ans, ans + n, [](int x) { cout << x << " "; }), cout << endl;
    return 0;
}


abc135_c - City Savers

Click to show code.


using namespace std;
using vi = vector<int>;
using ll = long long;
int main(void)
{
    int n;
    ll cur_killed, total_killed;
    vi a, b;
    cin >> n;
    a.resize(n + 1), b.resize(n);
    for (auto &ai : a)
        cin >> ai;
    for (auto &bi : b)
        cin >> bi;
    total_killed = 0;
    for (int i = 0; i < n; ++i)
    {
        cur_killed = min(b[i], a[i] + a[i + 1]);
        total_killed += cur_killed;
        a[i + 1] -= max(0LL, cur_killed - a[i]);
    }
    cout << total_killed << endl;
    return 0;
}