过了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. MySQL命令随手记之alter

    修改表名 alter table 表名 rename 新表名; //修改table名 添加.删除.修改字段 alter table 表名 add [column] 列名 数据类型; //添加colum ...

  2. jq模糊匹配(qq:2798641729)

    图灵学院--Java高级架构师-互联网企业级实战VIP课程(价值6380)(qq:1324981084) jq是一般程序员在前台开发的时候都会使用的技术,其中模糊匹配查询在动态添加标签的时候经常用到, ...

  3. C# 制作关键字醒目显示控件

    实现方式:WinForm自定义控件,继承系统Label控件实现. 第1步:创建“组件”,取名为:MarkLabel     第2步:修改添加如下代码: /* 添加命名空间引用: * using Sys ...

  4. 解决H5页面点击一瞬间出现灰色背景的问题

    H5页面有时会出现点击一瞬间出现灰色背景的问题(ios会,安卓不会),解决方法: 加上样式: -webkit-tap-highlight-color: transparent; 如果以上方法不行,则是 ...

  5. vue 3.0 项目搭建移动端 (六) 命名路由同级控制

    const Tabbar = () => import('@/components/Tabbar'); export default [ { path: '/', name: 'home', c ...

  6. lint-staged 使用教程

    lint-staged 是一个在git暂存文件上运行linters的工具,当然如果你觉得每次修改一个文件就给所有文件执行一次lint检查不恶心的话,这个工具对你来说就没有什么意义了,请直接关闭即可. ...

  7. HTML5 canvas绘图基础(电子名片生成器源码)

    创建canvas <canvas id="myCanvas" class="canvas"> 您的浏览器不支持canvas </canvas& ...

  8. tensorflow张量排序

    本篇记录一下TensorFlow中张量的排序方法 tf.sort和tf.argsort # 声明tensor a是由1到5打乱顺序组成的 a = tf.random.shuffle(tf.range( ...

  9. Nginx配置Https指南

    前言 本文是对Nginx配置SSL证书的总结. 申请SSL证书 你可以从任何证书提供商处申请证书,这里以阿里云为例. 打开阿里云SSL证书控制台,点击购买证书 选择免费型一年期的证书,点击立即购买 注 ...

  10. Java安全笔记

    前言 后端接口开发中,涉及到用户私密信息(用户名.密码)等,我们不能传输明文,必须使用加密方式传输.这次政府项目中,安全测试组提出了明文传输漏洞,抽空研究了下Java加解密相关知识,记录下. 散列函数 ...