比赛链接

A

题解

知识点:数学。

\(4\) 位密码,由两个不同的数码组成,一共有 \(C_4^2\) 种方案。从 \(10-n\) 个数字选两个,有 \(C_{10-n}^2\) 种方案。结果为 \(3(10-n)(9-n)\)。

时间复杂度 \(O(n)\)

空间复杂度 \(O(1)\)

代码

#include <bits/stdc++.h>
#define ll long long using namespace std; bool solve() {
int n;
cin >> n;
for (int i = 1;i <= n;i++) {
int x;
cin >> x;
}
cout << 3 * (10 - n) * (9 - n) << '\n';
return true;
} int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
if (!solve()) cout << -1 << '\n';
}
return 0;
}

B

题解

知识点:构造。

显然最小价值是 \(2\) 。把 \(1\) 和 \(2\) 分两端放即可。

时间复杂度 \(O(n)\)

空间复杂度 \(O(1)\)

代码

#include <bits/stdc++.h>
#define ll long long using namespace std; bool solve() {
int n;
cin >> n;
for (int i = 2;i <= n;i++) cout << i << ' ';
cout << 1 << '\n';
return true;
} int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
if (!solve()) cout << -1 << '\n';
}
return 0;
}

C

题解

知识点:贪心。

发现,可以将一段连续的盖子向左移,等效于把最后一个盖子移到第一个左边。因此,找到一个无盖且右边连续有盖的位置,从连续有盖的一段选一个最小的和无盖的位置交换,如果最小的比无盖的大那就不交换。

时间复杂度 \(O(n)\)

空间复杂度 \(O(n)\)

代码

#include <bits/stdc++.h>
#define ll long long using namespace std; int a[200007];
bool solve() {
int n;
cin >> n;
string s;
cin >> s;
s = "?" + s;
for (int i = 1;i <= n;i++) cin >> a[i]; for (int i = 1;i <= n - 1;i++) {
if (s[i] == '0' && s[i + 1] == '1') {
int mi = i;
for (int j = i + 1;j <= n && s[j] == '1';j++) {
if (a[j] < a[mi]) mi = j;
}
swap(s[i], s[mi]);
}
}
int sum = 0;
for (int i = 1;i <= n;i++) {
if (s[i] == '1') sum += a[i];
}
cout << sum << '\n';
return true;
} int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
if (!solve()) cout << -1 << '\n';
}
return 0;
}

D

题解

知识点:枚举。

第一个串显然是从第一个 \(1\) 开始截取到最后,现在考虑从第一个串截取出第二个串填补第一个串的 \(0\) 。

显然要从第一个 \(0\) 开始填。注意到,只有 \(0\) 前面的 \(1\) 可以通过截取向右移动与 \(0\) 重合。枚举前面每个 \(1\) 通过截取与第一个 \(0\) 重合即可,截取方法有很多,第一种,以各个 \(1\) 为起点,截取固定长度,保持 \(1\) 与 \(0\) 重合;第二种,从第一个 \(1\) 开始截取,截取不同长度,使各个 \(1\) 和 \(0\) 重合。

发现复杂度是和从第一个 \(1\) 开始连续 \(1\) 的长度有关,但因为是随机数据,因此不可能太长,可以看作常数。

时间复杂度 \(O(n)\)

空间复杂度 \(O(n)\)

代码

#include <bits/stdc++.h>

using namespace std;

int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n;
cin >> n;
string s;
cin >> s;
int pos = s.find('1');
if (!~pos) {
cout << 0 << '\n';
return 0;
}
string s1 = s.substr(pos);
pos = s1.find('0');
string ans = s1;
for (int i = 0;i < pos;i++) {
string s2 = s1;
for (int j = pos;j < s1.size();j++) {
if (s1[j - pos + i] == '1') s2[j] = '1';
}
ans = max(ans, s2);
}
cout << ans << '\n';
return 0;
}
/* #include <bits/stdc++.h> using namespace std; string elz(string s) {
for (int i = 0;i < s.size() - 1;i++)
if (s[i] == '1') return s.substr(i);
return s.substr(s.size() - 1);
}
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n;
string s;
cin >> n >> s;
bitset<1000005> a(s), b(s);
string ans = a.to_string();
for (int i = 1;i <= 10;i++)
ans = max(ans, (a | (b >> i)).to_string());
cout << elz(ans) << '\n'; return 0;
} */

E

题解

知识点:线性dp。

考虑设 \(dp[i]\) 为造成不少于 \(i\) 点伤害的最小时间。

先考虑 \(p_1,p_2\) 单独射击的转移:

dp[i] = min(dp[max(0LL, i - (p1 - s))] + t1, dp[max(0LL, i - (p2 - s))] + t2);

再考虑,\(p_2\) 在 \(p_1\) 的 \(j\) 轮时间中调整,配合 \(p_1\) 在第 \(j\) 轮同时射击。此时 \(p_1\) 伤害是 \((j-1)(p_1 - s)\) ,\(p_2\) 伤害是 \(\lfloor\frac{(j\cdot t_1 - t_2)}{t_2}\rfloor(p_2-s)\) ,同时射击伤害 \(p_1+p_2-s\) ,共计 \((j-1)(p_1 - s) + \lfloor\frac{(j\cdot t_1 - t_2)}{t_2}\rfloor(p_2-s) + (p_1+p_2-s)\) 。(同理 \(p_1\) 配合 \(p_2\) 同时射击)。

ll dmg = (j - 1) * (p1 - s) + (t1 * j - t2) / t2 * (p2 - s) + (p1 + p2 - s);
dp[i] = min(dp[i], dp[max(0LL, i - dmg)] + t1 * j);

时间复杂度 \(O(h^2)\)

空间复杂度 \(O(h)\)

代码

#include <bits/stdc++.h>
#define ll long long using namespace std; ll dp[5007];
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int p1, p2;
ll t1, t2;
cin >> p1 >> t1;
cin >> p2 >> t2;
int h;
ll s;
cin >> h >> s;
dp[0] = 0;
for (int i = 1;i <= h;i++) {
dp[i] = min(dp[max(0LL, i - (p1 - s))] + t1, dp[max(0LL, i - (p2 - s))] + t2);
for (int j = 1;j <= i;j++) {
if (t1 * j >= t2) {
ll dmg = (j - 1) * (p1 - s) + (t1 * j - t2) / t2 * (p2 - s) + (p1 + p2 - s);
dp[i] = min(dp[i], dp[max(0LL, i - dmg)] + t1 * j);
}
if (t2 * j >= t1) {
ll dmg = (j - 1) * (p2 - s) + (t2 * j - t1) / t1 * (p1 - s) + (p1 + p2 - s);
dp[i] = min(dp[i], dp[max(0LL, i - dmg)] + t2 * j);
}
}
}
cout << dp[h] << '\n';
return 0;
}

F

题解

知识点:排列组合,枚举。

我们考虑每个点对答案的贡献,显然是从他第一次出现开始计算。

比如某点在第 \(i\) 组区间出现,那么已经进行了 \(i-2\) 次操作了,产生 \(3^{i-2}\) 个集合。在这之后还有 \(n-i+1\) 次操作,如果操作时另一个区间有这个点,那么或和且能继承这个点继续;如果没有这个点,那么或和异或能继承这个点继续,注意无论以后有没有这个点都只有两个分支能使这个点有贡献,所以共 \(2^{n-i+1}\) 个集合是有这个点的,最后总贡献是 \(3^{i-2}2^{n-i+1}\) 。

要特别注意,第一个区间出现的点,之前的操作也是 \(0\) 次 ,并且之后的操作也只有 \(n-1\) 次,最终通式是 \(3^{\max(i-2,0)}2^{\min(n-i+1,n-1)}\) 。

接下来考虑如何记录某个点第一次出现的位置 \(lst[i]\) 。朴素枚举是 \(n^2\) 的,我们需要优化。显然出现过的点之后跳过就行了,我们记录每个点能跳跃到的位置 \(nxt[i]\) 即可,对于区间 \([l,r]\) ,如果某个点 \(i\) 没出现过,那么 \(nxt[i]\) 可以更新为 \(r[i]\) ;否则出现过,我们需要跳跃,同时更新这个点以后能跳跃到的位置,\(nxt'[i] = \max(nxt[i],r),i = nxt[i]\) ,注意需要保存一下旧位置,再更新完 \(nxt[i]\) 再更新 \(i\) 。

时间复杂度 \(O(n)\)

空间复杂度 \(O(n)\)

代码

#include <bits/stdc++.h>

using namespace std;

const int mod = 998244353;

int l[300007], r[300007], lst[300007], nxt[300007];
int pow2[300007], pow3[300007];
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n;
cin >> n;
memset(lst, -1, sizeof(lst));
pow2[0] = 1;
pow3[0] = 1;
for (int i = 1;i <= n;i++) {
pow2[i] = 1LL * pow2[i - 1] * 2 % mod;
pow3[i] = 1LL * pow3[i - 1] * 3 % mod;
}
for (int i = 1;i <= n;i++) cin >> l[i] >> r[i];
for (int i = n;i >= 1;i--) {
for (int j = l[i];j <= r[i];j++) {
if (lst[j] == -1)lst[j] = i, nxt[j] = r[i];
else {
int tmp = nxt[j];
nxt[j] = max(nxt[j], r[i]);
j = tmp;
}
}
}
int ans = 0;
for (int i = 0;i <= 300000;i++) {
if (lst[i] == -1) continue;
ans = (ans + 1LL * pow3[max(lst[i] - 2, 0)] * pow2[min(n - lst[i] + 1, n - 1)]) % mod;
}
cout << ans << '\n';
return 0;
}

Educational Codeforces Round 137 (Rated for Div. 2) A-F的更多相关文章

  1. Educational Codeforces Round 61 (Rated for Div. 2) D,F题解

    D. Stressful Training 题目链接:https://codeforces.com/contest/1132/problem/D 题意: 有n台电脑,每台电脑都有初始电量ai,也有一个 ...

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

随机推荐

  1. Jittered采样类定义和测试

    抖动采样算法测试,小图形看不出什么明显区别,还是上代码和测试图吧. 类声明: #pragma once #ifndef __JITTERED_HEADER__ #define __JITTERED_H ...

  2. Point2和Point3类定义

    支持以下图中的运算 类声明: class Point2 { public: Point2(); ~Point2(); Point2(ldouble a); Point2(ldouble a, ldou ...

  3. Git 07 IDEA集成Git

    参考源 https://www.bilibili.com/video/BV1FE411P7B3?spm_id_from=333.999.0.0 版本 本文章基于 Git 2.35.1.2 IDEA 是 ...

  4. .NET异步编程模式(一)

    .NET 提供了三种异步编程模型 TAP - task-based asynchronous pattern APM - asynchronous programming model EAP - ev ...

  5. JOIOI王国 - 二分+贪心

    题面 题解 通过一句经典的话"最大值的最小值" 我判断它是二分题, 不难发现,整个图形中两个省的分界线是一条单调不递减或单调不递增的折线. 而且,越到后来它的最大值只会越来越大,最 ...

  6. java基础———注释

    注释是写给读者看的,并不会被执行! 单行注释 以 //开头 例如://注释内容              可以注释一行文本 多行注释 以/*开头     以 */结束 例如:/*注释内容*/      ...

  7. 【java】学习路径19-Math类、BigDecimal的使用

    1--Math类简单的东西 //一些常数 show(Math.PI); show(Math.E); //四舍五入 show(Math.round(3.4)); show(Math.round(3.6) ...

  8. HC32L110(三) HC32L110的GCC工具链和VSCode开发环境

    目录 HC32L110(一) HC32L110芯片介绍和Win10下的烧录 HC32L110(二) HC32L110在Ubuntu下的烧录 HC32L110(三) HC32L110的GCC工具链和VS ...

  9. k8s中ingress,service,depoyment,pod如何关联

    k8s中pod通过label标签名称来识别关联,它们的label  name一定是一样的.ingress,service,depoyment通过selector 中app:name来关联 1.查询发布 ...

  10. KingbaseES V8R6 ksql 关闭自动提交

    背景 用过oracle或mysql的人都知道,做一个dml语句,如果发现做错了,还可以rollback,但在Kingbase ksql 中,如果执行一个dml,没有先运行begin; 的话,一执行完就 ...