比赛链接

A

题解

知识点:贪心,模拟。

遇到没用过的数字就给个字母,遇到用过的数字就对照字母是否一致。

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

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

代码

#include <bits/stdc++.h>
#define ll long long using namespace std; int a[57];
bool solve() {
int n;
cin >> n;
for (int i = 1;i <= n;i++) cin >> a[i];
string s;
cin >> s;
s = "?" + s;
map<int, char> vis;
for (int i = 1;i <= n;i++) {
if (!vis.count(a[i])) vis[a[i]] = s[i] - 'a';
if (vis[a[i]] != s[i] - 'a') return false;
}
cout << "YES" << '\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;
}

B

题解

知识点:数学,模拟。

记录奇数数量,每次模拟一下变化即可。

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

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

代码

#include <bits/stdc++.h>
#define ll long long using namespace std; bool solve() {
int n, q;
cin >> n >> q;
ll sum = 0;
int cnt1 = 0;
for (int i = 1;i <= n;i++) {
int x;
cin >> x;
sum += x;
if (x & 1) cnt1++;
}
while (q--) {
int op, x;
cin >> op >> x;
if (op == 0) {
sum += 1LL * (n - cnt1) * x;
if (x & 1) cnt1 = n;
}
else {
sum += 1LL * cnt1 * x;
if (x & 1) cnt1 = 0;
}
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;
}

C

题解

知识点:枚举,模拟。

\(last\) 存当前位置右边第一个绿灯。把 \(s\) 复制一遍到后面,方便最后一个绿灯的后面一段也能被算到。

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

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

代码

#include <bits/stdc++.h>
#define ll long long using namespace std; bool solve() {
int n;
char ch;
cin >> n >> ch;
string s;
cin >> s;
s = "?" + s + s;
int last = 0;
int ans = 0;
for (int i = 2 * n;i >= 1;i--) {
if (s[i] == 'g') last = i;
else if (s[i] == ch) ans = max(ans, last - i);
}
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;
}

D

题解

知识点:数论,贪心。

先把原来数字的 \(2\) 因子个数加到 \(sum\) 。然后再把 \([1,n]\) 的每个数的 \(2\) 因子存起来,贪心地从大到小拿,这样次数最小,直到 \(sum\) 超过 \(n\) 或取完所有数字。最后小于 \(n\) 则 \(-1\) ,否则输出操作次数。

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

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

代码

#include <bits/stdc++.h>
#define ll long long using namespace std; bool solve() {
int n;
cin >> n;
int sum = 0;
vector<int> tb2;
for (int i = 1;i <= n;i++) {
int x;
cin >> x;
while (x % 2 == 0) sum++, x /= 2;
x = i;
int idx = 0;
while (x % 2 == 0) idx++, x /= 2;
if (idx) tb2.push_back(idx);
}
sort(tb2.begin(), tb2.end(), [&](int a, int b) {return a > b;});
int cnt = 0;
for (auto x : tb2) {
if (sum >= n) break;
sum += x;
cnt++;
}
if (sum < n) return false;
else cout << cnt << '\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

题解

知识点:dfs,数论,质因数分解。

题目要求找到 \(x \in (a,c],y \in (b,d]\) 满足 \(a\cdot b | x \cdot y\) 。

显然 \(x,y\) 需要分摊到 \(a\cdot b\) 的所有质因子,即 \(x,y\) 一定是 \(a\cdot b\) 两个成对因子 \(f_1,f_2\) 的倍数。

注意到 \(10^{18}\) 内的数,因子数最多有 \(103680\) 个 ;\(10^9\) 内的数,因子数最多有 \(1344\) 个。因此,我们不妨先枚举 \(x\) 可能分摊到的因子 \(f_1(f_1 \leq c)\) ,同时可以求出另一个因子 \(f_2(f_2\leq d)\),最后将他们分别加倍到比 \(a\) 和 \(b\) 大,最终检验一下是否还在区间即可。

时间复杂度 \(O(10^5)\)

空间复杂度 \(O(10^5)\)

代码

#include <bits/stdc++.h>
#define ll long long using namespace std; int cnt;
bool vis[100007];
int prime[100007];
void euler_screen(int n) {
for (int i = 2;i <= n;i++) {
if (!vis[i]) prime[++cnt] = i;
for (int j = 1;j <= cnt && i * prime[j] <= n;j++) {
vis[i * prime[j]] = 1;
if (i % prime[j] == 0) break;
}
}
} int a, b, c, d;
vector<pair<int, int>> fd;
ll val;
vector<int> af; void dfs(int step, ll mul) {
if (mul > c) return;
if (step == fd.size()) {
if (val / mul <= d) af.push_back(mul);
return;
}
for (int i = 0;i <= fd[step].second;i++) {
dfs(step + 1, mul);
mul *= fd[step].first;
}
} bool solve() {
cin >> a >> b >> c >> d;
map<int, int> mp;
int tt = a;
for (int i = 1;prime[i] * prime[i] <= tt;i++) {
while (tt % prime[i] == 0) mp[prime[i]]++, tt /= prime[i];
}
if (tt > 1) mp[tt]++;
tt = b;
for (int i = 1;prime[i] * prime[i] <= tt;i++) {
while (tt % prime[i] == 0) mp[prime[i]]++, tt /= prime[i];
}
if (tt > 1) mp[tt]++;
fd.clear();
for (auto [x, y] : mp) fd.push_back({ x,y });
af.clear();
val = 1LL * a * b;
dfs(0, 1);
int ansx = -1, ansy = -1;
for (auto x : af) {
int y = val / x;
x = (a / x + 1) * x;
y = (b / y + 1) * y;
if (x <= c && y <= d) {
ansx = x;
ansy = y;
break;
}
}
cout << ansx << ' ' << ansy << '\n';
return 1;
}
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
euler_screen(100001);
while (t--) {
if (!solve()) cout << -1 << '\n';
}
return 0;
}

F

题解

知识点:枚举,双指针,数学。

尝试从小到大枚举 \(x \in [1,n]\) ,计算满足 \(med(l,r) < mex(l,r) = x\) 的段的个数( \(x = 0\) 显然没有满足的)。

首先,对于一段 \([l,r]\) 满足 \(mex(l,r) = x\) 一定包括了 \([0,x-1]\) 的数字。因此,当且仅当段长度 \(r-l+1 \leq 2x\) 才满足中位数 \(med(l,r)<mex(l,r) = x\) ,否则必然包括 \(>x\) 的数字。

假设要求 \(mex(l,r) = x\) ,先得到满足 \(mex(l,r) = x\) 的最小段 \([l,r]\) ,随后找到 \(x+1\) 的位置 \(pos\) (假设 \(pos\) 在 \([l,r]\) 外,在里面就不存在这个 \(mex\) 了qwq)。

不妨设 \(pos>r\) ,只要不包括 \(pos\) 这个位置,其他从 \([l,r]\) 扩展的段的 \(mex\) 一定是 \(x\) ,因此可以枚举 \([r,pos)\) 的每个位置作为右端点 \(i\), 找到最远左端点 \(j = \max(1,i-2x+1)\) ,随后每次可以得到 \(\max(0,l-i+1)\) 个合法段。同理可以求 \(pos<l\) 的情况。

我们可以从 \(mex(l,r) = 1\) 开始枚举,显然 \(l = pos[0],r = pos[0]\) 。

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

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

代码

#include <bits/stdc++.h>
#define ll long long using namespace std; int p[200007], pos[200007];
bool solve() {
int n;
cin >> n;
for (int i = 0;i <= n;i++) pos[i] = 0;
for (int i = 1;i <= n;i++) cin >> p[i], pos[p[i]] = i;
int l = pos[0], r = pos[0];
ll ans = 0;
for (int x = 1;x <= n;x++) {
if (l <= pos[x] && pos[x] <= r) continue;//目标mex被之前的mex段包括了,说明这个mex不会出现
int len = 2 * x;//一个mex>med段的最长长度是2mex
if (pos[x] > r) {//段mex在右侧
for (int i = r;i < pos[x];i++) {
int j = max(1, i - len + 1);//长度限制内,左端点可以到哪
ans += max(0, l - j + 1);
}
}
else {
for (int i = l;i > pos[x];i--) {
int j = min(n, i + len - 1);
ans += max(0, j - r + 1);
}
}
l = min(pos[x], l);
r = max(pos[x], r);
}
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 #828 (Div. 3) A-F的更多相关文章

  1. Codeforces Round #573 (Div. 1) 差F

    Codeforces Round #573 (Div. 1) E 题意:二维平面上有 n 个点,你可以放至多 m 条直线使得 (0,0) 与每个点的连线至少与一条直线相交.求原点与所有直线的距离最小值 ...

  2. Codeforces Round #541 (Div. 2) (A~F)

    目录 Codeforces 1131 A.Sea Battle B.Draw! C.Birthday D.Gourmet choice(拓扑排序) E.String Multiplication(思路 ...

  3. Codeforces Round #532 (Div. 2):F. Ivan and Burgers(贪心+异或基)

    F. Ivan and Burgers 题目链接:https://codeforces.com/contest/1100/problem/F 题意: 给出n个数,然后有多个询问,每次回答询问所给出的区 ...

  4. Codeforces Round #600 (Div. 2)E F

    题:https://codeforces.com/contest/1253/problem/E 题意:给定n个信号源,俩个参数x和s,x代表这个信号源的位置,s代表这个信号源的波及长度,即这个信号源可 ...

  5. Codeforces Round #346 (Div. 2) E F

    因为很久没有个人认真做题了 昨天晚上开了场虚拟cf来锻炼个人手速 选的是第一次做cf的场 那时候7出3还被hack...之后也没补题 这次做的时候顺便回忆了一下以前比赛的时候是怎么想的 发现经验还是很 ...

  6. Codeforces Round #322 (Div. 2) E F

    E. Kojiro and Furrari 题意说的是 在一条直线上 有n个加油站, 每加一单位体积的汽油 可以走1km 然后每个加油站只有一种类型的汽油,汽油的种类有3种 求从起点出发到达终点要求使 ...

  7. Codeforces Round #506 (Div. 3) 1029 F. Multicolored Markers

    CF-1029F 题意: a,b个小正方形构造一个矩形,大小为(a+b),并且要求其中要么a个小正方形是矩形,要么b个小正方形是矩形. 思路: 之前在想要分a,b是否为奇数讨论,后来发现根本不需要.只 ...

  8. Codeforces Round #828 (Div. 3) E2. Divisible Numbers (分解质因子,dfs判断x,y)

    题目链接 题目大意 给定a,b,c,d四个数,其中a<c,b<c,现在让你寻找一对数(x,y),满足一下条件: 1. a<x<c,b<y<d 2. (x*y)%(a ...

  9. Codeforces Round #485 (Div. 2) F. AND Graph

    Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...

随机推荐

  1. 重看Java教学视频时的查漏补缺

    数据类型 1.基本数据类型:四类八种. 2.数据范围与字节数不一定相关.如float为4字节表示范围比long的8字节要大. 3.浮点数默认double类型,如要用float,需加F. 4.boole ...

  2. Configuration的学习

    创建 //1.创建,调用的空惨 Configuration conf = new Configuration(); 加载主配置 //2.读取主配置文件==>如果是空参方法则自动加载sec下的re ...

  3. 第六十五篇:Vue的过滤器

    好家伙, 过滤器,vue3取消了,只有vue2能用 1.过滤器 过滤器(Filters)是vue为开发者提供的功能,常用于文本的格式化. 过滤器可以用在两个地方:插值表达式和v-bind属性绑定. 过 ...

  4. openstack中Nova组件简解

    一.Nova组件概述 计算节点通过Nova Computer进行虚拟机创建,通过libvirt调用kvm创建虚拟机,nova之间通信通过rabbitMQ队列进行通信. Nova位于Openstack架 ...

  5. KingbaseES lag 和 lead 函数

    1.简介 lag与lead函数是跟偏移量相关的两个分析函数,通过这两个函数可以在一次查询中取出同一字段的前N行的数据(lag)和后N行的数据(lead)作为独立的列,从而更方便地进行进行数据过滤. 2 ...

  6. 全网最简单的大文件上传与下载代码实现(React+Go)

    前言 前段时间我需要实现大文件上传的需求,在网上查找了很多资料,并且也发现已经有很多优秀的博客讲了大文件上传下载这个功能. 我的项目是个比较简单的项目,并没有采用特别复杂的实现方式,所以我这篇文章的目 ...

  7. Taurus.MVC-Java 版本打包上传到Maven中央仓库(详细过程):5、Maven版本发布与后续版本更新(大结局)

    文章目录: Taurus.MVC-Java 版本打包上传到Maven中央仓库(详细过程):1.JIRA账号注册 Taurus.MVC-Java 版本打包上传到Maven中央仓库(详细过程):2.PGP ...

  8. 使用 Elastic 技术栈构建 K8S 全栈监控 -3: 使用 Filebeat 采集 Kubernetes 集群日志

    文章转载自:https://www.qikqiak.com/post/k8s-monitor-use-elastic-stack-3/ 操作步骤 filebeat连接es使用上一步创建的secret: ...

  9. Beats在Kibana中的集中管理

    前提条件: 1.es版本是白金版 2.es开启安全设置,kibana访问es需要密码 操作步骤汇总: 1-3步是基础环境配置 4-9步是注册beats到集中管理平台,然后启动beats,只是单纯启动b ...

  10. Linux 上安装 PostgreSQL

    打开 PostgreSQL 官网 https://www.postgresql.org/,点击菜单栏上的 Download ,可以看到这里包含了很多平台的安装包,包括 Linux.Windows.Ma ...