牛客小白月赛87

小苯的石子游戏

贪心

#include<bits/stdc++.h>

using namespace std;

#define int long long
using vi = vector<int>;
using i32 = int32_t;
using pii = pair<int, int>;
using vii = vector<pii>;

const int inf = 1e9, INF = 1e18;
const int mod = 1e9 + 7;


void solve() {
    int n;
    cin >> n;
    priority_queue<int> q;
    for (int x; n; n--) cin >> x , q.push(x);
    int a = 0, b = 0;
    while (not q.empty()) {
        if (not q.empty()) a += q.top(), q.pop();
        if (not q.empty()) b += q.top(), q.pop();
    }
    if( a > b ) cout << "Alice\n";
    else cout << "Bob\n";
}

i32 main() {
    int TC;
    for (cin >> TC; TC; TC--)
        solve();
    return 0;
}

小苯的排序疑惑

判断第一个是不是最小,最后一个是不是最大

#include<bits/stdc++.h>

using namespace std;

#define int long long
using vi = vector<int>;
using i32 = int32_t;
using pii = pair<int, int>;
using vii = vector<pii>;

const int inf = 1e9, INF = 1e18;
const int mod = 1e9 + 7;


void solve() {
    int n;
    cin >> n;
    vi a(n);
    for (auto &i: a) cin >> i;
    if (a.front() == *min_element(a.begin(), a.end()))
        cout << "YES\n";
    else if (a.back() == *max_element(a.begin(), a.end()))
        cout << "YES\n";
    else
        cout << "NO\n";
    return;
}

i32 main() {
    int TC;
    for (cin >> TC; TC; TC--)
        solve();
    return 0;
}

小苯的IDE括号删除

用两个双端队列模拟一下,一个储存光标左侧,一个储存光标右侧

#include<bits/stdc++.h>

using namespace std;

#define int long long
using vi = vector<int>;
using i32 = int32_t;
using pii = pair<int, int>;
using vii = vector<pii>;

const int inf = 1e9, INF = 1e18;
const int mod = 1e9 + 7;

const vi dx = {0, 0, 1, -1}, dy = {1, -1, 0, 0};

i32 main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int n, m;
    string s, op;
    cin >> n >> m >> s;
    deque<char> a, b;
    for (int i = 0, flag = 0; i < n; i++) {
        if (s[i] == 'I') flag = 1;
        else if (flag == 0) a.push_back(s[i]);
        else b.push_back(s[i]);
    }
    while (m--) {
        cin >> op;
        if (op == "backspace") {
            if (a.empty()) continue;
            if (a.back() == '(' and b.front() == ')') b.pop_front();
            a.pop_back();
        } else if (op == "delete") {
            if (b.empty()) continue;
            b.pop_front();
        } else if (op == "<-") {
            if (a.empty()) continue;
            b.push_front(a.back()), a.pop_back();
        } else {
            if (b.empty()) continue;
            a.push_back(b.front()), b.pop_front();
        }
    }
    for (auto i: a) cout << i;
    cout << "I";
    for (auto i: b) cout << i;
    cout << "\n";
    return 0;
}

小苯的数组构造

保证\(b_i\ge0\)是一个比较容易想到的思路,这样的话最优解一定是使得\(b_1=0\),这样后面只要使得\(b_i\)尽可能的小即可

#include<bits/stdc++.h>

using namespace std;

using i32 = int32_t;
using i64 = long long;
using i128 = __int128;
using ldb = long double;

#define int i64

using vi = vector<int>;

using pii = pair<int, int>;
using vii = vector<pii>;

const int inf = INT_MAX, INF = 1e18;
const int mod = 998244353;
const vi dx = {0, 0, 1, -1}, dy = {1, -1, 0, 0};
using edge = array<int, 3>;

int power(int x, int y) {
    int ans = 1;
    while (y) {
        if (y & 1) ans = ans * x % mod;
        x = x * x % mod, y /= 2;
    }
    return ans;
}

int inv(int x) {
    return power(x, mod - 2);
}

int n, y, res;

set<int> vis;

i32 main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int n;
    cin >> n;
    vi a(n), b(n);
    for (auto &i: a) cin >> i;

    for (int i = 1; i < n; i++) {
        if (a[i] > a[i - 1]) continue;
        b[i] = a[i - 1] - a[i], a[i] = a[i - 1];
    }
    for( auto i : b)
        cout << i << " ";
    return 0;
}

小苯的数组切分

首先前两段的越长则和一定越长,第三段越长和越小。所以\(r=n-1\),枚举\(l\)即可

#include<bits/stdc++.h>

using namespace std;

using i32 = int32_t;
using i64 = long long;
using i128 = __int128;
using ldb = long double;

#define int i64

using vi = vector<int>;

using pii = pair<int, int>;
using vii = vector<pii>;

const int inf = INT_MAX, INF = 1e18;
const int mod = 998244353;
const vi dx = {0, 0, 1, -1}, dy = {1, -1, 0, 0};

void solve() {
    int n;
    cin >> n;
    vi a(n), b(n);
    for (auto &i: a) cin >> i;
    for (int x = 0, i = 0; i < n; i++)
        x ^= a[i], b[i] = x;
    int res = 0;
    for (int i = n - 2, x = 0; i > 0; i--) {
        x |= a[i];
        res = max(res, b[i - 1] + x + a[n - 1]);
    }
    cout << res << "\n";
}

i32 main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    solve();
    return 0;
}
posted @ 2024-02-18 17:57  PHarr  阅读(8)  评论(0编辑  收藏  举报