过了n天补的题解:D

AB就不用说了

C. Obtain The String

思路挺简单的,就是贪心,但是直接贪心的复杂度是O(|s|*|t|),会超时,所以需要用到序列自动机

虽然名字很高端但是就是个数组啦(不过我自己想不到就是了)

next[i][j]表示i之后第一次出现j字符的位置,用这个函数就可以把复杂度降到O(|t|)

#include <cstdio>
#include <cstring>
using namespace std;
const int N = ;
char s[N], t[N];
bool flag[];
int next[N][];
int main() {
int cs;
scanf("%d", &cs);
while (cs--) {
scanf("%s", s);
scanf("%s", t);
int len1 = strlen(t);
int len2 = strlen(s);
int l = , cnt = ;
memset(next, 0xff, sizeof(next));
for (int i = len2 - ; i >= ; i--) {
for (int j = ; j < ; j++)
next[i][j] = next[i + ][j];
next[i][s[i] - 'a'] = i; //递推式很简单
}
int pos = ;
for (int i = ; i < len1; i++) {
pos = next[pos][t[i] - 'a'] + ;
if (pos == ) {
cnt++;
pos = next[][t[i] - 'a'] + ;
if (pos == ) {
cnt = -;
break;
}
}
}
printf("%d\n", cnt);
}
return ;
}

D.Same GCDs

如果知道了gcd(a, b) = gcd(a - b, b) (a > b)就是欧拉函数模板题,可惜不知道

gcd(a, m) = gcd(a + x, m) = k

gcd(a/k, m/k) = gcd(a/k + b, m/k) = 1

刚好就是phi[m/k]的值

#include <cstdio>
using namespace std; long long gcd(long long a, long long b) {
return (a % b == ) ? b : gcd(b, a % b);
} long long GetPhi(long long a) {
long long res = a;
for (long long i = ; i * i <= a; i++) {
if (a % i == ) {
res -= res / i;
while (a % i == )
a = a / i;
}
}
if (a > ) res -= res / a;
return res;
} int main() {
int t;
scanf("%d", &t);
while (t--) {
long long a, m;
scanf("%lld %lld", &a, &m);
long long k = gcd(a, m);
long long ans = GetPhi(m / k);
printf("%lld\n", ans);
}
return ;
}
// gcd(a, m) = gcd(a + x, m) = k
// gcd(a/k, m/k) = gcd(a/k + b, m/k) = 1
// phi[m/k];

E.Permutation Separation

令set1 <= val,set2 =val 为若在pos的位置把序列分成两半,则val:=val + 1的时候[1, pos - 1]加上val对应的cost,[pos, n - 1]就减去这个cost,这个可以用线段树维护

然后有一个坑,把我卡在test 6了很久很久,就是val是从0开始,不是从1开始

#include <cstdio>
#include <algorithm>
using namespace std;
#define g(l, r) (l + r | l != r)
#define o g(l, r)
#define ls g(l, mid)
#define rs g(mid + 1, r)
const int N = * 1e5 + ;
typedef long long ll;
ll pos[N], p, w1, a[N];
ll pre[N], minn[N << ], mark[N << ];
int L, R;
void build(int l, int r) {
if (l == r) {
minn[o] = pre[l];
return ;
}
int mid = l + r >> ;
build(l, mid);
build(mid + , r);
minn[o] = min(minn[ls], minn[rs]);
} inline void PushDown(int l, int r) {
if (mark[o] != ) {
int mid = l + r >> ;
mark[ls] += mark[o];
mark[rs] += mark[o];
minn[ls] = minn[ls] + mark[o];
minn[rs] = minn[rs] + mark[o];
mark[o] = ;
}
} void update(int l, int r, int val) {
if (L > R) return ;
if (L <= l && R >= r) {
minn[o] += val;
mark[o] += val;
return ;
}
PushDown(l, r);
int mid = l + r >> ;
if (L <= mid) update(l, mid, val);
if (R > mid) update(mid + , r, val);
minn[o] = min(minn[ls], minn[rs]);
} int main() {
int n;
scanf("%d", &n);
for (int i = ; i <= n; i++) {
int k;
scanf("%d", &k);
pos[k] = i;
}
for (int i = ; i <= n; i++) {
scanf("%lld", &a[i]);
pre[i] = pre[i - ] + a[i];
}
build(, n - );
ll ans = minn[g(, n - )];
for (int i = ; i <= n; i++) {
L = pos[i], R = n - ;
update(, n - , -a[pos[i]]);
L = , R = pos[i] - ;
update(, n - , a[pos[i]]);
ans = min(ans, minn[g(, n - )]);
}
printf("%lld", ans);
return ;
}

Educational Codeforces Round 81 (Rated for Div. 2) 题解的更多相关文章

  1. [Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和)

    [Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和) E. Permuta ...

  2. Educational Codeforces Round 63 (Rated for Div. 2) 题解

    Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...

  3. Educational Codeforces Round 65 (Rated for Div. 2)题解

    Educational Codeforces Round 65 (Rated for Div. 2)题解 题目链接 A. Telephone Number 水题,代码如下: Code #include ...

  4. Educational Codeforces Round 64 (Rated for Div. 2)题解

    Educational Codeforces Round 64 (Rated for Div. 2)题解 题目链接 A. Inscribed Figures 水题,但是坑了很多人.需要注意以下就是正方 ...

  5. Educational Codeforces Round 60 (Rated for Div. 2) 题解

    Educational Codeforces Round 60 (Rated for Div. 2) 题目链接:https://codeforces.com/contest/1117 A. Best ...

  6. Educational Codeforces Round 58 (Rated for Div. 2) 题解

    Educational Codeforces Round 58 (Rated for Div. 2)  题目总链接:https://codeforces.com/contest/1101 A. Min ...

  7. Educational Codeforces Round 81 (Rated for Div. 2) B. Infinite Prefixes

    题目链接:http://codeforces.com/contest/1295/problem/B 题目:给定由0,1组成的字符串s,长度为n,定义t = sssssss.....一个无限长的字符串. ...

  8. Educational Codeforces Round 81 (Rated for Div. 2) C. Obtain The String

    题目链接:http://codeforces.com/contest/1295/problem/C 题目:给定字符串s,t.  给定一个空串z,需要按照规则把z构造成 string z == stri ...

  9. Educational Codeforces Round 81 (Rated for Div. 2) A-E简要题解

    链接:https://codeforces.com/contest/1295 A. Display The Number 贪心思路,尽可能放置更多位,如果n为奇数,消耗3去放置一个7,剩下的放1 AC ...

随机推荐

  1. JavaScript之BOM基础

    BOM(Browser Object Model)也叫浏览器对象,它提供了很多对象,用于访问浏览器的功能.但是BOM是没有标准的,每一个浏览器厂家会根据自己的需求来扩展BOM对象.本文主要以一些简单的 ...

  2. Android中通过ImageSwitcher实现相册滑动查看照片功能(附代码下载)

    场景 效果 注: 博客: https://blog.csdn.net/badao_liumang_qizhi关注公众号 霸道的程序猿 获取编程相关电子书.教程推送与免费下载. 实现 将需要滚动查看的照 ...

  3. jenkin docker node 自动化部署配置

    jenkins 安装必须插件 NodeJS Plugin Publish Over SSH 1:新建一个任务,选择构建一个自由风格的软件项目 2:源码管理选择Git 2.1 填写Git项目地址Repo ...

  4. Hadoop架构及集群

    Hadoop是一个由Apache基金会所开发的分布式基础架构,Hadoop的框架最核心的设计就是:HDFS和MapReduce.HDFS为海量的数据提供了存储,而MapReduce则为海量的数据提供了 ...

  5. linux设置服务器时间

    在 Linux 机器上有两种时钟: 由内核维持的软件时钟(又称系统时钟)和在机器关机后记录时间的(电池供电的)硬件时钟. 启动的时候, 内核会把系统时钟与硬件时钟同步. 之后, 两个时钟各自独立运行. ...

  6. linux中关机重启命令

    shutdown 解释 关机重启命令 语法 shutdown [选项] 时间 -c 取消前一个关机命令 -h 关机 -r 重启 示例 # 现在立刻关机 shutdown -h now # 定时关机 s ...

  7. 0x01 C语言-编写第一个hello world

    学习每一个编程语言都是从 "Hello world!" 开始的,这好像就是编程界一条不成文的规定一样. 在这篇文章中,我将教大家编写一个可以输出 "Hello world ...

  8. PERC H310 配置详细步骤【阵列RAID创建】【阵列恢复】【阵列池创建】

    机器配置: HP PRO6300 二手淘的201912,HP的主板芯片Intel Q75芯片组,集成显卡(集成显卡与H310阵列卡冲突),CPU Intel I5 3450 [raid5阵列创建] 1 ...

  9. python——面向对象基础(2),烤地瓜

    """Date:2020.2.9 测试案例:烤地瓜需求分析1.烤的时间和对应的地瓜状态:2.烤制过程步骤: 1.定义类, 地瓜属性,状态,烤的时间,调料 2.定义方法,怎 ...

  10. opencv —— src.at<Vec3b>(i, j)[0]、src.at<uchar>(i, j)、src.ptr<uchar>(i) 访问图像的单个像素

    动态地址访问像素:src.at<Vec3b>(i, j)[0].src.at<uchar>(i, j)  int b = src.at<Vec3b>(i, j)[0 ...