补题链接:Here
1525A. Potion-making (思维
【题意描述】
作为一个魔法师,现在我想配置一杯药物浓度为 的药水,
每次操作能进行添加:
- 一升水
- 一升药物精华
作为魔法师并不在意最后配出来药水升数,请问最少进行多少次操作能得到结果
【思路分析】
先不管 是多少但最多
次操作一定能得到想要的结果,
但某些情况下是不需要执行那么多次结果的,比如 ,此时只要进行 4 次
这样的话很容易想到求一下 100 / 公约数即可
【AC 代码】
void solve() {int n;cin >> n;cout << 100 / __gcd(100, n) << "\n";}
1525B. Permutation Sort (思维
【题意描述】
给定一个可能无序的数组a(1~n),现在允许执行操作:对数组按任意情况重排序,但最多 区间内重排
请问在给定数组的情况下最少执行多少次?
【思路分析】
- 情况1 :本身就有序了,输出
- 情况2:
则排一次补集即可
- 特殊3:
,
此时先排
然后排
,再排
。3次一定能得到升序(当然3次排序还有其他写法)
- 情况4:不是以上情况的情况下一定
次排完
【AC 代码】
int main() {ios::sync_with_stdio(false), cin.tie(nullptr);int _;for (cin >> _; _--;) {int n;cin >> n;int a[n + 1];int cnt = 0;for (int i = 1; i <= n; ++i) {cin >> a[i];if (a[i] != i)cnt++;}if (cnt == 0) cout << "0\n";else if (a[1] == 1 || a[n] == n)cout << "1\n";else {if (a[1] == n and a[n] == 1)cout << "3\n";else cout << "2\n";}}return 0;}
1525C. Robot Collisions
比赛的时候看出来是一道很复杂的模拟题就没去想
这里贴一下大佬的代码作为学习,开启C++17以便使用 tuple 元组
【AC 代码】
int n, m;void solve() {cin >> n >> m;vector<pair<int, int>>a(n);for (int i = 0; i < n; ++i)cin >> a[i].first;for (int i = 0; i < n; ++i) {char c; cin >> c;a[i].second = (c == 'R' ? 1 : 0);}vector<tuple<int, int, int>>v[2];for (int i = 0; i < n; ++i) {int val = (a[i].first & 1);v[val].emplace_back(a[i].first, a[i].second, i);}vector<int>ans(n, -1);auto calc = [&](vector<tuple<int, int, int>>&v)->void {sort(v.begin(), v.end());stack<pair<int, int>>st;for (auto&[x, d, ind] : v) {if (d == 0) {if (st.empty())st.push(make_pair(-x, ind));else {ans[st.top().second] = ans[ind] = (x - st.top().first) / 2;st.pop();}} else st.push(make_pair(x, ind));}while (st.size() > 1) {pair<int, int> p = st.top();st.pop();p.first = m + (m - p.first);ans[p.second] = ans[st.top().second] = (p.first - st.top().first) / 2;st.pop();}};calc(v[0]), calc(v[1]);for (int &x : ans)cout << x << " ";cout << "\n";}
1525D. Armchairs
【题意描述】
现有 个座位,有最多不超过
的人已经就座了,但由于某种情况每个人都不能坐在原来的位置,需要移动到最近的空位置(并且别人原本的位置是不能坐的,别人在移动时剩余的人不能动),请输出最少的移动距离和 (每个人的移动距离为:new seat- old seat)
【思路分析】
这道题在比赛时用贪心没写出来,然后推测需要DP优化,但没处理好
赛后看了一下高Rank写法发现这道题应该先分开来保存空位置和有人的位置,利用记忆化去枚举有人的位置到所有的空位置的最小代价
【AC 代码】
const int N = 5010;int c[N], b[N], f[N];void solve() {int n;cin >> n;int cb = 0, cc = 0;// 分别记录下标for (register int i = 1, te; i <= n; ++i) {cin >> te;(te ? b[++cb] : c[++cc]) = i;}// 注意这里不需要初始化全部位置memset(f + 1, 0x3f, sizeof(int) * cb);for (int i = 1; i <= cc; i++) {for (int j = min(cb, i); j >= 1; j--) {f[j] = min(f[j], f[j - 1] + abs(b[j] - c[i]));}}cout << f[cb] << "\n";}
顺序记录一下没考虑全面时写的贪心解法 WA8
using ll = long long;const int N = 5010;int a[N], vis[N];int n;int rdfs(int x) {while ((a[x] == 1 or a[x] == -1) and x >= 1)x--;if (x <= 0)x = -1;return x;}int ldfs(int x) {while ((a[x] == 1 or a[x] == -1) and x <= n)x++;if (x > n)x = -1;return x;}void solve() {cin >> n;int cnt = 0;for (int i = 1; i <= n; ++i)cin >> a[i], cnt += a[i];if (cnt == 0) {cout << "0\n"; return ;}int ans = 0;for (int i = 1; i <= n; ++i) {if (a[i] == 1) {int r = rdfs(i), l = ldfs(i);cout << i << " " << l << " " << r << "\n";if (r == -1 and l != -1) ans += l - i, a[l] = -1;else if (l == -1 and r != -1)ans += i - r, a[r] = -1;else if (l != -1 and r != -1) {if (l - i <= i - r) a[l] = -1, ans += l - i;else a[r] = -1, ans += i - r;}}}cout << ans << "\n";}
