Codeforces Round #887 (Div. 2) A-D
A
代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int a[507];
bool solve() {
int n;
cin >> n;
for (int i = 1;i <= n;i++) cin >> a[i];
int ans = 1e9;
for (int i = 2;i <= n;i++) ans = min(ans, max(0, a[i] - a[i - 1] + 1));
cout << (ans + 1) / 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 n, k;
cin >> n >> k;
int ans = 0;
for (int i = 0;i <= n;i++) {
int a = n, b = i;
int cnt = 2;
while (cnt < k && 0 <= a - b && a - b <= b) {
cnt++;
a -= b;
swap(a, b);
}
if (cnt == k) 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;
}
C
题意
有一个数字集合 \(\{ 1,2,3,\cdots,10^{1000} \}\) 。
给定一个长为 $n $ 正整数数组 \(a\) ,每轮删除集合中排名 \(a_1,\cdots,a_n\) 的数字。
一共删 \(k\) 轮,问最后集合中最小的元素。
题解
知识点:枚举,贪心。
首先特判 \(a_1 \geq 2\) 的情况,答案就是 \(1\) 。
考虑从最后一轮逆推最后一个数的排名 \(x\) ,一开始 \(x = 1\) 。
我们考虑上一轮在 \(x\) 之前删了 \(pos\) 个数,那么 \(pos\) 应该满足 \(a_{pos} < x + pos <a_{pos+1}\) 。我们知道 \(a_{pos}\) 的递增速率是大于等于 \(pos\) 的,更具体地说,我们有 \(a_{pos} - pos < x < a_{pos+1} - pos-1+1\) ,显然 \(a_{pos}-pos\) 随着 \(pos\) 增加是不严格递增的,因此任意 \(pos\) 产生的区间都是非空的,所以我们一定能找到一个 \(pos\) 满足条件,可以考虑二分这个 \(pos\) 。
但我们注意到 \(x\) 是严格递增的,因此我们可以保留上一轮的 \(pos\) 继续递增。因此,复杂度可以优化成线性。
时间复杂度 \(O(n)\)
空间复杂度 \(O(n)\)
代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int a[200007];
bool solve() {
int n, k;
cin >> n >> k;
for (int i = 1;i <= n;i++) cin >> a[i], a[i] -= i;
if (a[1] + 1 >= 2) {
cout << 1 << '\n';
return true;
}
ll x = 1;
int pos = 0;
for (int i = 1;i <= k;i++) {
while (pos < n) {
if (a[pos] < x && x < a[pos + 1] + 1) break;
pos++;
}
x += pos;
}
cout << x << '\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\) 的数组 \(a\) ,要求构造长为 \(n\) 的数组 \(b\) ,满足对于 \(1 \leq i \leq n\) 有 \(a_i\) 个下标 \(j\) 使得 \(b_i + b_j > 0\) 。
其中 \(a_i \in [0,n]\) ,要求 \(b_i \neq 0\) 且不存在 \(b_i + b_j = 0\) 。
题解
知识点:构造。
可以考虑从绝对值最大的数字开始构造,因为这些数字确定后可以直接删除,不会影响或产生确定的影响。显然,我们有 \(a_i < a_j\) 则 \(b_i<b_j\) ,同时若绝对值最大的数字 \(b_x<0\) 则 \(a_x = 0\) 否则 \(a_x = n\) 。
我们考虑将 \(a\) 从小到大排序,每次删除按如下操作:
若 \(a_1 = 0\) 成立,则 \(b_1 = -n\) ;若 \(a_n = n\) 成立,则 \(b_n = n\) ;若前面两者同时成立或不成立,则无解。
同时成立代表,既有一个数字和任何数字加都是小于等于 \(0\) ,又有一个数字加任何数字都是大于 \(0\) 矛盾。
同时不成立代表,不存在绝对值最大的数字(因为存在就一定会有 \(a_1 = 0\) 或 \(a_n = n\) )矛盾。
删去后,令 \(n \to n-1\) 。若删去的是 \(a_n\) ,则令所有 \(a_i\) 减 \(1\) 。
若存在剩余数字,则回到步骤 \(1\) ,否则就构造完成。
注意,构造时要保证原来的顺序。
时间复杂度 \(O(n \log n)\)
空间复杂度 \(O(n)\)
代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int a[100007];
int b[100007];
bool solve() {
int n;
cin >> n;
for (int i = 1;i <= n;i++) cin >> a[i];
vector<int> ord(n + 1);
iota(ord.begin() + 1, ord.end(), 1);
sort(ord.begin() + 1, ord.end(), [&](int i, int j) {return a[i] < a[j];});
int l = 1, r = n, delta = 0;
while (l <= r) {
int ok = (a[ord[l]] - delta == 0) ^ (a[ord[r]] - delta == r - l + 1);
if (!ok) return false;
if (a[ord[l]] - delta == 0) {
b[ord[l]] = -(r - l + 1);
l++;
}
else {
b[ord[r]] = r - l + 1;
r--;
delta++;
}
}
cout << "YES" << '\n';
for (int i = 1;i <= n;i++) cout << b[i] << " \n"[i == 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 << "NO" << '\n';
}
return 0;
}
Codeforces Round #887 (Div. 2) A-D的更多相关文章
- 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 ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
- 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 ...
- 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 ...
- Codeforces Round #371 (Div. 1)
A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...
- Codeforces Round #268 (Div. 2) ABCD
CF469 Codeforces Round #268 (Div. 2) http://codeforces.com/contest/469 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...
- 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts
题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...
随机推荐
- 12-异步FIFO
1.异步FIFO的应用 跨时钟域 批量数据 传输效率高 2.异步FIFO结构 FIFO深度 - 双端口RAM设计 3.异步FIFO深度计算 4.异步FIFO读写地址的编码 5.异步FIFO读写时钟域的 ...
- 3. Oracle数据库异常关闭,导致错误3. Oracle数据库异常关闭,导致错误ERROR: ORA-01034: ORACLE ngt available; ORA-27101: shared memory realm does not exist
之前由于电脑没电,强制关机,导致Oracle数据库异常关闭,再次启动电脑登陆数据库时,发生以下错误: 当我尝试重新启动数据库时,发生错误: 经过查阅资料后得知:缺少INITXE.ORA文件,需要从下图 ...
- [转帖]京东大佬细说:Nginx反向代理时保持长连接,看完直呼"学到了!"
https://mp.weixin.qq.com/s?__biz=MzU1MzE2NzIzMg==&mid=2247488405&idx=1&sn=7081ff4e0ac1de ...
- SQLServer数据库JDBC连接串参数的简单学习
SQLServer数据库JDBC连接串参数的简单学习 背景 前段时间一直跟同事一起处理SQLServer 比其他数据库的deadlock更多的问题. 涉及到了几个驱动的参数. 想着问题基本上告一段落, ...
- [转帖]005、体系结构之TiKV_Raft日志
Raft日志 1.Raft与Multi Raft 2.Raft 日志复制 2.1.复制流程总览 2.2.Propose 2.3.Append 2.3.Replicate(Append) 2.4 Com ...
- [转帖]【KingbaseES】sys_dump逻辑备份工具详解
KingbaseES逻辑备份还原工具提供了数据库对象一级的联机备份还原功能,备份对象包括: 数据库 模式 表 视图 约束 权限 触发器 函数 序列 逻辑备份的输出格式包括: 二进制 SQL脚本 此外, ...
- [转帖]Linux fuse用户态文件系统及其libfuse
https://www.jianshu.com/p/abc5524ac18c 为什么要有用户态文件系统 VFS文件系统可知文件系统在内核态的,应用程序操作文件,统一调用内核态的VFS层抽象接口. 突然 ...
- [转帖]linux,wget 的证书不可信,证书使用不安全的算法签名
centos wget 的证书不可信,证书使用不安全的算法签名 wget wget https://www.php.net/distributions/php-7.4.28.tar.gz 出现错误: ...
- SingleNode HDFS 搭建过程
背景 1. 纯粹测试 2. 未考虑安全和授权以及数据处理. 3. 单节点最简单的部署, 验证功能连接的可能性 资料获取以及环境变量的设置 获取最新的安装文件 https://downloads.apa ...
- 可插拔组件设计机制—SPI
作者:京东物流 孔祥东 1.SPI 是什么? SPI 的全称是Service Provider Interface,即提供服务接口:是一种服务发现机制,SPI 的本质是将接口实现类的全限定名配置在文件 ...