Educational Codeforces Round 81 (Rated for Div. 2) A-E简要题解
链接:https://codeforces.com/contest/1295
A. Display The Number
贪心思路,尽可能放置更多位,如果n为奇数,消耗3去放置一个7,剩下的放1
AC代码:
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
int n,m;
int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
string ans = "";
if(n% == ) ans.append(,''),ans.append((n-)/,'');
else ans.append(n/,'');
cout<<ans<<endl;
}
return ;
}
B. Infinite Prefixes
前缀和思路,存字符串前n个位置的前缀和,如果cnt0+cnt1 = 0,且 x 在前缀和中出现过,那么就是无限的。
其他情况扫一遍字符串的前缀和,然后与x的差值和f[n]取余,如果为0,说明差值可以用任意个字符串的cnt0-cnt1拼接起来,ans就++
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
int t;
cin>>t;
while(t--){
int n,x;
cin>>n>>x;
string s;
cin>>s;
int f[n+];
f[] = ;
int can = ;
for(int i = ;i<n;i++){
if(f[i] == x) can = ;
if(s[i] == '') f[i+] = f[i]-;
else f[i+] = f[i] + ;
}
if(f[n] == x) can = ;
if(can == && f[n] == ) {
cout<<-<<endl;
continue;
}
int ans = ;
ll d = f[n];
// cout<<d<<endl;
for(int i = ;i<=n;i++){
if(f[i] == x) ans++;
if(i!= && f[i]<x && d> &&(x-f[i])%d==) ans++;
if(i!= && f[i]>x && d< &&(f[i]-x)%(-d)==) ans++;
}
cout<<ans<<endl;
}
return ;
}
C. Obtain The String
贪心思路,用一个next二维数组存储当前位置i 下一个26个字母的距离最近的位置,然后按需求跳转过去,如果找不到,那么需要的字串数量就+1,再从头开始扫描原串。
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int maxn = 1e5+;
int main()
{
int n;
cin>>n;
while(n--){
string s,t;
cin>>s>>t;
int next[s.length()+][];
for(int i = ;i<;i++) next[s.length()][i] = -;
for(int i = s.length()-;i>=;i--){
for(int j = ;j<;j++) next[i][j] = next[i+][j];//存储i后面,26个字母距离i最近的位置
int cur = s[i]-'a';
next[i][cur] = i+;
}
int ans = ;
int cur = ;
bool f = true;
for(int i = ;i<t.length();i++){
int now = t[i]-'a';
if(next[cur][now]!=-) cur = next[cur][now];//跳转过去
else{
ans++;//如果找不的ans就++
cur = ;
if(next[cur][now] == -){//如果还没有,说明无法拼接起来
f = false;
break;
}
cur = next[cur][now];
}
}
if(!f) cout<<-<<endl;
else cout<<ans<<endl;
}
return ;
}
D. Same GCDs
设gcd(a,m)= n,那么找gcd(a +x ,m)= n个数,其实就等于找gcd((a+x)/n,m/n) = 1的个数,等价于求m/n的欧拉函数
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
ll euler_phi(ll n) {
ll m = ll(sqrt(n + 0.5));
ll ans = n;
for (ll i = ; i <= m; i++)
if (n % i == ) {
ans = ans / i * (i - );
while (n % i == ) n /= i;
}
if (n > ) ans = ans / n * (n - );
return ans;
}
ll gcd(ll a, ll b) {
if (b == ) return a;
return gcd(b, a % b);
}
int main()
{
int t;
cin>>t;
while(t--){
ll a,m;
cin>>a>>m;
m=m/gcd(a,m);
ll x = euler_phi(m);
cout<<x<<endl;
}
return ;
}
E. Permutation Separation
建一颗线段树,叶子结点是花费从1到i所需要花费的前缀和,表示前i个元素全部移动到右边的花费,再维护区间最小值,然后从1到n-1扫一遍,对于第i个位置,找到数字i在序列中的位置 pos ,将区间1到pos-1加上数字i移动的花费,pos到n-1减去数字i移动的花费,因为位置大于等于i 的时候,不需要将数字i移动到右边,位置小于i 时,需要把数字i移动到左边,所以需要增加数字i的花费,结合线段树维护的是前缀和数组,那么对于每一个位置i 用线段树维护的最小值去更新答案ans即可。
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int maxn = 2e5+;
ll pos[maxn],cost[maxn],a[maxn],sum[maxn];
struct segT{
ll l,r;
ll dat,lz;
}t[maxn*];
void build(ll p,ll l,ll r){
t[p].l = l,t[p].r = r;
if(l == r) { t[p].dat = sum[l] ,t[p].lz = ;return;}
ll mid = (l+r)/;
build(p*,l,mid);
build(p*+,mid+,r);
t[p].dat = min(t[p*].dat ,t[p*+].dat );
}
void upd(ll p,ll L,ll R,ll v){
if(t[p].l >= L&&t[p].r <= R ) {t[p].dat += v,t[p].lz +=v;return;}
if (t[p].lz && L!=R ) {
t[p * ].dat += t[p].lz ,t[p*+].dat +=t[p].lz ;
t[p * ].lz += t[p].lz ,t[p*+].lz += t[p].lz;
t[p].lz = ;
}
int mid = (t[p].l + t[p].r )/;
if(L<=mid) upd(p*,L,R,v);
if(R>mid) upd(p*+,L,R,v);
t[p].dat = min(t[p*].dat ,t[p*+].dat );
}
int query(ll p,ll l,ll r){
if(l<=t[p].l && r>=t[p].r ) return t[p].dat ;
int mid = (t[p].l + t[p].r )/;
int val = 0x3f3f3f3f;
if(l<=mid) val = min(val,query(p*,l,r));
if(r>mid) val = min(val,query(p*+,l,r));
return val;
}
int main()
{
int n;
scanf("%d",&n);
for(int i = ;i<=n;i++) {
scanf("%lld",&a[i]);
pos[a[i]] = i;
}
for(int i = ;i<=n;i++){
scanf("%lld",&cost[i]);
sum[i] = sum[i-] + cost[i];
}
build(,,n-);
ll ans = cost[n];
ans = min(ans,t[].dat );//特判
for(int i = ;i<=n;i++){
if(pos[i]!=n) upd(,pos[i],n-,-cost[pos[i]]);
if(pos[i]!=) upd(,,pos[i]-,cost[pos[i]]);
ans = min(ans,t[].dat);
}
printf("%lld",ans);
return ;
}
Educational Codeforces Round 81 (Rated for Div. 2) A-E简要题解的更多相关文章
- [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 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 0~9需要多少笔画,自取7和1,判奇偶 #include<bits/stdc++.h> using namespace std; #define ll long long #defin ...
- Educational Codeforces Round 81 (Rated for Div. 2) 题解
过了n天补的题解:D AB就不用说了 C. Obtain The String 思路挺简单的,就是贪心,但是直接贪心的复杂度是O(|s|*|t|),会超时,所以需要用到序列自动机 虽然名字很高端但是就 ...
- Educational Codeforces Round 81 (Rated for Div. 2)E(线段树)
预处理把左集划分为大小为1~i-1时,把全部元素都移动到右集的代价,记作sum[i]. 然后枚举终态时左集的大小,更新把元素i 留在/移动到 左集的代价. 树状数组/线段树处理区间修改/区间查询 #d ...
- Educational Codeforces Round 81 (Rated for Div. 2) - D. Same GCDs(数学)
题目链接:Same GCDs 题意:给你两个数$a$,$m(1 \leq a < m \leq 10^{10})$,求有多少个$x$满足:$0 \leq x < m$且$gcd(a,m)= ...
- Educational Codeforces Round 76 (Rated for Div. 2)E(dp||贪心||题解写法)
题:https://codeforces.com/contest/1257/problem/E 题意:给定3个数组,可行操作:每个数都可以跳到另外俩个数组中去,实行多步操作后使三个数组拼接起来形成升序 ...
- Educational Codeforces Round 92 (Rated for Div. 2) B、C题解
TAT 第一场codeforces B. Array Walk #暴力 #贪心 题目链接 题意 有\(a1, a2, ..., an\) 个格子(每个格子有各自分数),最初为1号格(初始分数为\(a1 ...
随机推荐
- 如何开发自己的第一个 Serverless Component
前言 上一篇 基于 Serverless Component 的全栈解决方案 介绍 Serverless Component 是什么和如何使用 Serverless Component 开发一个全栈应 ...
- STL-map/multimap 简述
#include <iostream> #include <cstdio> #include <map> using namespace std; int main ...
- C#加密与解密(DES\RSA)学习笔记
本笔记摘抄自:https://www.cnblogs.com/skylaugh/archive/2011/07/12/2103572.html,记录一下学习过程以备后续查用. 数据加密技术是网络中最基 ...
- PAT (Basic Level) Practice (中文)1029 旧键盘 (20 分)
旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现.现在给出应该输入的一段文字.以及实际被输入的文字,请你列出肯定坏掉的那些键. 输入格式: 输入在 2 行中分别给出应该输入的文字.以及 ...
- Verilog-case、casez和casex的区别
参考博客:https://www.cnblogs.com/guolongnv/articles/6906929.html 1.基本概念 1)?表示z,而不是“dont care” 2)区分: case ...
- Redis可能出现的问题
缓存穿透 一般出现这样的问题,是因为当我们查询一条肯定不存在的数据的时候,缓存中没有,就会透过缓存来查询数据库,数据库也不存在,这样就不会将值保存在缓存中,最后还是缓存和数据库中都没有,如果一直访问这 ...
- VPS性能测试shell工具以及锐速安装
比较熟悉的UnixBench非常耗费资源,需要长时间跑满cpu和IO,很多主机商都深恶痛绝,会做各种限制,其实也代表不了实际使用的业务效果,毕竟真正需要那么多cpu和IO的应用并不多.而网络状况却是大 ...
- Python之四:控制流
1.If 逻辑判断: if a: b elif c: d else: e 先判断a语句块的值是否为真,如果为真,则执行b语句块,如果不为真则转到elif判断c语句块的值是否为真,如果为真执行d语句块, ...
- source insight增加tab标签页的方法之sihook
1.效果如下 2.方法见如下博客 http://www.cnblogs.com/Red_angelX/archive/2013/01/23/2873603.html
- 阿里云oss 直传
sts获取 参考https://help.aliyun.com/document_detail/28792.html?spm=a2c4g.11186623.6.786.6fb238dfI9iiqA 配 ...