1365B - Trouble Sort

Click to show code.


using namespace std;
const int NMAX = 5e2 + 11;
int n, a[NMAX];
bool b[NMAX];
bool sorted(void)
{
    for (int i = 1; i < n; ++i)
        if (a[i - 1] > a[i])
            return false;
    return true;
}
bool solve(void)
{
    if (sorted())
        return true;
    int zeros, ones;
    zeros = ones = 0;
    for (int i = 0; i < n; ++i)
    {
        if (b[i])
            ones += 1;
        else
            zeros += 1;
    }
    if (zeros > 0 and ones > 0)
        return true;
    else
        return false;
}
int main(void)
{
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n;
        for (int i = 0; i < n; ++i)
            cin >> a[i];
        for (int i = 0; i < n; ++i)
            cin >> b[i];
        cout << (solve() ? "Yes" : "No") << endl;
    }
    return 0;
}


1365A - Matrix Game

Click to show code.


using namespace std;
int main(void)
{
    int t, n, m;
    bool aij;
    cin >> t;
    while (t--)
    {
        cin >> n >> m;
        bool row_marked;
        int free_rows = n;
        int free_cols = m;
        vector<bool> cols_marked(m, false);
        for (int row = 0; row < n; ++row)
        {
            row_marked = false;
            for (int col = 0; col < m; ++col)
            {
                cin >> aij;
                cols_marked[col] = cols_marked[col] | aij;
                row_marked |= aij;
            }
            free_rows -= row_marked;
        }
        for (int col = 0; col < m; ++col)
            free_cols -= cols_marked[col];
        int x = min(free_rows, free_cols);
        if (x % 2 == 0)
            cout << "Vivek" << endl;
        else
            cout << "Ashish" << endl;
    }
    return 0;
}


1364C - Ehab Prefix Mex

Click to show code.


using namespace std;
using vi = vector<int>;
const int NMAX = 1e5 + 11;
int n, a[NMAX];
bool visited[NMAX];
void solve(void)
{
    vi ans(n + 1, NMAX);
    for (int i = 1; i <= n; ++i)
    {
        if (a[i] != a[i - 1])
        {
            ans[i] = a[i - 1];
            visited[ans[i]] = true;
        }
    }
    visited[a[n]] = true;
    int p = 0;
    for (int i = 1; i <= n; ++i)
    {
        while (visited[p])
            ++p;
        if (ans[i] == NMAX)
        {
            ans[i] = p;
            visited[p] = true;
        }
        cout << ans[i] << " ";
    }
}
int main(void)
{
    cin >> n;
    for (int i = 1; i <= n; ++i)
        cin >> a[i];
    solve();
    return 0;
}


1364B - Most Distanced Subsequence

Click to show code.


using namespace std;
using vi = vector<int>;
const int NMAX = 1e5 + 11;
int n, a[NMAX];
bool increasing[NMAX];
void solve(void)
{
    vi ans;
    ans.push_back(a[0]);
    for (int i = 1; i <= n; ++i)
    {
        if (increasing[i] != increasing[i - 1])
            ans.push_back(a[i - 1]);
    }
    cout << ans.size() << endl;
    for (auto x : ans)
        cout << x << " ";
    cout << endl;
}
int main(void)
{
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n;
        for (int i = 0; i < n; ++i)
        {
            cin >> a[i];
            if (i > 0)
                increasing[i] = (a[i] >= a[i - 1]);
        }
        increasing[0] = increasing[1];
        increasing[n] = !increasing[n - 1];
        solve();
    }
    return 0;
}


1364A - Xxxxx

Click to show code.


using namespace std;
using ll = long long;
const int NMAX = 1e5 + 11;
int n, x, a[NMAX];
int solve(void)
{
    int l, r;
    if (accumulate(a, a + n, (ll)0) % x != 0)
        return n;
    l = 0;
    while (l < n and (a[l] % x == 0))
        ++l;
    r = n - 1;
    while (r >= 0 and (a[r] % x == 0))
        --r;
    if (l == n or r == -1)
        return -1;
    return n - min(l + 1, n - r);
}
int main(void)
{
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n >> x;
        for (int i = 0; i < n; ++i)
            cin >> a[i];
        cout << solve() << endl;
    }
    return 0;
}


1363B - Subsequence Hate

Click to show code.


using namespace std;
using vi = vector<int>;
int solve(string s)
{
    int n = s.size();
    vi acc(n, 0);
    for (int i = 0; i < n; ++i)
    {
        acc[i] = (s[i] == '1');
        if (i != 0)
            acc[i] += acc[i - 1];
    }
    auto ones = [=](int l, int r) -> int {
        int ans = acc[r];
        if (l != 0)
            ans -= acc[l - 1];
        return ans;
    };
    auto zeros = [=](int l, int r) -> int {
        int ans = (r - l + 1) - ones(l, r);
        return ans;
    };
    int ans = INT_MAX;
    ans = min(ans, acc[n - 1]);
    ans = min(ans, n - acc[n - 1]);
    for (int i = 0; i < n - 1; ++i)
    {
        ans = min(ans, ones(0, i) + zeros(i + 1, n - 1));
        ans = min(ans, zeros(0, i) + ones(i + 1, n - 1));
    }
    return ans;
}
int main(void)
{
    int t;
    string s;
    cin >> t;
    while (t--)
    {
        cin >> s;
        cout << solve(s) << endl;
    }
    return 0;
}


1363A - Odd Selection

Click to show code.


using namespace std;
bool solve(int x, int evens, int odds)
{
    if (odds == 0)
        return false;
    if (odds % 2 == 0)
        odds -= 1;
    int xp = x - 1;
    odds -= 1;
    xp -= min(odds, 2 * (xp / 2));
    return (xp - evens <= 0);
}
int main(void)
{
    int t, n, x, ai;
    cin >> t;
    while (t--)
    {
        cin >> n >> x;
        int rem[2] = {0, 0};
        for (int i = 0; i < n; ++i)
        {
            cin >> ai;
            rem[ai % 2]++;
        }
        cout << (solve(x, rem[0], rem[1]) ? "Yes" : "No") << endl;
    }
    return 0;
}


1362D - Johnny Contribution

Click to show code.


using namespace std;
using vi = vector<int>;
const int NMAX = 5e5 + 11;
int n, m, topic[NMAX];
vi g[NMAX], blogs;
vi solve(void)
{
    auto cmp = [](int x, int y) { return topic[x] < topic[y]; };
    sort(blogs.begin(), blogs.end(), cmp);
    for (auto b : blogs)
    {
        sort(g[b].begin(), g[b].end(), cmp);
        int nxt = 1;
        for (auto v : g[b])
            if (nxt == topic[v])
                ++nxt;
        if (topic[b] != nxt)
            return {-1};
    }
    return blogs;
}
int main(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int u, v;
    cin >> n >> m;
    for (int i = 1; i <= m; ++i)
    {
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    for (int i = 1; i <= n; ++i)
    {
        cin >> topic[i];
        blogs.push_back(i);
    }
    for (auto u : solve())
        cout << u << " ";
    cout << endl;
    return 0;
}


1362C - Johnny Another Rating Drop

Click to show code.


using namespace std;
using ll = long long;
int main(void)
{
    int t;
    ll n;
    cin >> t;
    while (t--)
    {
        cin >> n;
        ll ans = 0;
        ll rate = 1;
        for (int i = 0; i < 64; ++i)
        {
            ans += max((ll)0, (ll)((n + 1 + rate - 1) / rate) - 1);
            rate *= 2;
        }
        cout << ans << endl;
    }
    return 0;
}


1362B - Johnny Hobbies

Click to show code.


using namespace std;
const int NMAX = 1024 + 11;
int n, s[NMAX];
int solve(void)
{
    sort(s, s + n);
    for (int k = 1; k < NMAX; ++k)
    {
        int cnt = 0;
        for (int i = 0; i < n; ++i)
            if (binary_search(s, s + n, s[i] ^ k))
                ++cnt;
        if (cnt == n)
            return k;
    }
    return -1;
}
int main(void)
{
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n;
        for (int i = 0; i < n; ++i)
            cin >> s[i];
        cout << solve() << endl;
    }
    return 0;
}