牛客小白月赛87

1|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; 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; }

2|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; }

3|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; }

4|0小苯的数组构造


保证bi0是一个比较容易想到的思路,这样的话最优解一定是使得b1=0,这样后面只要使得bi尽可能的小即可

#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; }

5|0小苯的数组切分


首先前两段的越长则和一定越长,第三段越长和越小。所以r=n1,枚举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; }

__EOF__

本文作者PHarr
本文链接https://www.cnblogs.com/PHarr/p/18019733.html
关于博主:前OIer,SMUer
版权声明CC BY-NC 4.0
声援博主:如果这篇文章对您有帮助,不妨给我点个赞
posted @   PHarr  阅读(11)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示