1343D - Constant Palindrome Sum

Click to show code.


using namespace std;
using ll = long long;
using vi = vector<int>;
int solve(vi a, int k)
{
    int n = (int)a.size(), ans = 1e9;
    vi cnt(2 * k + 2, 0), pre(2 * k + 2, 0);
    for (int i = 0; i < n / 2; ++i)
    {
        int l = a[i], r = a[n - i - 1];
        int ll = l + 1, lr = l + k;
        int rl = r + 1, rr = r + k;
        ++cnt[l + r];
        ++pre[min(ll, rl)];
        --pre[max(lr, rr) + 1];
    }
    for (int i = 1; i <= 2 * k + 1; ++i)
        pre[i] += pre[i - 1];
    for (int sum = 2; sum <= 2 * k; ++sum)
        ans = min(ans, (pre[sum] - cnt[sum]) + (n / 2 - pre[sum]) * 2);
    return ans;
}
int main(void)
{
    int t;
    cin >> t;
    while (t--)
    {
        int n, k;
        cin >> n >> k;
        vi a(n);
        for (auto &ai : a)
            cin >> ai;
        cout << solve(a, k) << endl;
    }
    return 0;
}


1342B - Binary Period

Click to show code.


using namespace std;
int n;
string s;
bool is_k1(void)
{
    char start = s[0];
    for (int i = 1; i < n; ++i)
    {
        if (s[i] != start)
            return false;
    }
    return true;
}
void solve(void)
{
    if (is_k1())
    {
        for (int i = 0; i < n; ++i)
            cout << s[i];
        return;
    }
    char last = s[0];
    cout << last;
    for (int i = 1; i < n; ++i)
    {
        if (s[i] != last)
        {
            cout << s[i];
        }
        else
        {
            cout << (last == '0' ? '1' : '0');
            cout << s[i];
            last = s[i];
        }
        last = s[i];
    }
}
int main(void)
{
    int t;
    cin >> t;
    while (t--)
    {
        cin >> s;
        n = s.size();
        solve();
        cout << endl;
    }
    return 0;
}


1342A - Road To Zero

Click to show code.


using namespace std;
using ll = long long;
ll x, y, a, b;
ll solve(void)
{
    ll ans = 0;
    if (x < y)
        swap(x, y);
    if (y == 0 and x == 0)
        return 0;
    if (y == 0)
        return a * x;
    ans += a * (x - y);
    x = y;
    if (b < a * 2)
        ans += b * (x);
    else
        ans += (a * 2) * (x);
    return ans;
}
int main(void)
{
    int t;
    cin >> t;
    while (t--)
    {
        cin >> x >> y;
        cin >> a >> b;
        cout << solve() << endl;
    }
    return 0;
}


1341C - Nastya Strange Generator

Click to show code.


using namespace std;
const int NMAX = 1e5 + 11;
int n, pos[NMAX], p[NMAX];
bool solve(void)
{
    bool vis[n];
    memset(vis, false, sizeof(*vis) * n);
    int cur = 0;
    while (cur < n)
    {
        if (vis[pos[cur]])
        {
            ++cur;
            continue;
        }
        if (not vis[pos[cur]])
        {
            vis[pos[cur++]] = true;
            while (pos[cur - 1] + 1 < n and not vis[pos[cur - 1] + 1])
            {
                if (pos[cur] != pos[cur - 1] + 1)
                    return false;
                vis[cur++] = true;
            }
        }
    }
    return true;
}
int main(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t, pi;
    cin >> t;
    while (t--)
    {
        cin >> n;
        for (int i = 0; i < n; ++i)
        {
            cin >> pi;
            p[i] = pi - 1;
            pos[pi - 1] = i;
        }
        cout << (solve() ? "Yes" : "No") << endl;
    }
    return 0;
}


1341B - Nastya And Door

Click to show code.


using namespace std;
using ll = long long;
const int NMAX = 2 * 1e5 + 11;
int peaks[NMAX];
int a[NMAX];
int n, k;
int main(void)
{
    int t, maxpeaks, pos, cur;
    cin >> t;
    while (t--)
    {
        cin >> n >> k;
        for (int i = 0; i < n; ++i)
            cin >> a[i];
        peaks[0] = 0;
        for (int i = 1; i < (n - 1); ++i)
        {
            peaks[i] = peaks[i - 1];
            if (a[i - 1] < a[i] and a[i] > a[i + 1])
                ++peaks[i];
        }
        peaks[n - 1] = peaks[n - 2];
        pos = 0;
        maxpeaks = 0;
        for (int i = n - 1; (i - k + 1) >= 0; --i)
        {
            cur = peaks[i - 1] - peaks[i - k + 1];
            if (cur >= maxpeaks)
            {
                maxpeaks = cur;
                pos = i - k + 1;
            }
        }
        cout << maxpeaks + 1 << " " << pos + 1 << endl;
    }
}


1341A - Nastya And Rice

Click to show code.


using namespace std;
using ll = long long;
int main(void)
{
    int t, n, a, b, c, d;
    ll minsum, maxsum;
    bool ans;
    cin >> t;
    while (t--)
    {
        cin >> n >> a >> b >> c >> d;
        minsum = n * (a - b);
        maxsum = n * (a + b);
        ans = ((minsum) > (c + d)) or (maxsum < (c - d));
        cout << (not ans ? "Yes" : "No") << endl;
    }
    return 0;
}


1339E1 - Weights Division

Click to show code.


using namespace std;
using ii = pair<int, int>;
using ll = long long;
const int NMAX = 1e5 + 11;
int n, leaves[NMAX];
ll s, cost[NMAX], contribution[NMAX];
vector<ii> g[NMAX];
void dfs(int u, int p = 0)
{
    for (auto [v, w] : g[u])
    {
        if (v == p)
            continue;
        dfs(v, u);
        cost[v] = w;
        contribution[v] = cost[v] * leaves[v];
        leaves[u] += leaves[v];
    }
    if (leaves[u] == 0)
        leaves[u]++;
}
void reset(void)
{
    for (int u = 0; u < n; u++)
        g[u].clear();
    memset(contribution, 0, sizeof(contribution));
    memset(cost, 0, sizeof(cost));
    memset(leaves, 0, sizeof(leaves));
}
int main(void)
{
    ios_base::sync_with_stdio(false), cin.tie(NULL);
    int t, u, v, w;
    cin >> t;
    while (t--)
    {
        cin >> n >> s;
        reset();
        for (int i = 0; i < n - 1; ++i)
        {
            cin >> u >> v >> w, --u, --v;
            g[u].emplace_back(v, w);
            g[v].emplace_back(u, w);
        }
        dfs(0);
        ll sum = accumulate(contribution + 1, contribution + n, (ll)0);
        int ans = 0;
        set<pair<ll, int>, greater<pair<ll, int>>> edges;
        for (int u = 1; u < n; ++u)
            edges.insert({contribution[u] - (cost[u] / 2 * leaves[u]), u});
        while (sum > s)
        {
            auto it = edges.begin();
            auto [diff, u] = *it;
            cost[u] /= 2;
            contribution[u] = leaves[u] * cost[u];
            sum -= diff;
            edges.erase(it);
            edges.insert({contribution[u] - (cost[u] / 2 * leaves[u]), u});
            ++ans;
        }
        cout << ans << endl;
    }
    return 0;
}


1337B - Kana Dragon Quest

Click to show code.


using namespace std;
inline void void_absorption(int &x) { x = x / 2 + 10; }
inline void lightning_strike(int &x) { x = x - 10; }
bool solve(int x, int n, int m)
{
    while (n-- > 0 and x > 20)
        void_absorption(x);
    while (m-- > 0 and x > 0)
        lightning_strike(x);
    return x <= 0;
}
int main(void)
{
    int t, x, n, m;
    cin >> t;
    while (t--)
    {
        cin >> x >> n >> m;
        cout << (solve(x, n, m) ? "YES" : "NO") << endl;
    }
    return 0;
}


1337A - Ichime Triangle

Click to show code.


using namespace std;
int a, b, c, d;
int x, y, z;
int main(void)
{
    int t;
    cin >> t;
    while (t--)
    {
        cin >> a >> b >> c >> d;
        x = b;
        y = c;
        z = c;
        cout << x << " " << y << " " << z << endl;
    }
    return 0;
}


1335A - Candies Two Sisters

Click to show code.


using namespace std;
using ll = long long;
ll n;
ll solve(void)
{
    ll ans = n - ceil((n + 1) / 2.0);
    if (ans == n - ans)
        return 0;
    return ans;
}
int main(void)
{
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n;
        cout << solve() << endl;
    }
    return 0;
}