比赛链接

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. vue之字符串的方法

    目录 简介 indexOf方法 简介 本文会把遇到的字符串的方法慢慢补充进来 indexOf方法 indexOf方法判断字符串是否包含另一个字符串 判断结果如果包含返回的是索引,如果不包含,则返回-1 ...

  2. [大数据]Hadoop HDFS文件系统命令集

    基本格式: hadoop fs -cmd [args] 1 Query 显示命令的帮助信息 # hadoop fs -help [cmd] 查看hadoop/hdfs的用户 # hdfs dfs -l ...

  3. 四月十八日java基础知识

    1.由于每个对象的pi值都是相同的,所以没有必要让每个对象都保存有自己的pi值,因此将pi声明为静态变量,使之成为所有对象共用的存储空间,所有对象都公用pi这个变量也就是说共用的变量可以设定为静态变量 ...

  4. 研发运维双管齐下!Seal AppManager的正确打开方式

    新一代应用统一部署管理平台 Seal AppManager 采用平台工程的理念,通过降低基础设施操作的复杂度为研发和运维团队提供易用.一致的应用管理和部署体验.Seal AppManager 帮助研发 ...

  5. [bx] 和 Loop指令

    在masm编译器中不同于debug的命令 如:在debug中 mov ax,[0] --> 是说将 偏移地址为 0 中的数据送入ax中 而在汇编语言中 mov ax,[0] --> 是说将 ...

  6. 华为云 OpenTiny 跨端、跨框架企业级开源组件库项目落地实践直播即将开启!

    大家好,我是 Kagol,公众号:前端开源星球. "你们这个产品怎么只能在电脑上适配呀?我想在手机上看都不行,太麻烦了!!" "你们这个产品看起来太简单了,我想要@@功能 ...

  7. 使用Jmeter测试MQTT

    使用Jmeter测试MQTT 准备工作 JMeter本身没有MQTT的压力测试功能需要下载插件进行压力测试下载地址将下载好的mqtt-xmeter-2.0.2-jar-with-dependencie ...

  8. js与java对json的操作

    JSON呢,是现在大部分,并且主流的传递数据的方式. 今天讲一下javascript的java对json的操作 提到js,当下就有一个比较主流的插件,vue.js,这个插件程序员没用过也都听说过吧, ...

  9. P5356 [Ynoi2017] 由乃打扑克

    md调了5h才调出来恶心坏了没想到这么快就做了第二道Ynoi 据说这题其实不卡常 屠龙宝刀点击就送 题面也很清楚,给定两种操作,一种是区间加,一种是询问区间内第 k 小的数的值是多少. 对于区间加,在 ...

  10. 【STL】C++使用STL处理替换字符串操作。

    // Examples4STL.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <stdio.h> #incl ...