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 ...
随机推荐
- toj 3019 Hidden Password (最小表示法)
Hidden Password 时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte总提交: 53 测试通过: 19 描述 Some time the progr ...
- 【爬虫】 爬虫请求json数据,返回乱码问题的解决
from django.http import JsonResponse from rest_framework.utils import json from utils import request ...
- python实现自动点赞
1.思路通过pyautogui可以实现鼠标点击.滚动鼠标.截屏等操作.由此功能实现打开页面,进行点赞.aircv可以从大图像获得小图像的位置,利用pyautogui截屏得到的图片,可以在页面获取到每一 ...
- JavaScript的算数,赋值,比较和逻辑运算符
类似a=1+1这样的表达式称为运算符,js的运算符分为算数,赋值,比较和逻辑运算符:常见的算数有:+ - * / %(加减乘除,取模),比方说5/4=4*1+1:5%4=1,js算数顺序:从左往右,先 ...
- Cesium案例解析(五)——3DTilesPhotogrammetry摄影测量3DTiles数据
目录 1. 概述 2. 案例 3. 结果 1. 概述 3D Tiles是用于传输和渲染大规模3D地理空间数据的格式,例如摄影测量,3D建筑,BIM / CAD,实例化特征和点云等.与常规的模型文件格式 ...
- Ubuntu系统下使用php7+mysql+apache2搭建自己的博客
很多人都有写博客的习惯,奈何国内的博客网站正在一家家地关闭与重整,部分博客网站也充斥着太多的广告,使用体验非常不好.对于爱写博客的朋友来说,其实还有一个更好的选择,那就是自己搭建一个博客. 搭建一个自 ...
- Java架构师必看,超详细的架构师知识点分享!
在Java程序员行业中,有不少Java开发人员的理想是成为一名优秀的Java架构师,Java架构师的主要任务不是从事具体的软件程序的编写,而是从事更高层次的开发构架工作.他必须对开发技术非常了解,并且 ...
- proptypes介绍
开始 prop-types的主要作用:对props中数据类型进行检测及限制 引用方法:import PropTypes from 'prop-types' 用法: // 基本用法 用来检测数据类型 c ...
- 重构与动态为angularjs栏位赋值或获取值
先来看下面一段html: 这个ng-model名称带有一定的规律带有序号. 先来实现数据绑定,从数据取到数据后,为ng-model绑定相对应的值: var c = response.data $sco ...
- css的三种导入方式
内联样式表 <p style="font-size:50px; color:blue">css内联样式表</p> 内部样式表 <style type= ...