Educational Codeforces Round 109 (Rated for Div. 2) 个人补题记录(A~D,AB思维,C模拟构造,D题DP)
补题链接:Here
1525A. Potion-making (思维
【题意描述】
作为一个魔法师,现在我想配置一杯药物浓度为 \(k\%\) 的药水,
每次操作能进行添加:
- 一升水
- 一升药物精华
作为魔法师并不在意最后配出来药水升数,请问最少进行多少次操作能得到结果
【思路分析】
先不管 \(k\) 是多少但最多 \(100\) 次操作一定能得到想要的结果,
但某些情况下是不需要执行那么多次结果的,比如 \(k = 25\) ,此时只要进行 4 次
这样的话很容易想到求一下 100 / 公约数即可
【AC 代码】
void solve() {
int n;
cin >> n;
cout << 100 / __gcd(100, n) << "\n";
}
1525B. Permutation Sort (思维
【题意描述】
给定一个可能无序的数组a(1~n),现在允许执行操作:对数组按任意情况重排序,但最多 \(n-1\) 区间内重排
请问在给定数组的情况下最少执行多少次?
【思路分析】
- 情况1 :本身就有序了,输出 \(0\)
- 情况2:\(a_1 = 1\ or\ a_n = n\) 则排一次补集即可
- 特殊3:\(n = 5\) ,\(a[] = \{5,.,.,.,1 \}\) 此时先排 \([1,n - 1]\) 然后排 \([2,n]\),再排 \([1,2]\)。3次一定能得到升序(当然3次排序还有其他写法)
- 情况4:不是以上情况的情况下一定 \(2\) 次排完
【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
【题意描述】
现有 \(n\) 个座位,有最多不超过 \(\frac{n}2\) 的人已经就座了,但由于某种情况每个人都不能坐在原来的位置,需要移动到最近的空位置(并且别人原本的位置是不能坐的,别人在移动时剩余的人不能动),请输出最少的移动距离和 (每个人的移动距离为: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";
}
Educational Codeforces Round 109 (Rated for Div. 2) 个人补题记录(A~D,AB思维,C模拟构造,D题DP)的更多相关文章
- [Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和)
[Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和) E. Permuta ...
- Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship
Problem Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...
- Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)
Problem Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...
- Educational Codeforces Round 43 (Rated for Div. 2)
Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...
- Educational Codeforces Round 35 (Rated for Div. 2)
Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...
- Educational Codeforces Round 63 (Rated for Div. 2) 题解
Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...
- Educational Codeforces Round 39 (Rated for Div. 2) G
Educational Codeforces Round 39 (Rated for Div. 2) G 题意: 给一个序列\(a_i(1 <= a_i <= 10^{9}),2 < ...
- Educational Codeforces Round 48 (Rated for Div. 2) CD题解
Educational Codeforces Round 48 (Rated for Div. 2) C. Vasya And The Mushrooms 题目链接:https://codeforce ...
随机推荐
- [CSAPP、APUE、UNP]文件、IO
<鸟哥的Linux私房菜:基础学习篇(第四版)> 第5章 Linux的文件权限与目录配置 第6章 LInux文件与目录管理(正在进行) <CSAPP> 第10章 系统级IO 1 ...
- springboot下添加日志模块和设置日志文件输出
前言 日志的使用将通过SLF4J来使用,SLF4J(Simple Logging Facade for Java)是一个为Java应用提供简单日志记录的接口.它的主要目标是在不同的日志系统之间提供一个 ...
- [ABC266G] Yet Another RGB Sequence
Problem Statement You are given integers $R$, $G$, $B$, and $K$. How many strings $S$ consisting of ...
- N100低功耗win11安装wsl2当入门nas
前言 最近入了一台16gb+512gb的N100,想着用来存些资源,当个nas,偶尔要用用windows系统,所以想直接在这上面搞个虚拟机算了,WSL2似乎是一个不错的选择,下面介绍捣鼓的教程. 没用 ...
- 苹果推信群发,苹果推信群发软件,iMessage群发系统
在当今数字化的时代,智能手机的普及率已达到了前所未有的高度,其中,苹果公司的iPhone无疑是市场上最受欢迎的智能手机之一,然而,与手机的广泛应用相伴的是,众多企业对于如何有效地向这些手机用户推送信息 ...
- --{module_name}_binary_host_mirror和--{module_name}_binary_site
--{module_name}_binary_host_mirror和--{module_name}_binary_site demo // .npmrc文件 sass_binary_site=htt ...
- STM32CubeMX教程1 工程建立
1.准备材料 开发板(STM32F407G-DISC1) ST-LINK/V2驱动 STM32CubeMX软件(Version 6.10.0) keil µVision5 IDE(MDK-Arm) 2 ...
- 玩转Python:用Python处理文本数据,附代码
Python 提供了多种库来处理纯文本数据,这些库可以应对从基本文本操作到复杂文本分析的各种需求.以下是一些常用的纯文本处理相关的库: str 类型: Python 内建的字符串类型提供了许多简便的方 ...
- STM32CubeMX教程7 TIM 通用定时器 - 输入捕获
1.准备材料 开发板(STM32F407G-DISC1) ST-LINK/V2驱动 STM32CubeMX软件(Version 6.10.0) keil µVision5 IDE(MDK-Arm) X ...
- 为什么Java中“1000==1000”为false,而”100==100“为true?
在日常编程中,我们经常遇到一些看似简单却隐藏着复杂逻辑的问题. 比如,你是否想过为什么在 Java 中表达式1000==1000会返回 false,而 100==100 却返回 true 呢? Int ...