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 ...
随机推荐
- ThinkPHP v6.0.x 反序列化漏洞利用
前言: 上次做了成信大的安询杯第二届CTF比赛,遇到一个tp6的题,给了源码,目的是让通过pop链审计出反序列化漏洞. 这里总结一下tp6的反序列化漏洞的利用. 0x01环境搭建 现在tp新版本的官网 ...
- 吴裕雄--天生自然 R数据分析:2014年美国人时间使用调查(ATUS)饮食与健康模块文件分析
# libraries we'll need library(car) # for avplots library(tidyverse) # for general utility functions ...
- ES的性能优化
ES的性能优化 es在数据量很大的情况下(数十亿级别)如何提高查询效率? 在es里,不要期待着随手调一个参数,就可以万能的应对所有的性能慢的场景.也许有的场景是你换个参数,或者调整一下语法,就可以搞定 ...
- 安装TensorFlow失败
ERROR: Could not find a version that satisfies the requirement tensorflow==2.1.0 (from versions: non ...
- 170.分组-group、permission、user的操作
分组 1.Group.objects.create(group_name):创建分组. 2.group.permissions:某个分组上的权限.多对多关系. (1)group.permissions ...
- Android记事本在菜单栏添加搜索按钮方法
效果图 这个app结构和我之前将记事本开发的博客基本一致,我这里直接讲一下怎样添加 使用的开发软件为android studio 首先在res目录下新建文件夹menu,添加目录布局文件main_men ...
- jquery form表单赋值封装
;!(function ($) { $.fn.setFormValue = function (options) { var $this = $(this); $.each(options, func ...
- link(外部资源关系)
规定了外部资源与当前文档的关系 常于链接样式表<link href="/media/examples/link-element-example.css" rel=" ...
- CF1227F2 Wrong Answer on test 233 (Hard Version)
题意 \(n\)道题,每道题有\(k\)种选项,其中第\(i\)道题正确答案是\(a_i\),但是填答案的时候填错啦,第一道题的选择填到了第二道题...第\(n\)道题的选择填到了第一道题,求在\(k ...
- Qt多线程实现思路二
建立一个继承于Qobject的类myThread 在类myThread中定义线程处理函数不必是思路一里的run(); 在窗口类中开辟一个自定义线程myThread的指针对象myT = new myTh ...