whp 6 - Codeforces Round #767 (Div. 2)

Posted by tet3828 on Wed, 26 Jan 2022 01:58:48 +0100

Codeforces Round #767 (Div. 2)

Codeforces Round #767 (Div. 2)

A. Download More RAM

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll N = 110;

struct node {
    ll a, b;
};

node mp[N];

signed main() {
    ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);

    ll T;
    cin >> T;
    while (T--) {
        ll n, k;
        cin >> n >> k;
        for (ll i = 1; i <= n; ++i) {
            cin >> mp[i].a;
        }
        for (ll i = 1; i <= n; ++i) {
            cin >> mp[i].b;
        }
        sort(mp + 1, mp + n + 1, [&](node x, node y) {
            return x.a < y.a;
        });
        for (ll i = 1; i <= n; ++i) {
            if (mp[i].a <= k) {
                k += mp[i].b;
            }
        }
        cout << k << endl;
    }
    return 0;
}

B. GCD Arrays

Topic: Given l and R means that there are numbers from l to r. You can select two numbers in one operation and put their product back. Ask if you can make the gcd of the remaining number greater than 1 in k operations

Makes gcd contain 2 the number of odd operands that combine all odd numbers with any even number

Determine if k is greater than or equal to the number of odd numbers

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;


signed main() {
    ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);

    ll T;
    cin >> T;
    while (T--) {
        int l, r, k;
        cin >> l >> r >> k;
        int num = (r - l + 1)/ 2;
        if((l & 1) && (r & 1)) num ++;
        if(k >= num)  {
            cout << "YES" << endl;
            continue;
        }
        if(r == l && r > 1) {
            cout << "YES" << endl;
            continue;
        }
        cout <<"NO" << endl;

    }
    return 0;
}

C. Meximum Array

Topic: Give an array a, which can be deleted from a by selecting several consecutive numbers from the beginning each time. Add their mex values to the end of b, requiring that the B array be as large as possible, and output the B array

Count the number and location of all the numbers (queues are used here because I find it convenient to delete them)

Maintain where numbers appear for each mex

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 2e5 + 10;
int a[N];
queue<int> g[N];
vector<int> res;

signed main() {
    ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);

    ll T;
    cin >> T;
    while (T--) {
        int n;
        cin >> n;
        res.clear();
        for (int i = 1; i <= n; ++i) {
            cin >> a[i];
            g[a[i]].push(i);
        }
        int pos = 0;
        while(pos < n) {
            for (int i = 0; i <= n; ++i) {
                if (g[i].empty()) {
                    res.push_back(i);
                    break;
                }
                pos = max(pos, g[i].front());
            }
            if(res[res.size() - 1] == 0) {
                pos ++;
                continue;
            }
            for(int i=0; i < res[res.size() - 1]; ++ i) {
                while(g[i].size() && g[i].front() <= pos) g[i].pop();
            }
        }

        cout << res.size() << endl;
        for(auto tmp : res) cout << tmp <<" ";
        cout << endl;

        //init
        for (int i = 0; i <= n; ++i) {
            while(!g[i].empty()) g[i].pop();
        }
    }
    return 0;
}

D. Peculiar Movie Preferences

Idea: There are n strings (no more than 3) that need to be kept at least one so that they can be joined together to form a palindrome string

If a string is itself a palindrome, simply leave it, except in this case

Otherwise, as long as the two pieces are spelled together as palindromes, that is

(AB)(BA)

(ABC)(BA)

(AB)(CBA)

(ABC)(CBA)

A direct judgement of these situations is possible. For BA and CBA, AB and ABC can be constructed, maintained with set, and the remaining ABC will be swept upside down once additional to the previous BA.

(As to why there is no proof without discrimination but as if there is no decision,

#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 10;

set<string> st;
string a[N];

inline bool check(string s) {
    if (s.size() == 1) return 1;
    if (s.size() == 2)
        return (s[0] == s[1]);
    if (s.size() == 3)
        return (s[0] == s[2]);
}

signed main() {
    ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);

    int T;
    cin >> T;
    while (T--) {
        int n;
        cin >> n;
        st.clear();
        bool flag = 0;
        for (int i = 1; i <= n; ++i) {
            string s;
            cin >> s;
            a[i] = s;
            if (check(s)) flag = 1;
            if (s.size() == 2) {
                string t = "";
                t = t + s[1] ;
                t = t + s[0];
                if (st.count(t)) flag = 1;
            }
            if (s.size() == 3) {
                string t = "";
                t = t + s[2];
                t = t + s[1];
                if (st.count(t)) flag = 1;
                t = t + s[0];
                if (st.count(t)) flag = 1;
            }
            st.insert(s);
        }
        if (flag) {
            cout << "YES" << endl;
            continue;
        }

        st.clear();
        for (int i = n; i >= 1; --i) {
            string s = a[i];
            if (s.size() == 3) {
                string t = "";
                t = t+ s[1];
                t = t + s[0];
                if (st.count(t)) {
                    flag = 1;
                    break;
                }
            }
            st.insert(s);
        }
        if (flag) {
            cout << "YES" << endl;
        } else {
            cout << "NO" << endl;
        }
    }
}

E. Grid Xor

Topic: n × n n \times n n × n has a number for each cell in the cell. Now tell the XOR sum of the cells around each cell and ask what the total XOR sum of the whole cell is.

Convert directly to a flip grid game (the one that turns black to white to black)

The two adjacent lattices can be grouped into hexagons, which can then be joined seamlessly as long as the corners are processed a little more.

Then the first row does a good job of laying the first row on the back

Thank you as usual for hoshimi classmates, toning the code tomorrow and sleeping

Topics: C++ Algorithm Dynamic Programming cf