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 ...
随机推荐
- [WPF 自定义控件]创建包含CheckBox的ListBoxItem
1. 前言 Xceed wpftoolkit提供了一个CheckListBox,效果如下: 不过它用起来不怎么样,与其这样还不如参考UWP的ListView实现,而且动画效果也很好看: 它的样式如下: ...
- c语言心形告白代码实现
c语言心形告白代码实现 1.彩色告白 include<stdio.h> include<math.h> include<windows.h> include< ...
- StarUML之六、StarUML规则与快捷键
本章内容参考官网即可,不做详细说明,实践出真知! starUMl规则主要是在模型设计的约束条件 https://docs.staruml.io/user-guide/validation-rules ...
- IBM x3250m5安装redhat 6.5 加载raid卡驱动
原文地址:http://www.i5i6.net/post/118.html 1. 下载对应raid卡驱动 for redhat6.5 x64(如本次x3250 m5 c100阵列卡驱动 lsi_dd ...
- idea svn提交时,performing vcs refresh时间很长的解决办法
解决方法:version control -> local changes -> local changelist 列表中无用的文件或文件夹右键选择svn忽略 ps:原因是文件太多,导致对 ...
- 新年上新!极光认证 Web SDK 首版上线
新年伊始,极光开发者服务也抢先为各位开发者朋友带来了"新年大礼包",几款明星产品都悉数有不少更新: 极光认证 Web SDK 版本上线 相信不少小伙伴早已熟知极光认证这款产品,3秒 ...
- Enityt模型特性
数据验证相关的数据注解: 特性 解释 Remote 使用 jQuery 验证插件远程验证程序的特性 FileExtension 验证文件扩展名 Compare 比较两个属性的值 RegularExpr ...
- 分库分表技术演进&最佳实践
每个优秀的程序员和架构师都应该掌握分库分表,这是我的观点. 移动互联网时代,海量的用户每天产生海量的数量,比如: 用户表 订单表 交易流水表 以支付宝用户为例,8亿:微信用户更是10亿.订单表更夸张, ...
- 洛谷P1464 Function HDU P1579 Function Run Fun
洛谷P1464 Function HDU P1579 Function Run Fun 题目描述 对于一个递归函数w(a,b,c) 如果a≤0 or b≤0 or c≤0就返回值11. 如果a> ...
- 剑指offer-面试题55-平衡二叉树-递归
/* 题目: 判断二叉树是否为平衡二叉树. */ /* 思路: 判断二叉树的左子树和右子树高度相差是否为1. */ #include<iostream> #include<cstri ...