Educational Codeforces Round 81 (Rated for Div. 2) 题解
过了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) 题解的更多相关文章
- [Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和)
[Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和) E. Permuta ...
- Educational Codeforces Round 63 (Rated for Div. 2) 题解
Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...
- Educational Codeforces Round 65 (Rated for Div. 2)题解
Educational Codeforces Round 65 (Rated for Div. 2)题解 题目链接 A. Telephone Number 水题,代码如下: Code #include ...
- Educational Codeforces Round 64 (Rated for Div. 2)题解
Educational Codeforces Round 64 (Rated for Div. 2)题解 题目链接 A. Inscribed Figures 水题,但是坑了很多人.需要注意以下就是正方 ...
- Educational Codeforces Round 60 (Rated for Div. 2) 题解
Educational Codeforces Round 60 (Rated for Div. 2) 题目链接:https://codeforces.com/contest/1117 A. Best ...
- Educational Codeforces Round 58 (Rated for Div. 2) 题解
Educational Codeforces Round 58 (Rated for Div. 2) 题目总链接:https://codeforces.com/contest/1101 A. Min ...
- Educational Codeforces Round 81 (Rated for Div. 2) B. Infinite Prefixes
题目链接:http://codeforces.com/contest/1295/problem/B 题目:给定由0,1组成的字符串s,长度为n,定义t = sssssss.....一个无限长的字符串. ...
- 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 ...
- Educational Codeforces Round 81 (Rated for Div. 2) A-E简要题解
链接:https://codeforces.com/contest/1295 A. Display The Number 贪心思路,尽可能放置更多位,如果n为奇数,消耗3去放置一个7,剩下的放1 AC ...
随机推荐
- Java代码优化实践
1. 尽量指定类的final修饰符 带有final修饰符的类是不可派生的.指定一个类为final,则该类所有方法都是final.Java编译器会会找机会内联所有否final方法,这样能够使性能平均 ...
- JS常见的表单验证,H5自带的验证和正则表达式的验证
H5验证 自带的验证无法满足需求: <form action="" method="get"> name:<input type=" ...
- 如何使用maven开启一个webapp项目
1.使用maven创建好一个webapp项目 2.pom.xml: 第一步:修改版本 <properties> <project.build.sourceEncoding>UT ...
- 【redis】基于redis实现分布式并发锁
基于redis实现分布式并发锁(注解实现) 说明 前提, 应用服务是分布式或多服务, 而这些"多"有共同的"redis"; (2017-12-04) 笑哭, 写 ...
- cra
const paths = require('react-scripts/config/paths'); paths.appBuild = path.join(path.dirname(paths.a ...
- 解决jmeter5.1高版本linux CPU,IO,Memory监控性能测试 java.lang.NoSuchMethodError: org.apache.jmeter.samplers.SampleSaveConfiguration.setFormatter(Ljava/t
jmeter中也可以监控服务器的CPU和内存使用情况,但是需要安装一些插件还需要在被监测服务器上开启服务. 安装性能监控插件(jmeter-plugins)后报如下错误,是由于jmeter版本过高jm ...
- 【Spring】事务(transactional) - REQUIRES_NEW在JdbcTemplate、Mybatis中的不同表现
环境 数据库: oracle 11g JAR: org.springframework:spring-jdbc:4.3.8.RELEASE org.mybatis:mybatis:3.4.2 概念 R ...
- PTA 凑零钱(深度优先搜索)
韩梅梅喜欢满宇宙到处逛街.现在她逛到了一家火星店里,发现这家店有个特别的规矩:你可以用任何星球的硬币付钱,但是绝不找零,当然也不能欠债.韩梅梅手边有 10000 枚来自各个星球的硬币,需要请你帮她盘算 ...
- WebDev.WebServer20.exe应用程序错误
我的.net网页,在iis运行成功,在VS2010调试网页时报一个WebDev.WebServer20.exe应用程序错误. 最后查找资料,发现了网站设置的框架是2.0,在vs2010里不能调试2.0 ...
- 数据结构(集合)学习之Queue
集合 框架关系图: Collection接口下面有三个子接口:List.Set.Queue.此篇是关于Queue<E>的简单学习总结. 补充:HashTable父类是Dictionary, ...