牛客小白月賽30(我的題解)

最近沉迷寫前端代碼,如今緩過來簡單補下html

比賽連接:https://ac.nowcoder.com/acm/contest/9667#question前端

A:黑白邊

簡單的並查集模板ios

B:最好的寶石

待補c++

C:滑板上樓梯

math數組

// Author : RioTian
// Time : 20/12/07
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
int main() {
    // freopen("in.txt", "r", stdin);
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    ll n, cnt = 0;
    cin >> n;
    ll ans = n / 4;
    cnt += ans * 2;
    ans = n % 4;
    if (ans < 3)
        cnt += ans;
    else
        cnt++;
    cout << cnt << endl;
}

D:GCD

素數篩反向選spa

// Author : RioTian
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
int n;
bool isPrime[N];
int main() {
    // freopen("in.txt", "r", stdin);
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> n;
    int cnt = 0;
    for (int i = 2; i <= n; i++) {
        if (!isPrime[i]) {
            cnt++;
            for (int j = i * 2; j <= n; j += i) {
                isPrime[j] = 1;
            }
        }
    }
    // cout << cnt << endl;
    cout << (n <= 3 ? -1 : cnt + 2) << endl;
}

E:牛牛的加法

模擬題code

若是兩數字位數不一樣就先補零便於後面處理htm

// Author : RioTian
// Time : 20/12/07
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
string a, b, ans;
int cot = 0, len;
bool lin;
int main() {
    // freopen("in.txt", "r", stdin);
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> a >> b;
    if (a.size() > b.size()) {
        cot = a.size() - b.size();
        while (cot--)
            b = '0' + b;
    } else {
        cot = b.size() - a.size();
        while (cot--)
            a = '0' + a;
    }
    len = a.size();
    ans = "";
    lin = true;
    for (int i = 0; i < len; i++) {
        if ((a[i] - '0' + b[i] - '0') % 10 == 0 && lin)
            continue;
        lin = false;
        ans += (char)((a[i] - '0' + b[i] - '0') % 10 + '0');
    }
    if (ans.size() == 0)
        ans += "0";
    cout << ans << endl;
}

F:石子合併

模擬一下樣例1,會發現16 + 15,容易發下16爲5項和,而\(15 = max(a[i]) * (n - 2)\)對象

// Author : RioTian
// Time : 20/12/07
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
int n;
ll a[N];
int main() {
    // freopen("in.txt", "r", stdin);
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> n;
    for (int i = 1; i <= n; ++i)
        cin >> a[i];
    ll cnt = 0, Max = 0;
    for (int i = 1; i <= n; ++i) {
        Max = max(Max, a[i]);
        cnt += a[i];
    }
    cout << Max * (n - 2) + cnt;
}

G:滑板比賽

相似二分圖,在知足條件的狀況下儘可能增長匹配數。blog

直接寫二分圖確定很麻煩,但轉過來一想,爲數組排序呢?

\[2\ 3\ 4\ 6\ 7\\ 2\ 3\ 4\ 4\ 6\\ \\ 從後往前匹配對象(數值儘可能相近,這樣就能夠把小的數字留給其餘的)\\ 7匹配6\\ 6匹配4\\ 4匹配3\\ 3匹配2\\ 因爲不存在空閒比2小的數字了,因此最終成功匹配數爲4 \]

// Author : RioTian
// Time : 20/12/07
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
int n, m;
ll a[N], b[N];
int main() {
    // freopen("in.txt", "r", stdin);
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= n; ++i)
        cin >> a[i];
    for (int i = 1; i <= m; ++i)
        cin >> b[i];
    sort(a + 1, a + 1 + n);
    sort(b + 1, b + 1 + m);
    int i, j;
    int ans = 0, l = 1, r = n;
    for (i = m; i >= 1; i--) {
        if (b[i] < a[r])
            r--, ans++;
        else
            l++;
    }
    cout << ans << endl;
}

H:第 k 小

看到題的瞬間想用線段樹,但簡單算了下時間複雜度,可能會超時,因此轉而想了想主席樹(沒了解的同窗能夠看這裏:Here),但以爲牛客應該不會在小白賽出這種碼量大的題。

被迫無奈看了下題解,發現原來快速寫下優先隊列便可(都沒想到這個)

如下是AC代碼

// Author : RioTian
// Time : 20/12/07
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
ll a[N];
priority_queue<ll> que;
int main() {
    // freopen("in.txt", "r", stdin);
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int n, m, k;
    cin >> n >> m >> k;
    for (int i = 1; i <= n; ++i)
        cin >> a[i];
    sort(a + 1, a + 1 + n);
    int ans = min(n, k);
    for (int i = 1; i <= ans; ++i)
        que.push(a[i]);
    int opt, x;
    while (m--) {
        cin >> opt;
        if (opt == 1) {
            cin >> x;
            if (que.size() <= k - 1)
                que.push(x);
            else {
                if (x > que.top())
                    continue;
                que.push(x), que.pop();
            }
        } else {
            if (que.size() != k)
                cout << -1 << endl;
            else
                cout << que.top() << endl;
        }
    }
}

I:區間異或

J:小遊戲

這兩個待補

相關文章
相關標籤/搜索