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 ...
随机推荐
- 论文阅读笔记(十四)【AAAI2020】:Appearance and Motion Enhancement for Video-based Person Re-identification
Introduction 本文的贡献:提出了基于视频的行人重识别模型:Appearance and Motion Enhancement Model(AMEM).该模型对两类信息进行提取:提出了App ...
- malloc分配内存的结构
在C/C++,我们都可以使用malloc进行分配大小,单位是字节. malloc实际分配的内存大小和传入的参数其实是不同的. 实际分配的内存大小>传入的参数大小. malloc分配的内存 ...
- IDA PRO
链接:https://pan.baidu.com/s/1LTXhXra5Honpn3L9rfSOgA 提取码:7bwb 工具下载地址: https://www.jb51.net/softjc/5799 ...
- SSM开发健康信息管理系统
Spring+Spring MVC+MyBatis基于MVC架构的个人健康信息管理系统 采用ssm框架,包含 健康档案.健康预警(用户输入数据,系统根据范围自动判断给出不同颜色箭头显示). 健康分析. ...
- python--终端工具之subprocess
一. subprocess.getstatusoutput import subprocess cmd = 'ifconfig' def cmds(cmd,print_msg=True): statu ...
- HDU1408 - 盐水的故事
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1408 解题思路:主要考虑最后一滴可能不满足D毫升,但仍算1秒.另外还要注意浮点数的比较. #inclu ...
- Django---Django返回用户输入数据
前面写了关于HTML和Django结合的文章,通过视图与HTML结合,然后加上urls渲染返回给用户浏览器.很明显我们都能看到这些仅仅是静态HTML,那如何通过Django创建动态的HTML呢? 动态 ...
- Java(二)Arrays工具类
Arrays是一个专门用于操作数组的工具类,该类位于java.util包中. Arrays的常用方法: 1.排序方法 原型:static void sort(int [] a) 功能:对指定的int型 ...
- JavaDay9(下)
Java learning_Day9(下) 本人学习视频用的是马士兵的,也在这里献上 <链接:https://pan.baidu.com/s/1qKNGJNh0GgvlJnitTJGqgA> ...
- CTS、CLS、CLR
CTS.CLS和CLR是.NET框架的3个核心部分,下面分别对它们进行介绍. 1)CTS Common Type System CTS即通用类型系统,它定义了如何在.NET Framework运行库 ...