比赛链接

A

代码

#include <bits/stdc++.h>
using namespace std;
using ll = long long; bool solve() {
int n;
cin >> n;
int cnt = 0;
for (int i = 1;i <= n;i++) {
int x;
cin >> x;
if (x == -1) cnt++;
}
int diff = max(0, (2 * cnt - n + 1) / 2);
cout << diff + ((cnt - diff) % 2 == 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;
}

B

代码

#include <bits/stdc++.h>
using namespace std;
using ll = long long; bool solve() {
string a, b;
cin >> a >> b;
int n = max(a.size(), b.size());
a = "?" + string(n - a.size(), '0') + a;
b = "?" + b;
int ans = 0;
for (int i = 1;i <= n;i++) {
if (a[i] != b[i]) {
ans += b[i] - a[i] + 9 * (n - i);
break;
}
}
cout << ans << '\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

题目

给两个串 \(S,T\) ,AB轮流操作,A先操作。

A:选一个串,选择其中一个位置,修改成任意想要的字符。

B:选择一个串,反转他。

问最少操作几次,使得 \(S=T\) 。

题解

知识点:贪心。

显然,B不会修改字符,并且操作两次等于没操作。

因此,关键在于A的两种情况的操作次数:

  1. 把两个字符串的对应位置修改成一样。
  2. 反转其中一个后,把两个字符串的对应位置修改成一样。

这两种情况,对B的要求:

  1. 前者需要B操作偶数次,因此若A的操作次数是奇数,那么B要减一次操作。
  2. 后者需要B操作奇数次,因此若A的操作次数是偶数,那么B要减一次操作。注意这种A可能操作 \(0\) 次,此时通过前面计算得出的是 \(-1\) 是不合法的,为了方便我们对前面的结果与 \(2\) 取最大值即可。

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

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

代码

#include <bits/stdc++.h>
using namespace std;
using ll = long long; bool solve() {
int n;
cin >> n;
string s, t;
cin >> s >> t;
s = "?" + s;
t = "?" + t;
int cnt1 = 0, cnt2 = 0;
for (int i = 1;i <= n;i++) {
cnt1 += s[i] == t[i];
cnt2 += s[i] == t[n - i + 1];
}
int ans1 = 2 * (n - cnt1) - ((n - cnt1) & 1);
int ans2 = max(2, 2 * (n - cnt2) - (!((n - cnt2) & 1)));
cout << min(ans1, ans2) << '\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,m]\) 上给定 \(n\) 个区间 \([l_i,r_i]\) 。

可以选择 \([1,m]\) 中若干个不同数字,那么每个区间的值为区间出现的所选数字个数减去剩余的所选数字个数。

问如何选择数字,使得选择后区间值的最大值与最小值的差值最大。

题解

知识点:枚举,贪心。

考虑枚举每个区间作为最大值,对于一个区间 \(A\) 作为最大值,即选择其中所有数字。此时,对于其他任意一个区间 \(B\) ,\(A\) 与 \(B\) 的区间值的差值为 \(|A| - (|A \cap B|- (|A|-|A \cap B|)) = 2(|A| - |A \cap B|)\) ,等价于我们要选择一个 \(B\) ,最大化 \(A\) 不在 \(B\) 中的部分,考虑三种情况以及对应的方案:

  1. \(B\) 在 \(A\) 中或 \(A\) 在 \(B\) 中,我们要找到最短的 \(|B|_{min}\) ,则差值最大值为 \(|A| - |B|\) 。
  2. \(B\) 与 \(A\) 的左侧相交,我们要找到最小的右端点 \(minR\) ,则差值最大值为 \(r_i - minR\)。
  3. \(B\) 与 \(A\) 的右侧相交,我们要找到最大的左端点 \(maxL\),则差值最大值 \(maxL - l_i\) 。

更进一步,我们可以得到,对于一个 \(B\) ,无论 \(B\) 属于上面哪一种情况,三种方案只会有一个会得到最大值。因此, \(A\) 与 \(B\) 的关系并不重要,我们不需要每次对一个特定的 \(A\) 将所有的 \(B\) 按三种情况分类后分别求解,而是可以无视 \(A\) 是什么,直接对所有 \(B\) 采取三种方案,其中总会有一个正确的最大值。

既然如此,那么我们事先得到所有区间的最短长度、最小右端点、最大左端点,随后枚举每个区间作为最大值区间 \(A\) ,直接使用它们计算答案取最大值即可,如此会方便很多。

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

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

代码

#include <bits/stdc++.h>
using namespace std;
using ll = long long; int L[100007], R[100007];
bool solve() {
int n, m;
cin >> n >> m;
int minlen = 2e9, maxlen = 0;
for (int i = 1;i <= n;i++) {
cin >> L[i] >> R[i];
minlen = min(minlen, R[i] - L[i] + 1);
maxlen = max(maxlen, R[i] - L[i] + 1);
};
int maxL = *max_element(L + 1, L + n + 1);
int minR = *min_element(R + 1, R + n + 1);
int ans = maxlen - minlen;
for (int i = 1;i <= n;i++) ans = max(ans, min(R[i] - L[i] + 1, max(maxL - L[i], R[i] - minR)));
cout << 2 * ans << '\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;
}

E

题目

给一个有 \(n\) 个数的数组 \(a\) ,问最小的 \(x\) 使得 \(x\) 不等于 \(a\) 任意子区间的 \(\text{lcm}\) 。

题解

知识点:GCD和LCM,线性dp,枚举。

问题等价于找到全部子区间LCM的MEX,即第一个未出现的数字。

全部子区间的LCM种类不会超过 \(n^2\) 个,因此其MEX大小不会超过 \(n^2\) ,我们只需要求出不大于 \(n^2\) 的LCM即可,超过的 \(n^2\) 的LCM可以断定是无效的。

我们考虑固定子区间右端点,对于任意左端点,设子区间产生不同的LCM为 \(x_1 < \cdots < x_k\) 。同时,因为是固定了右端点,因此大的子区间的LCM一定是小的子区间的LCM的倍数,有不等式 \(x_i \geq 2x_{i-1}, i = 2,3,\cdots,k\) ,于是我们有 \(n^2 \geq x_k \geq 2^{k-1}\) ,所以 \(k\leq 1+ 2\log_2n\) 。

通过上述方法,我们可以得到每次迭代中,枚举的LCM不超过 \(1+ 2\log_2n\) ,总的LCM不超过 \(n(1+2\log_2n)\) 。因此,我们这个方法得到全部有效的LCM的复杂度是 \(O(n \log n)\) 的。当然,我们需要用 set 维护不同种类,所以最终复杂度是 \(O(n \log^2 n)\) 。

更进一步地,LCM总种类数 \(n(1+2\log_2n)\) 又可以反过来推断出MEX大小的不会超过 \(n(1+2\log_2n)\) ,这个大小在这道题不超过 \(2 \times 10^7\) ,因此可以用一个 int 范围的数限制大小。

时间复杂度 \(O(n \log^2 n)\)

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

代码

#include <bits/stdc++.h>
using namespace std;
using ll = long long; int a[300007];
bool solve() {
int n;
cin >> n;
for (int i = 1;i <= n;i++) cin >> a[i]; set<int> st, pre;
for (int i = 1;i <= n;i++) {
set<int> now;
now.insert(a[i]);
for (auto x : pre) {
ll y = lcm((ll)x, a[i]);
if (y <= 2e6) now.insert(y);
}
for (auto x : now) st.insert(x);
swap(pre, now);
}
int ans = 1;
while (st.count(ans)) ans++;
cout << ans << '\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;
}

Codeforces Round #879 (Div. 2) A-E的更多相关文章

  1. Codeforces Round #879 (Div. 2) C. Short Program

    题目链接:http://codeforces.com/contest/879/problem/C C. Short Program time limit per test2 seconds memor ...

  2. Codeforces Round #443 (Div. 2) 【A、B、C、D】

    Codeforces Round #443 (Div. 2) codeforces 879 A. Borya's Diagnosis[水题] #include<cstdio> #inclu ...

  3. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  4. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  5. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  6. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  7. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

  8. Codeforces Round #262 (Div. 2) 1003

    Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...

  9. Codeforces Round #262 (Div. 2) 1004

    Codeforces Round #262 (Div. 2) 1004 D. Little Victor and Set time limit per test 1 second memory lim ...

  10. Codeforces Round #371 (Div. 1)

    A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...

随机推荐

  1. python之爬虫二

    10正则表达式 正则表达式(regular expression)是一种字符串匹配模式或者规则,它可以用来检索.替换那些符合特定规则的文本.正则表达式几乎适用于所有编程语言,无论是前端语言 JavaS ...

  2. Survivor

    Survivor (https://codeforces.com/group/L9GOcnr1dm/contest/422378/problem/F) 血的教训 比较有意思的一个贪心题 简单翻译一下题 ...

  3. django渲染模版时比实际少了8小时?

    这是因为django的时间是UTC时间. 我们通过改配置文件将其改成本地时间 修改配置文件 # 将时间从UTC转化成当前时间 TIME_ZONE = 'Asia/Shanghai' # USE_TZ ...

  4. 微服务 - Redis缓存 · 数据结构 · 持久化 · 分布式 · 高并发

    本篇内容基于 Redis v7.0 的阐述:官网:https://redis.io/ 本篇计划用 Docker 容器辅助部署,所以需要了解点 Docker 知识:官网:https://www.dock ...

  5. prometheus node-exporter安装

    目录 prometheus node-exporter安装 包安装在linux服务器 Daemonset安装 Docker安装 helm安装 prometheus node-exporter安装 安装 ...

  6. JVM调优笔记(一)--Nacos GC引发的服务批量下线问题

    故障背景 线上批量发服务下线的告警邮件,偶发nacos连接超时.采用了spring boot admin(以下称sba)进行服务监控. 原因分析 因为sba服务是基于nacos对其它服务进行监控,所以 ...

  7. Vue项目的网络请求代理到封装详细步骤

    1.创建vue项目 vue create demo demo是项目名称 2.安装axios 进入demo里面打开终端(黑窗口),执行 npm install axios 3.进行config.js配置 ...

  8. Python Requets库学习总结

    快速开始 发送请求 >>> import requests >>> r = requests.get('https://api.github.com/events' ...

  9. boot-admin整合Liquibase实现数据库版本管理

    Liquibase 和 Flyway 是两款成熟的.优秀的.开源/商业版的数据库版本管理工具,鉴于 Flyway 的社区版本对 Oracle 数据库支持存在限制,所以 boot-admin 选择整合 ...

  10. 在vue标签代码块中定义变量

    方式一: 在标签上使用:set关键字,不管什么标签都可以 <template> <h1>test</h1> <template :set="firs ...