数不改变,只需要最大值和次大值相加就好了
AC代码:
#include <bits/stdc++.h> using namespace std; using i64 = long long; void solve() { int n; cin >> n; vector<int> a(n); for (int &x : a) { cin >> x; } sort(a.begin(), a.end()); cout << 1ll * a[n - 1] + a[n - 2] << endl; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr);cout.tie(nullptr); #ifdef ONLINE_JUDGE #else freopen("in.txt", "r", stdin); #endif int _; cin >> _; while (_--) { solve(); } return 0; }
如果后面有开头的元素,那么就删去,如果没有直接输出剩下的字符串
AC代码:
#include <bits/stdc++.h> using namespace std; using i64 = long long; void solve() { string s; cin >> s; int len = s.size(); map<char, int> mp; int now = 0; for (int i = 0; i < len; i++) { mp[s[i]]++; } for (int i = 0; i < len; i++) { if (mp[s[i]] > 1) { now++; mp[s[i]]--; } else { break; } } for (int i = now; i < len; i++) { cout << s[i]; } cout << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr);cout.tie(nullptr); #ifdef ONLINE_JUDGE #else freopen("in.txt", "r", stdin); #endif int _; cin >> _; while (_--) { solve(); } return 0; }
逆向思维,对整个蛋糕开始切分,如果有这么大的一块蛋糕就剪枝,否则继续dfs下去,最后判断最后还有没有某种大小的蛋糕没有切到
AC代码:
#include <bits/stdc++.h> using namespace std; using i64 = long long; void solve() { int n; cin >> n; vector<int> a(n); i64 sum = 0; for (int i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; } multiset<i64> r(a.begin(), a.end()); int cnt = 1; auto dfs = [&](auto self, auto s) -> void { if (cnt > n) { return; } if (r.find(s) != r.end()) { r.erase(r.find(s)); return; } cnt++; self(self, s >> 1); self(self, s - (s >> 1)); }; dfs(dfs, sum); if (cnt == n && r.empty()) { cout << "YES" << '\n'; } else { cout << "NO" << "\n"; } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr);cout.tie(nullptr); #ifdef ONLINE_JUDGE #else freopen("in.txt", "r", stdin); #endif int _; cin >> _; while (_--) { solve(); } return 0; }
Codeforces Round #778 (Div. 1 + Div. 2, based on Technocup 2022 Final Round)(匿名函数的自我调用)
于 2022-03-21 19:27:18 首次发布