Educational Codeforces Round 142 (Rated for Div. 2) A-D
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 rst = n - cnt / 2 * 2;
cout << rst + cnt / 2 << '\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() {
int a, b, c, d;
cin >> a >> b >> c >> d;
if (a == 0) {
cout << min(b + c + d, 1) << '\n';
return 1;
}
else {
int ans = a + 2 * min(b, c) + min(a, abs(b - c) + d) + (abs(b - c) + d > a);
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
题意
给一个长为 \(n\) 的排列,每次操作可以任选两个数,其中小的挪到开头,大的挪到末尾,问最少几次操作可以使得排列有序。
题解
知识点:贪心,枚举,双指针。
注意到,操作不影响没有被操作过的数字的相对位置,因此考虑排列中不需要操作的数字。显然,最终被保留的数字应该是连续上升的一个子序列,如 23456 是,而 13456 不是因为 \(1\) 和 \(3\) 中间没有 \(2\) 。
假设我们操作了某一组数 \((x,y)\) ,那么 \((x,y),(x-1,y+1),\cdots ,(1,n)\) 一定都需要操作一遍才能保证这些数字有序。因此只有中间的数我们不需要操作,所以我们保留的数字应该从中间开始往外拓展。
若 \(n\) 为奇数,则从中点 \(\dfrac{1+n}{2}\) 开始往两边扩展;若 \(n\) 为偶数,先保证 \(\left\lfloor \dfrac{1+n}{2} \right\rfloor ,\left\lceil \dfrac{1+n}{2} \right\rceil\) 有序,再从这两个数两边扩展,如果不有序直接输出 \(\dfrac{n}{2}\) 。
为了方便找到某个数的位置,我们可以先处理数到位置的映射 \(pos\) ,再利用双指针 \(l,r\) 指向扩展的边界,向两边同时扩展,如果有一边扩展不了那就不需要继续了,最后结果是 \(l-1\) 。
代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int pos[200007];
bool solve() {
int n;
cin >> n;
for (int i = 1;i <= n;i++) {
int x;
cin >> x;
pos[x] = i;
}
int l = (1 + n) / 2, r = (1 + n + 1) / 2;
if (pos[l] > pos[r]) {
cout << n / 2 << '\n';
return 1;
}
while (1 < l && r < n) {
if (pos[l - 1] > pos[l] || pos[r] > pos[r + 1]) break;
l--;
r++;
}
cout << l - 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;
}
D
题意
给你 \(n\) 个长为 \(m\) 的排列 \(a_i\) 。
定义一个排列 \(p\) 的值为满足 \(p_i = i,i \in[1,k]\) 中 \(k\) 的最大值。
定义两个排列 \(p,q\) 的乘法 \(p \cdot q\) 为 \(r_i = q_{p_i}\) 。
对于给定的 \(n\) 个排列中,对于每个排列 \(a_i\) 找到另一个排列 \(a_j\) ( \(j\) 可以等于 \(i\) )使得 \(a_i \cdot a_j\) 的值最大,求出这 \(n\) 个最大值。
题解
知识点:枚举。
先考虑两个排列 \(p,q\) 乘积的求值过程,即如何求出 \(q_{p_1} = 1,\cdots,q_{p_k} = k\) 中 \(k\) 最大值。
显然 \(q_{p_1} = 1\) ,即 \(q\) 中 \(1\) 的位置是 \(p_1\) ,我们就能得到 \(k\) 至少是 \(1\) ,以此类推直到 \(q_{p_i} \neq i\) 就能得到 \(k = i-1\) ,复杂度是 \(O(n^2m^2)\) ,先考虑先优化枚举过程。
既然我们要知道某个数的位置,那么我们可以先预处理出 \(q\) 所有数字出现的位置 \(pos\) 。我们发现 \(q_{p_i} = i\) 等价于 \(pos_i = p_i\) ,即 \(i\) 出现的位置等于 \(p_i\) 那么自然可以得到 \(q_{p_i} = i\) ,由此我们从 \(pos_1 = p_1\) 开始找到最大的 \(k\) 满足 \(pos_k = p_k\) 即可。现在复杂度是 \(O(n^2m)\) ,考虑优化 \(n\) 次查找。
我们发现查找的过程,其实就是一个 \(p\) 和 \(n\) 个 \(pos\) 匹配最长前缀的过程,可以用字典树 trie 解决,复杂度是 \(O(nm)\) 。但这里 \(m\) 不大(其实是我不会字典树),我们可以将排列用十进制压缩成一个整数,用 map 记录 \(n\) 个排列的前缀信息来解决。设 \(mp_i\) 为 \(n\) 个排列的 \(pos\) 前 \(i\) 个数的前缀信息,例如排列 \(pos = [3,1,4,2]\) 前三个数字的信息就是 \(314\) ,记录在 \(mp_3\) 中。例如,我们查找 \(p = [4,3,2,1]\) 前 \(2\) 个数的匹配信息时,只要判断 \(mp_2\) 中有无 \(43\) 即可。到此为止,我们对 \(p\) 从前 \(1\) 个数依次查找,最多查找 \(m\) 次就可以找到最大的 \(k\) 了,复杂度是 \(O(nm\log n)\) 。
时间复杂度 \(O(nm \log n)\)
空间复杂度 \(O(nm)\)
代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int a[50007][11];
int pos[11];
map<ll, int> mp[11];
bool solve() {
int n, m;
cin >> n >> m;
for (int i = 1;i <= m;i++) mp[i].clear();
for (int i = 1;i <= n;i++) {
for (int j = 1;j <= m;j++) {
cin >> a[i][j];
pos[a[i][j]] = j;
}
ll _t = 0;
for (int j = 1;j <= m;j++) {
_t = _t * 10 + pos[j] - 1;
mp[j][_t] = 1;
}
}
for (int i = 1;i <= n;i++) {
ll _t = 0;
int ans = m;
for (int j = 1;j <= m;j++) {
_t = _t * 10 + a[i][j] - 1;
if (!mp[j][_t]) {
ans = j - 1;
break;
}
}
cout << ans << ' ';
}
cout << '\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;
}
Educational Codeforces Round 142 (Rated for Div. 2) A-D的更多相关文章
- 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 ...
- Educational Codeforces Round 60 (Rated for Div. 2) 题解
Educational Codeforces Round 60 (Rated for Div. 2) 题目链接:https://codeforces.com/contest/1117 A. Best ...
随机推荐
- 记一次 .NET 某医疗器械 程序崩溃分析
一:背景 1.讲故事 前段时间有位朋友在微信上找到我,说他的程序偶发性崩溃,让我帮忙看下怎么回事,上面给的压力比较大,对于这种偶发性崩溃,比较好的办法就是利用 AEDebug 在程序崩溃的时候自动抽一 ...
- SpringBoot报错: No identifier specified for entity: XXX.XXX.XXX.XXX
今天练习的时候报错说是 : 没有为实体指定标识符 仔细看了实体类才发现忘记写了一些注解 用JPA写实体类时一些注解是必须的 @entity 标名本类是实体类 @table(name="表名 ...
- pta第一次博客
目录 pta第一次博客 1.前言 2.设计与分析 第二次作业第二题 第三次作业第一题 第三次作业第二题 第三次作业第三题 3.踩坑心得: 4.改进建议 5.总结 pta第一次博客 1.前言 这三次pt ...
- CentOS7虚拟机配置git仓库(配置虚拟机,网络,git仓库,windows端git访问)
想要达成的目的:从windows使用git访问CentOS7服务器上搭建的git仓库 用到的软件: (1)VMware-workstation-full-15.5.0-14665864.exe (2) ...
- 分布式ID生成方案总结整理
目录 1.为什么需要分布式ID? 2.业务系统对分布式ID有什么要求? 3.分布式ID生成方案 3.1 UUID 3.2.数据库自增 3.3.号段模式 3.4. Redis实现 3.4. 雪花算法(S ...
- 使用idea创建第一个Mybatis程序及可能遇到的问题
第一个Mybatis程序 思路:搭建环境->导入Mybatis->编写代码->执行 搭建环境 创建数据库 CREATE DATABASE `mybatis` USE `mybatis ...
- Latex数学公式学习
要想博客写的更详细,更好,那么具体详细的数学推导这一部分是少不了的,不仅要好看还要方便输入那些更为复杂的符号,因此学习Latex就是必不可少的啦,说不定过几天就要用嘞! 本篇文章参考自超详细 LaTe ...
- MSP430中断小实验——通过按键改变小灯闪烁频率
本小实验基于MSP430f5529,不同的型号可能管脚和中断配置有所不同. 实现的功能为: 第一次按下按键后,系统以每 2 秒钟,指示灯暗 1 秒,亮 1 秒的方式闪烁.程序采用默认时钟配置: 第二次 ...
- 简单的sql注入3
仍然 1 1' 1" 发现1'报错了.....我觉得作者对'情有独钟 再试试 1# 1'# 1"# 发现都可以正常登录 试试1' and '1'='1和1' and '1'='2发 ...
- 【每日一题】【DFS和回溯的区别】【BFS】104. 二叉树的最大深度-211227/220218
给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例:给定二叉树 [3,9,20,null,null,15,7], ...