Educational Codeforces Round 147 div2题解

目录

A. Matching(签到)

思路:

代码: 

B. Sort the Subarray(签到)

思路:

代码:

C. Tear It Apart(枚举)

思路:

代码:

D. Black Cells(模拟)

思路:

 代码一:

代码二:(模仿自"AG"佬)


A. Matching(签到)

An integer template is a string consisting of digits and/or question marks.

A positive (strictly greater than 0) integer matches the integer template if it is possible to replace every question mark in the template with a digit in such a way that we get the decimal representation of that integer without any leading zeroes.

For example:

  • 42 matches 4?;
  • 1337 matches ????;
  • 1337matches 1?3?;
  • 1337 matches 1337;
  • 3 does not match ??;
  • 8 does not match ???8;
  • 1337 does not match 1?7.

You are given an integer template consisting of at most 5 characters. Calculate the number of positive (strictly greater than 0) integers that match it.

Input

The first line contains one integer t (1≤t≤2⋅104) — the number of test cases.

Each test case consists of one line containing the string s (1≤|s|≤5) consisting of digits and/or question marks — the integer template for the corresponding test case.

Output

For each test case, print one integer — the number of positive (strictly greater than 00) integers that match the template.

Example

input

8

??

?

0

9

03

1??7

?5?

9??99

output

90
9
0
1
0
100
90
100

思路:

签到,如果第一位是0,0种填法,?在第一位9种填法,在其它位10种填法

代码: 

#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;

void solve(){
    string s;
    cin>>s;
    if(s[0]=='0'){
        cout<<0<<endl;
        return;
    }
    
    int cnt=0;
    for(int i=0;i<s.length();i++){
        cnt+=(s[i]=='?');
    }

    ll ans=1;
    for(int i=1;i<=cnt;i++)ans*=10;
    if(s[0]=='?'){
        ans=ans/10*9;
    }
    
    cout<<ans<<endl;
    return;
}

int main(){

    int T=1;
    cin>>T;
    while(T--){
        solve();
    }
    return 0;
}

B. Sort the Subarray(签到)

Monocarp had an array a consisting of n integers. He has decided to choose two integers l and r such that 1≤l≤r≤n1≤, and then sort the subarray a[l..r] (the subarray a[l..r] is the part of the array a containing the elements al,al+1,al+2,…,ar−1,ar in non-descending order. After sorting the subarray, Monocarp has obtained a new array, which we denote as a′.

For example, if a=[6,7,3,4,4,6,5], and Monocarp has chosen l=2,r=5, then a′=[6,3,4,4,7,6,5]

You are given the arrays a and a′. Find the integers l and r that Monocarp could have chosen. If there are multiple pairs of values (l,r), find the one which corresponds to the longest subarray.

Input

The first line contains one integer t (1≤t≤104) — the number of test cases.

Each test case consists of three lines:

  • the first line contains one integer n(2≤n≤2⋅105);
  • the second line contains ntegers a1,a2,…,an (1≤ai≤n1);
  • the third line contains n integers a′1,a′2,…,a′n (1≤a′i≤n).

Additional constraints on the input:

  • the sum of n over all test cases does not exceed 2⋅105;
  • it is possible to obtain the array by sorting one subarray of a;
  • a′≠a (there exists at least one position in which these two arrays are different).

Output

For each test case, print two integers — the values of l and r (1≤l≤r≤n). If there are multiple answers, print the values that correspond to the longest subarray. If there are still multiple answers, print any of them.

Example

input

3

7

6 7 3 4 4 6 5

6 3 4 4 7 6 5

3

1 2 1

1 1 2

3

2 2 1

2 1 2

output

2 5
1 3
2 3

思路:

给两个数组,第二个数组是由第一个数组进行部分排序后得到的,求排序的最长区间的左右界

分别从前后遍历数组,找见第一个不同的位置,中间一定进行了排序,两边的元素如果加入后依然有序,可以加入到排序区间中

代码:

#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;

void solve(){
    int n;
    cin>>n;
    vector<int>a1(n+1),a2(n+1);
    for(int i=1;i<=n;i++)cin>>a1[i];
    for(int i=1;i<=n;i++)cin>>a2[i];
    
    int pos1,pos2;
    for(int i=1;i<=n;i++){      //左边一个个不同的位置
        if(a1[i]!=a2[i]){
            pos1=i;
            break;
        }
    }
    for(int i=n;i>=1;i--){      //右边一个个不同的位置
        if(a1[i]!=a2[i]){
            pos2=i;
            break;
        }
    }
    
    for(int i=pos1-1;i>=1;i--){     //若左边元素加入后仍有序,则加入排序区间
        if(a2[i]<=a2[i+1])pos1=i;
        else break;
    }
    for(int i=pos2+1;i<=n;i++){     //右边同理
        if(a2[i]>=a2[i-1])pos2=i;
        else break;
    }
    
    cout<<pos1<<' '<<pos2<<endl;
    return;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int T=1;
    cin>>T;
    while(T--){
        solve();
    }
    return 0;
}

C. Tear It Apart(枚举)

You are given a string s, consisting of lowercase Latin letters.

In one operation, you can select several (one or more) positions in it such that no two selected positions are adjacent to each other. Then you remove the letters on the selected positions from the string. The resulting parts are concatenated without changing their order.

What is the smallest number of operations required to make all the letters in s the same?

Input

The first line contains a single integer t (1≤t≤104) — the number of testcases.

The only line of each testcase contains a string s, consisting of lowercase Latin letters. Its length is from 11 to 2⋅105.

The total length of the strings over all testcases doesn't exceed 2⋅105.

Output

For each testcase, print a single integer — the smallest number of operations required to make all the letters in the given string s the same.

Example

input

5

abacaba

codeforces

oooooooo

abcdef

mewheniseearulhiiarul

output

1
3
0
2
4

Note

In the first testcase, you can select positions 2,42,4 and 66 and remove the corresponding letters 'b', 'c' and 'b'.

In the third testcase, the letters in the string are already the same, so you don't have to make any operations.

In the fourth testcase, one of the possible solutions in 22 operations is the following. You can select positions 1,4,61,4,6 first. The string becomes "bce". Then select positions 11 and 33. The string becomes "c". All letters in it are the same, since it's just one letter.

思路:

给一个字符串,每次可以挑任意个不相邻的字母删除,求变成仅有一种字符所需要的最少操作次数

枚举字符串中所有字符,字符把字符串分为若干段,找出最长的一段字符串最短的字符,全部化为这个字符所需要的操作次数最少

找规律发现字段为n的操作次数为logn-1

代码:

#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
int Log[200005];
void init(){        //预处理log函数
    Log[0]=-1;
    for(int i=1;i<=200005;i++){
        if(i&(i-1))Log[i]=Log[i-1];
        else Log[i]=Log[i-1]+1;
    }
}

void solve(){
    string s;
    cin>>s;
    int n=s.length();
    vector<int>pos[30];     //储存当前字符在字符串中的所有位置
    vector<int>dis(30,-1);      //表示当前字符划分出的最长一段的长度
    s=" "+s;
    for(int i=1;i<=n;i++){
        int x=s[i]-'a';
        pos[x].push_back(i);
        if(pos[x].size()==1)dis[x]=i-1;         //找出每个字符划分的最长的长度
        else dis[x]=max(dis[x],i-pos[x][pos[x].size()-2]-1);
    }

    int ans=INT_MAX;
    for(int i=0;i<28;i++)
        if(dis[i]!=-1){
            dis[i]=max(dis[i],n-pos[i][pos[i].size()-1]);
            ans=min(ans,dis[i]);    //找出最短的最长间距
        }
    cout<<Log[ans]+1<<endl;     //求操作次数
    return;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    init();
    int T=1;
    cin>>T;
    while(T--){
        solve();
    }
    return 0;
}

D. Black Cells(模拟)

You are playing with a really long strip consisting of 1018 white cells, numbered from left to right as 0, 1, 2 and so on. You are controlling a special pointer that is initially in cell 00. Also, you have a "Shift" button you can press and hold.

In one move, you can do one of three actions:

  • move the pointer to the right (from cell x to cell x+1);
  • press and hold the "Shift" button;
  • release the "Shift" button: the moment you release "Shift", all cells that were visited while "Shift" was pressed are colored in black.

(Of course, you can't press Shift if you already hold it. Similarly, you can't release Shift if you haven't pressed it.)

Your goal is to color at least k cells, but there is a restriction: you are given n segments [li,ri] — you can color cells only inside these segments, i. e. you can color the cell x if and only if li≤x≤ri for some i.

What is the minimum number of moves you need to make in order to color at least k cells black?

Input

The first line contains a single integer t (1≤t≤104) — the number of test cases.

The first line of each test case contains two integers n and k (1≤n≤2⋅105; 1≤k≤109) — the number of segments and the desired number of black cells, respectively.

The second line contains n integers l1,l2,…,ln (1≤l1<l2<⋯<ln≤109), where li is the left border of the i-th segment.

The third line contains n integers r1,r2,…,rn (1≤ri≤109 li≤ri<li+1−1), where ri is the right border of the i-th segment.

Additional constraints on the input:

  • every cell belongs to at most one segment;
  • the sum of n doesn't exceed 2⋅1052⋅105.

Output

For each test case, print the minimum number of moves to color at least k cells black, or −1 if it's impossible.

Example

input

4

2 3

1 3

1 4

4 20

10 13 16 19

11 14 17 20

2 3

1 3

1 10

2 4

99 999999999

100 1000000000

output

8
-1
7
1000000004

Note

In the first test case, one of the optimal sequences of operations is the following:

  1. Move right: pointer is moving into cell 1;
  2. Press Shift;
  3. Release Shift: cell 1 is colored black;
  4. Move right: pointer is moving into cell 2;
  5. Move right: pointer is moving into cell 3;
  6. Press Shift;
  7. Move right: pointer is moving into cell 4;
  8. Release Shift: cells 3 and 4 are colored in black.

We've colored 3 cells in 8 moves.

In the second test case, we can color at most 8 cells, while we need 20 cell to color.

In the third test case, one of the optimal sequences of operations is the following:

  1. Move right: pointer is moving into cell 1;
  2. Move right: pointer is moving into cell 2;
  3. Move right: pointer is moving into cell 3;
  4. Press Shift;
  5. Move right: pointer is moving into cell 4;
  6. Move right: pointer is moving into cell 5;
  7. Release Shift: cells 3, 4 and 5 are colored in black.

We've colored 3 cells in 7 moves.

思路:

有三个操作,按下和松开shift键,和往前走一步,按下shift键时的块会被涂色,给n个区间,只能在给定区间上涂色,求涂k个块需要的最少操作数

我们发现,区间长度为1时,需要进行两次操作来涂一个块,是最麻烦的,这样的区间涂得越少越好;如果涂好了k个块,当前区间还能往后走的话,就往后走一步,删去之前的一个1的区间不涂,这样更优。

如果已经没有长度为1的区间,若已经涂好了k个块,再往后走一定没有当前状态优,直接返回。

 代码一:

#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
struct node{
    ll l,r;
    ll cnt;     //区间内块数
}seg[200005];
void solve(){
    int n,k;
    cin>>n>>k;
    for(int i=1;i<=n;i++){
        cin>>seg[i].l;
    }
    vector<ll>pre(n+1,0);   //区间块数前缀和

    ll ans=INT_MAX;
    ll nn=0;       //长度为1的区间个数

    for(int i=1;i<=n;i++){
        cin>>seg[i].r;
        seg[i].cnt=seg[i].r-seg[i].l+1;
        pre[i]=pre[i-1]+seg[i].cnt;
    }
    
    if(pre[n]<k){       //能涂色的个数不足k个
        cout<<-1<<endl;
        return;
    }

    ll dstep=0;     //不走的1的个数
    for(int i=1;i<=n;i++){
            
        nn+=(seg[i].cnt==1);
        pre[i]=pre[i-1]+seg[i].cnt;     //由于修改,前缀和重算

        if(pre[i]>=k){
            ll tmp=pre[i]-k;    //空出来的位置
            
            ll step=i-dstep;       //shift的次数为总共区间数-不走的1的个数
                        //nn是剩余1的个数
            if(tmp>=nn){    //有多余的长度,用来补齐没走的1
                tmp-=nn;
                step-=nn;   
                seg[i].r-=tmp;      //所有1都被补起来,后面的区间就不走了
                ans=min(ans,step*2+seg[i].r);   
                break;
            }

            //tmp<nn
            dstep+=tmp;        //空缺位置全补上1
            ans=min(ans,(step-tmp)*2+seg[i].r);
            nn-=tmp;    
            pre[i]=k;
        }
    }

    cout<<ans<<endl;

    return;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int T=1;
    cin>>T;
    while(T--){
        solve();
    }
    return 0;
}

代码二:(模仿自"AG"佬)

枚举每一个可能结束的终点,然后尽可能减去之前的长度为1的区间,更新答案即可(不愧是佬)

#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
void solve(){
    int ans=INT_MAX;
    int n,k;
    cin>>n>>k;
    vector<pair<int,int> >a(n);
    for(auto&[x,y]:a)cin>>x;
    for(auto&[x,y]:a)cin>>y;

    int num=0;
    int nn=0;
    for(int i=0;i<n;i++){
        auto [l,r]=a[i];
        num+=(r-l+1);
        if(num>=k){
            int remov=min(nn,num-k);    //能填多少1填多少1
            int res=num-remov;      //涂色的总块数
            int pos=max(l,r-(res-k));       //右边减去不必要涂的块数
            ans=min(ans,pos+2*(i+1-remov));
        }
        nn+=(l==r);     //只填前面的1,所有涂完色,再填一
    }

    if(ans==INT_MAX)ans=-1;
    cout<<ans<<endl;
    return;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int T=1;
    cin>>T;
    while(T--){
        solve();
    }
    return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/13964.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

玩转ChatGPT:论文翻译润色

一、写在前面 首先还是让小Chat推销下自己&#xff1a; 嘿&#xff01;你是否在写论文的过程中感到头疼&#xff0c;无从下手&#xff1f;你是否在担心自己的语言表达不够专业、不够流畅&#xff0c;影响了论文的质量&#xff1f;不要担心&#xff0c;ChatGPT的润色服务可以帮…

Redis 持久化八股文

目录 Redis的持久化机制 持久化方式对比 RDB RDB 持久化 RDB 的优缺点 优点 缺点 RDB 快照时运行修改数据吗 RDB 快照时修改数据过程 写时复制技术 RDB 的执行频率 增量快照 AOF 如何开启AOF AOF 为什么要采用后写日志呢&#xff1f; 后写日志的弊端 AOF 的优…

pdf转成word | ppt | jpg图片,免费一键转换教程

我不允许真的还有人不知道如何免费将pdf转成 ppt、word 或者 jpg图片&#xff01; 职场小伙伴是不是会经常遇到pdf怎么转成word&#xff0c;pdf怎么转成word&#xff0c;pdf怎么jpg图片等问题&#xff1f;别再为pdf转化格式难、而且还要付费而发愁了&#xff01;这份pdf免费一…

Python OpenCV3 计算机视觉秘籍:6~9

原文&#xff1a;OpenCV 3 Computer Vision with Python Cookbook 协议&#xff1a;CC BY-NC-SA 4.0 译者&#xff1a;飞龙 本文来自【ApacheCN 计算机视觉 译文集】&#xff0c;采用译后编辑&#xff08;MTPE&#xff09;流程来尽可能提升效率。 当别人说你没有底线的时候&…

IDAPython入门基础语法

文章目录 参考文章IDAPython简介常用函数获取界面地址的函数数值获取函数数值判断函数patch操作函数去除花指令实例 参考文章 IDAPython入门教程 基于IDA7.5_Python3 第一讲 简介与地址获取 IDAPython简介 IDAPython拥有强大的功能,在使用IDA分析程序时非常有用,可以简化许多…

QT 插件通信接口调用 CTK开发(四)

CTK 为支持生物医学图像计算的公共开发包,其全称为 Common Toolkit。为医学成像提供一组统一的基本功能;促进代码和数据的交互及结合;避免重复开发;在工具包(医学成像)范围内不断扩展到新任务,而不会增加现有任务的负担;整合并适应成功的解决方案。 本专栏文章较为全面…

信息安全复习三:古典密码之设计好的密码算法

一.章节梗概 讨论以下算法&#xff0c;理解怎么设计好的密码算法的关键问题 1.Caesar cipher 2.单字母表密码 3.Playfairmima 4.维吉尼亚密码 5.自动生成密码 二.Caesar cipher 2.1 穷举攻击 穷举攻击定义&#xff1a;尝试所有密钥直到有一个合法密钥能够把密文还原成明文&…

Docker私有仓库Harbor搭建及使用

文章目录 一、Harbor简介二、Harbor仓库部署三、Harbor仓库使用 一、Harbor简介 官网地址&#xff1a;https://github.com/goharbor/harbor Docker容器应用的开发和运行离不开可靠的镜像管理&#xff0c;虽然Docker官方也提供了公共的镜像仓库&#xff0c;但是从安全和效率等…

如何在Web上实现激光点云数据在线浏览和展示?

无人机激光雷达测量是一项综合性较强的应用系统&#xff0c;具有数据精度高、层次细节丰富、全天候作业等优势&#xff0c;能够精确测量三维现实世界&#xff0c;为各个行业提供了丰富有效的数据信息。但无人机激光雷达测量产生的点云数据需要占用大量的存储空间&#xff0c;甚…

SpringSecurity之权限模块设计

目录 前言 实现思路 代码结构 使用说明 前言 前面我们了解了关于微服务权限设计方案以及J W T的相关介绍&#xff0c;今天我们来聊一下&#xff0c;如何避免自己重复的写相同的代码&#xff0c;一次代码实现&#xff0c;即可完美复制到任何项目中实现权限相关的功能。 实现…

RK3568平台开发系列讲解(驱动基础篇)SMP(Symmetrical Multi-Processing)

🚀返回专栏总目录 文章目录 一、linux SMP 和 AMP二、linux SMP的启动流程三、CPU的描述:cpumask四、CPU之间的关系沉淀、分享、成长,让自己和他人都能有所收获!😄 📢本篇将介绍 SMP(Symmetrical Multi-Processing)。 一、linux SMP 和 AMP 目前支持多核处理器的实时操…

CxImage学习使用1:环境搭建

目录 前言 一、CxImage相关介绍 二、编译源码 三、将CxImage使用到自己的工程中 前言 CxImage是一个可以用于MFC 的C图像处理类库类&#xff0c;它可以打开&#xff0c;保存&#xff0c;显示&#xff0c;转换各种常见格式的图像文件&#xff0c;比如BMP, JPEG, GIF, PNG, TI…

300元的蓝牙耳机什么牌子好?300内无线蓝牙耳机推荐

感受过无线的自在舒适后&#xff0c;越来越多的小伙伴爱上了蓝牙耳机白天出街更潇洒&#xff0c;目前市面上蓝牙耳机琳琅满目可选择性较多价格从几十、几百元到数千元不等然而蓝牙耳机的安全性、舒适性如何&#xff1f;连接稳吗&#xff1f;下面整理了几款300元价位的耳机分享给…

【CSDN周赛】第46期题解

&#x1f468;‍&#x1f4bb;个人主页&#xff1a;花无缺 欢迎 点赞&#x1f44d; 收藏⭐ 留言&#x1f4dd; 加关注✅! 本文由 花无缺 原创 本文章收录于专栏 【CSDN周赛】 本篇文章目录 &#x1f30f;一、吃吃吃&#x1f338;题目描述&#x1f338;题解 &#x1f30f;二、n …

Java核心技术 卷1-总结-12

Java核心技术 卷1-总结-12 具体的集合链表数组列表 具体的集合 下表中除了以 Map结尾的类之外&#xff0c; 其他类都实现了 Collection 接口&#xff0c;而以 Map结尾的类实现了 Map 接口。 集合类型描述ArrayList一种可以动态增长和缩减的索引序列LinkedList一种可以在任何位…

MySQL高级篇——索引的创建与设计原则

导航&#xff1a; 【黑马Java笔记踩坑汇总】JavaSEJavaWebSSMSpringBoot瑞吉外卖SpringCloud黑马旅游谷粒商城学成在线牛客面试题 目录 一、索引的分类与使用 1.1 索引的分类 1.1.1. 普通索引 1.1.2. 唯一性索引 1.1.3. 主键索引&#xff08;唯一非空&#xff09; 1.1.4…

百度ai智能写作工具-百度ai自动写文章

百度AI智能写作工具&#xff1a;让创作更快捷、高效&#xff01; 在当今竞争激烈的文化创意市场中&#xff0c;创作一篇高质量的文章需要投入大量时间和精力。然而&#xff0c;有了百度AI智能写作工具&#xff0c;创作变得更快捷、高效了。 百度AI智能写作工具采用最先进的人…

数据可视化神器!Matplotlib Python教程 | 从入门到精通绘制各种类型的图形和保存图形

大家好&#xff0c;我是爱吃熊掌的鱼&#xff0c;今天我要给大家带来一篇有趣开朗的Matplotlib Python教程。Matplotlib是Python中最流行的数据可视化库之一&#xff0c;它可以帮助我们将数据转化为易于理解的图表和图形。无论你是初学者还是专业人士&#xff0c;Matplotlib都是…

ThreadPoolExecutor源码阅读流程图

1.创建线程池 public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue<Runnable> workQueue) {this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,Executors.defaultThreadFactory(), def…
最新文章