Educational Codeforces Round 94 (Rated for Div. 2)【ABCD】
比赛链接:https://codeforces.com/contest/1400
A. String Similarity
题意
给出一个长 $2n-1$ 的二进制串 $s$,构造一个长 $n$ 的字符串,使其与 $s$ 的每个长 $n$ 的子串至少有一处字母相等。
题解
长 $n$ 的子串恰有 $n$ 个,每个子串取一位即可。
代码
#include <bits/stdc++.h>
using namespace std; void solve() {
int n;
string s;
cin >> n >> s;
for (int i = 0; i < int(s.size()); i += 2)
cout << s[i];
cout << "\n";
} int main() {
int t;
cin >> t;
while (t--) solve();
}
B. RPG Protagonist
题意
有两个容积分别为 $p,f$ 的背包和两种物品:
- 一种有 $cnt_s$ 个,每个体积为 $s$
- 一种有 $cnt_w$ 个,每个体积为 $w$
问最多能装多少个物品。
题解
优先装体积较小的一种物品,然后枚举第一个背包所装的数量。
代码
#include <bits/stdc++.h>
using namespace std; void solve() {
int p, f;
cin >> p >> f;
int cnt_s, cnt_w;
cin >> cnt_s >> cnt_w;
int s, w;
cin >> s >> w;
if (s > w) {
swap(cnt_s, cnt_w);
swap(s, w);
}
auto func = [](int& cap, int wt, int &cnt) {
int mi = min(cnt, cap / wt);
cap -= mi * wt;
cnt -= mi;
return mi;
};
int ans = 0;
const int N = min(cnt_s, p / s);
for (int i = 0; i <= N; i++) {
int res = i;
int caps = p - i * s, capw = f;
int ns = cnt_s - i, nw = cnt_w;
res += func(caps, w, nw);
res += func(capw, s, ns);
res += func(capw, w, nw);
ans = max(ans, res);
}
cout << ans << "\n";
} int main() {
int t;
cin >> t;
while (t--) solve();
}
C. Binary String Reconstruction
题意
一个二进制串 $s$ 的衍生串 $t$ 按如下规则构造:
- 如果 $s_{i-x}=1$ 或 $s_{i+x}=1$,$t_i=1$
- 否则 $t_i=0$
给出 $t$ 和 $x$,试还原 $s$ 。
题解
根据 $t_i=0$ 确定 $s$ 中的 $0$,其余位置均为 $1$ 。
然后判断构造出的 $s$ 是否与 $t$ 矛盾即可。
代码
#include <bits/stdc++.h>
using namespace std; void solve() {
string s;
cin >> s;
int x;
cin >> x;
const int N = s.size();
string ans(N, '1');
for (int i = 0; i < N; i++) {
if (s[i] == '0') {
if (i - x >= 0) ans[i - x] = '0';
if (i + x < N) ans[i + x] = '0';
}
}
bool ok = true;
for (int i = 0; i < N; i++) {
if (s[i] == '1') {
if (i - x >= 0 and ans[i - x] == '1') continue;
if (i + x < N and ans[i + x] == '1') continue;
ok = false;
}
}
cout << (ok ? ans : "-1") << "\n";
} int main() {
int t;
cin >> t;
while (t--) solve();
}
D. Zigzags
题意
给出一个大小为 $n$ 的数组 $a$,找出满足:
- $1 \le i < j < k < l \le n$
- $a_i=a_k$ and $a_j=a_l$
的四元组 $(i,j,k,l)$ 的数目。
题解
移项得:$(a_i,a_j)=(a_k,a_l)$ 。
即计算数组中相同的二元组的数目。
Tips
因为 $n^2$ 将近 $10^7$,所以使用 map 的 $O(n^2log_n)$ 会超时,可以用二维散列代替。
代码
#include <bits/stdc++.h>
using namespace std; void solve() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
--a[i];
}
long long ans = 0;
vector<int> cnt(n * n);
for (int j = 0; j < n; j++) {
for (int i = j - 1; i >= 0; i--) {
++cnt[a[i] * n + a[j]];
}
int k = j + 1;
for (int l = k + 1; l < n; l++)
ans += cnt[a[k] * n + a[l]];
}
cout << ans << "\n";
} int main() {
int t;
cin >> t;
while (t--) solve();
}
Educational Codeforces Round 94 (Rated for Div. 2)【ABCD】的更多相关文章
- Educational Codeforces Round 97 (Rated for Div. 2)【ABCD】
比赛链接:https://codeforces.com/contest/1437 A. Marketing Scheme 题解 令 \(l = \frac{a}{2}\),那么如果 \(r < ...
- Educational Codeforces Round 94 (Rated for Div. 2) String Similarity、RPG Protagonist、Binary String Reconstruction、Zigzags 思维
题目链接:String Similarity 题意: 首先题目定义了两个串的相似(串的构成是0.1),如果两个串存在对于一个下标k,它们的值一样,那么这两个串就相似 然后题目给你一个长度为2n-1的串 ...
- Educational Codeforces Round 94 (Rated for Div. 2) D. Zigzags (枚举,前缀和)
题意:有一长度为\(n(4\le n\le 3000)\)的数组,选择四个位置\((i,j,k,l)\ (1\le i<j<k\le n)\),使得\(a_i=a_k\)并且\(a_j=a ...
- Educational Codeforces Round 94 (Rated for Div. 2) C. Binary String Reconstruction (构造)
题意:给你一个字符串\(s\),原字符串为\(w\),如果\(i>x\)且\(w_{i-x}=1\),那么\(s_{i}=1\),如果\(i+x\le n\)且\(w_{i+x}=1\),那么\ ...
- Educational Codeforces Round 94 (Rated for Div. 2) B. RPG Protagonist (数学)
题意:你和你的随从去偷剑和战斧,你可以最多可以拿\(p\)重的东西,随从可以拿\(f\)重的东西,总共有\(cnt_{s}\)把剑,\(cnt_{w}\)把战斧,每把剑重\(s\),战斧重\(w\), ...
- Educational Codeforces Round 94 (Rated for Div. 2) A. String Similarity (构造水题)
题意:给你一个长度为\(2*n-1\)的字符串\(s\),让你构造一个长度为\(n\)的字符串,使得构造的字符串中有相同位置的字符等于\(s[1..n],s[2..n+1],...,s[n,2n-1] ...
- Educational Codeforces Round 74 (Rated for Div. 2)【A,B,C【贪心】,D【正难则反的思想】】
A. Prime Subtractiontime limit per test2 secondsmemory limit per test256 megabytesinputstandard inpu ...
- 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 ...
随机推荐
- 为Github仓库添加Github Actions实现持续集成: Android apk自动编译发布以及github pages同步推送coding.net
内容转载自我的博客 目录 说明 1. 编写Android项目的CI配置文件 2. 编写Jekyll项目的CI配置文件 2.1 配置coding.net 2.2 配置github 2.3 自动部署到co ...
- 深入浅出Dotnet Core的项目结构变化
有时候,越是基础的东西,越是有人不明白. 前几天Review一个项目的代码,发现非常基础的内容,也会有人理解出错. 今天,就着这个点,写一下Dotnet Core的主要类型的项目结构,以及之间的转 ...
- 【JavaWeb】EL 表达式
EL 表达式 简介 EL(Expression Language),即表达式语言. EL 表达式主要是代替 jsp 页面中 表达式脚本 在 jsp 页面中进行数据的输出,因为 EL 表达式在输出数据的 ...
- 剑指offer-56数组中数字出现的次数
题目 一个整型数组 nums 里除两个数字之外,其他数字都出现了两次.请写程序找出这两个只出现一次的数字.要求时间复杂度是O(n),空间复杂度是O(1). 输入:nums = [4,1,4,6] 输出 ...
- 【Git】4、创建代码仓库,HTTP、SSH拉取远端代码
拉取远端代码:使用Git命令下载远程仓库到本地 文章目录 拉取远端代码:使用Git命令下载远程仓库到本地 1.创建远程代码仓库 2.创建仓库 3.进入仓库 4.HTTP(S)获取远程仓库 首次拉取 更 ...
- 【ASM】从asm中复制文件到本地,或者从本地到asm中方法
工作中,有时需要把文件从ASM中复制到文件系统中或者反过来,做一些维护操作,本文介绍了4种复制文件的的方法: ASMCMD中的cp命令(11g) dbms_file_transfer包 rman的co ...
- hive窗口函数/分析函数详细剖析
hive窗口函数/分析函数 在sql中有一类函数叫做聚合函数,例如sum().avg().max()等等,这类函数可以将多行数据按照规则聚集为一行,一般来讲聚集后的行数是要少于聚集前的行数的.但是有时 ...
- Rancher On K3s 高可用架构部署
Rancher 推荐部署架构 k3s 模式 RKE 和 k8s 模式 备注: 我对 RKE 的理解就是 Ansible + kubeadm 的打包,首先 rke 需要到每一个节点都可以免密 ssh , ...
- Mysql数据库下InnoDB数据引擎下的事务详解
一.什么是数据库事务? 数据库事务( transaction)是访问并可能操作各种数据项的一个数据库操作序列,这些操作要么全部执行,要么全部不执行,是一个不可分割的工作单位.事务由事务开始与事务结束之 ...
- babel : 无法加载文件 C:\Users\win\AppData\Roaming\npm\babel.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/ go.micros
babel报错:babel : 无法加载文件 C:\Users\win\AppData\Roaming\npm\babel.ps1,因为在此系统上禁止运行脚本.有关详细信息,请参阅 https:/ g ...