补题链接: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)的更多相关文章

  1. [Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和)

    [Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和) E. Permuta ...

  2. 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 ...

  3. 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 ...

  4. Educational Codeforces Round 43 (Rated for Div. 2)

    Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...

  5. Educational Codeforces Round 35 (Rated for Div. 2)

    Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...

  6. 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 ...

  7. 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 ...

  8. Educational Codeforces Round 63 (Rated for Div. 2) 题解

    Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...

  9. 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 < ...

  10. Educational Codeforces Round 48 (Rated for Div. 2) CD题解

    Educational Codeforces Round 48 (Rated for Div. 2) C. Vasya And The Mushrooms 题目链接:https://codeforce ...

随机推荐

  1. 多项目git账户用户名和邮箱设置以及局部github代理

    因为公司使用自建的gitlab服务器所以需要配置两个git账户分别用来访问公司仓库和自己的github仓库. 前言: 首先给大家梳理一下多用户名或者说多邮箱使用git的理解误区.我们需要知道的是我们的 ...

  2. Pycharm 2022 取消双击 shift 搜索框

    Pycharm取消双击shift搜索框 基于PyCharm 2022.3.2 (Professional Edition),旧版本修改方式自行搜索 双击shift弹出搜索框,输入内容double mo ...

  3. 使用咱们公司的DataInside可视化产品配置了一个教育行业的大屏展示软件

    今天在公司用配置了一个可视化大屏软件,大家觉得如何?

  4. 分布式文件系统HDFS简介

    HDFS实现目标: 兼容廉价的硬件设备    支持大数据集   实现流数据读写   支持简单的文件模型    强大的跨平台兼容性 自身的局限性: 不适合低延迟的数据访问   无法高效储存大量小文件  ...

  5. 第一章 JavaEE应用和开发环境

    1.1 java EE应用概述 1.java EE的分层模型 数据库--[提供持久化服务]-->Domain Object层 --[封装]--〉DAO层--[提供数据访问服务]-->业务逻 ...

  6. 理解 Paimon changelog producer

    介绍 目的 Chaneglog producer 的主要目的是为了在 Paimon 表上产生流读的 changelog, 所以如果只是批读的表是可以不用设置 Chaneglog producer 的. ...

  7. JXNU acm选拔赛 最小的数

    最小的数 Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other) Total Submissi ...

  8. 介绍一款轻量型 Web SCADA 组态软件

    ​ 随着互联网.物联网技术的快速发展,图扑物联基于多年研发积累和私有部署实践打磨.以及对业务场景的深入理解,推出了适用于物联网应用场景的轻量型云组态软件. 该产品采用 B/S 架构,提供 Web 管理 ...

  9. pytest框架学习-fixture

    一.fixture是什么 被@pytest.fixture()装饰器装饰的函数就是一个fixture,fixture可以灵活的为不同范围的测试用例提供前置和后置操作,以及向测试用例传递测试数据. 二. ...

  10. Python汉诺塔递归算法实现

    关于用递归实现的原理,请查看我之前的文章: C语言与汉诺塔 C#与汉诺塔 以下为代码: count = 0 def move(pile, src, tmp, dst): global count if ...