Case of Matryoshkas

题面翻译

这是一套有n个娃娃的套娃,由1到n依次编号。编号小的娃娃可以放进编号大的娃娃里面,但是不能平行的放入两个娃娃,即套娃在嵌套放置的时候只能一个套一个例如1 → 2 → 4 → 5。

又两个操作

  1. 选择两个娃娃a,b,要求a<b,a没有被其他的娃娃套起来,b既没有被其他娃娃套起来,也没有套其他娃娃。然后把a放到b里面。

  2. 选择两个娃娃a,b,要求a<b,a已经被b套起来,b没有被其他娃娃套起来。然后把a从b里面拿出来。

现在这n个娃娃相互嵌套成了k个套娃摆在桌上,Andrewid想知道最少多少次操作可以把这n个娃娃嵌套成1个套娃,即1→2→…→n-1→n。

1 < = k < = n < = 1 0 5 1<=k<=n<=10^5 1<=k<=n<=105

题目描述

Andrewid the Android is a galaxy-famous detective.

He is now investigating the case of vandalism at the exhibition of contemporary art.

The main exhibit is a construction of n n n matryoshka dolls that can be nested one into another.

The matryoshka dolls are numbered from 1 1 1 to n n n .

A matryoshka with a smaller number can be nested in a matryoshka with a higher number, two matryoshkas can not be directly nested in the same doll, but there may be chain nestings, for example, 1 → 2 → 4 → 5 1→2→4→5 1→2→4→5 .

In one second, you can perform one of the two following operations:

  • Having a matryoshka a a a that isn’t nested in any other matryoshka and a matryoshka b b b , such that b b b doesn’t contain any other matryoshka and is not nested in any other matryoshka, you may put a a a in b b b ;
  • Having a matryoshka a a a directly contained in matryoshka b b b , such that b b b is not nested in any other matryoshka, you may get a a a out of b b b .

According to the modern aesthetic norms the matryoshka dolls on display were assembled in a specific configuration, i.e. as several separate chains of nested matryoshkas, but the criminal, following the mysterious plan, took out all the dolls and assembled them into a single large chain ( 1 → 2 → . . . → n 1→2→...→n 1→2→...→n ).

In order to continue the investigation Andrewid needs to know in what minimum time it is possible to perform this action.

输入格式

The first line contains integers n n n ( 1 < = n < = 1 0 5 1<=n<=10^{5} 1<=n<=105 ) and k k k ( 1 < = k < = 1 0 5 1<=k<=10^{5} 1<=k<=105 ) — the number of matryoshkas and matryoshka chains in the initial configuration.

The next k k k lines contain the descriptions of the chains: the i i i -th line first contains number m i m_{i} mi​ ( 1 < = m i < = n 1<=m_{i}<=n 1<=mi​<=n ), and then m i m_{i} mi​ numbers a i 1 , a i 2 , . . . , a i m i a_{i1},a_{i2},...,a_{imi} ai1​,ai2​,...,aimi​ — the numbers of matryoshkas in the chain (matryoshka a i 1 a_{i1} ai1​ is nested into matryoshka a i 2 a_{i2} ai2​ , that is nested into matryoshka a i 3 a_{i3} ai3​ , and so on till the matryoshka a i m i a_{imi} aimi​ that isn’t nested into any other matryoshka).

It is guaranteed that m 1 + m 2 + . . . + m k = n m_{1}+m_{2}+...+m_{k}=n m1​+m2​+...+mk​=n , the numbers of matryoshkas in all the chains are distinct, in each chain the numbers of matryoshkas follow in the ascending order.

输出格式

In the single line print the minimum number of seconds needed to assemble one large chain from the initial configuration.

样例 #1

样例输入 #1

3 2
2 1 2
1 3

样例输出 #1

1

样例 #2

样例输入 #2

7 3
3 1 3 7
2 2 5
2 4 6

样例输出 #2

10

提示

In the first sample test there are two chains: 1 → 2 1→2 1→2 and 3 3 3 .

In one second you can nest the first chain into the second one and get 1 → 2 → 3 1→2→3 1→2→3 .

In the second sample test you need to disassemble all the three chains into individual matryoshkas in 2 + 1 + 1 = 4 seconds and then assemble one big chain in 6 seconds.

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
typedef long long ll;
int n,k,m,f,one,x,p=1,s=0;
int main(){cin>>n>>k;for(int i=1;i<=k;i++){scanf("%d%d",&m,&f);if(f==1){for(int ii=2;ii<=m;ii++){scanf("%d",&x);if(x==ii) p++;}s+=m-p;}else{for(int iii=1;iii<m;iii++){scanf("%d",&x);}s+=m-1;}}s+=n-p;cout<<s<<endl;return 0;
}

Two Substrings

题面翻译

给你一个字符串 S S S( 1 ≤ ∣ S ∣ ≤ 1 0 4 1 \leq |S| \leq 10^4 1≤∣S∣≤104),判断 S S S 中是否有不相交的 B A BA BA, A B AB AB 这两个子串。如果存在,输出 Yes,否则输出 No

题目描述

You are given string s s s .

Your task is to determine if the given string s s s contains two non-overlapping substrings “AB” and “BA” (the substrings can go in any order).

输入格式

The only line of input contains a string s s s of length between 1 1 1 and 1 0 5 10^{5} 105 consisting of uppercase Latin letters.

输出格式

Print “YES” (without the quotes), if string s s s contains two non-overlapping substrings “AB” and “BA”, and “NO” otherwise.

样例 #1

样例输入 #1

ABA

样例输出 #1

NO

样例 #2

样例输入 #2

BACFAB

样例输出 #2

YES

样例 #3

样例输入 #3

AXBYBXA

样例输出 #3

NO

提示

In the first sample test, despite the fact that there are substrings “AB” and “BA”, their occurrences overlap, so the answer is “NO”.

In the second sample test there are the following occurrences of the substrings: BACFAB.

In the third sample test there is no substring “AB” nor substring “BA”.

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
typedef long long ll;
string s,ss;
int k;
int main(){cin>>s;ss=s;k=s.find("AB");if(k!=-1){s.replace(k,2,"SSS");k=s.find("BA");if(k!=-1){puts("YES");return 0;} }k=ss.find("BA");if(k!=-1){ss.replace(k,2,"SSS");k=ss.find("AB");if(k!=-1){puts("YES");return 0;}   }puts("NO");return 0;
}

Removing Columns

题面翻译

题目描述:

给你一个 n n n* m m m的小写字符矩阵,你可以选择删除一些列,使得剩下的字符矩阵的每一行的字符串从上到下的字典序非减。

求最少删除多少列

输入格式:

第一行输入两个整数n,m
接下来n行每行输入m个字符

输出格式:

输出一个整数表示最少删除的列

题目描述

You are given an n × m n×m n×m rectangular table consisting of lower case English letters.

In one operation you can completely remove one column from the table.

The remaining parts are combined forming a new table.

For example, after removing the second column from the table

<br></br>abcd<br></br>edfg<br></br>hijk<br></br>we obtain the table:

<br></br>acd<br></br>efg<br></br>hjk<br></br>A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one.

Determine the minimum number of operations of removing a column needed to make a given table good.

输入格式

The first line contains two integers — n n n and m m m ( 1 < = n , m < = 1<=n,m<= 1<=n,m<= 100).

Next n n n lines contain m m m small English letters each — the characters of the table.

输出格式

Print a single number — the minimum number of columns that you need to remove in order to make the table good.

样例 #1

样例输入 #1

1 10
codeforces

样例输出 #1

0

样例 #2

样例输入 #2

4 4
case
care
test
code

样例输出 #2

2

样例 #3

样例输入 #3

5 4
code
forc
esco
defo
rces

样例输出 #3

4

提示

In the first sample the table is already good.

In the second sample you may remove the first and third column.

In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).

Let strings s s s and t t t have equal length.

Then, s s s is lexicographically larger than t t t if they are not equal and the character following the largest common prefix of s s s and t t t (the prefix may be empty) in s s s is alphabetically larger than the corresponding character of t t t .

#include<bits/stdc++.h>
using namespace std;
const int N=1e3+10;
typedef long long ll;
string s[N];
int n,m,ss=0;
void clean(int p){for(int pp=1;pp<=n;pp++){s[pp][p]=' ';}
}
int main(){cin>>n>>m;for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){cin>>s[i][j];  }   }for(int i=1;i<n;i++){for(int j=1;j<=m;j++){if(s[i+1][j]<s[i][j]) break;else if(s[i+1][j]>s[i][j]){clean(j);ss++;i=1,j=1;}}}cout<<ss<<endl;return 0;
}

Treasure

题面翻译

得你一个包含"(“和”#"的序列,'#‘需要转换为k个右括号 ( k > 0 ),那么这个表达式的括号能否在你转化后匹配,如果可以,输出每一个’#'转化的右括号数 k 的值;如果不能,就输出 −1

可能有很多组满足的情况只需输出一组即可

题目描述

Malek has recently found a treasure map.

While he was looking for a treasure he found a locked door.

There was a string s s s written on the door consisting of characters ‘(’, ‘)’ and ‘#’.

Below there was a manual on how to open the door.

After spending a long time Malek managed to decode the manual and found out that the goal is to replace each ‘#’ with one or more ‘)’ characters so that the final string becomes beautiful.

Below there was also written that a string is called beautiful if for each i i i ( 1 < = i < = ∣ s ∣ 1<=i<=|s| 1<=i<=∣s∣ ) there are no more ‘)’ characters than ‘(’ characters among the first i i i characters of s s s and also the total number of ‘(’ characters is equal to the total number of ‘)’ characters.

Help Malek open the door by telling him for each ‘#’ character how many ‘)’ characters he must replace it with.

输入格式

Malek has recently found a treasure map.

While he was looking for a treasure he found a locked door.

There was a string s s s written on the door consisting of characters ‘(’, ‘)’ and ‘#’.

Below there was a manual on how to open the door.

After spending a long time Malek managed to decode the manual and found out that the goal is to replace each ‘#’ with one or more ‘)’ characters so that the final string becomes beautiful.

Below there was also written that a string is called beautiful if for each i i i ( 1 < = i < = ∣ s ∣ 1<=i<=|s| 1<=i<=∣s∣ ) there are no more ‘)’ characters than ‘(’ characters among the first i i i characters of s s s and also the total number of ‘(’ characters is equal to the total number of ‘)’ characters.

Help Malek open the door by telling him for each ‘#’ character how many ‘)’ characters he must replace it with.

输出格式

Malek has recently found a treasure map.

While he was looking for a treasure he found a locked door.

There was a string s s s written on the door consisting of characters ‘(’, ‘)’ and ‘#’.

Below there was a manual on how to open the door.

After spending a long time Malek managed to decode the manual and found out that the goal is to replace each ‘#’ with one or more ‘)’ characters so that the final string becomes beautiful.

Below there was also written that a string is called beautiful if for each i i i ( 1 < = i < = ∣ s ∣ 1<=i<=|s| 1<=i<=∣s∣ ) there are no more ‘)’ characters than ‘(’ characters among the first i i i characters of s s s and also the total number of ‘(’ characters is equal to the total number of ‘)’ characters.

Help Malek open the door by telling him for each ‘#’ character how many ‘)’ characters he must replace it with.

样例 #1

样例输入 #1

(((#)((#)

样例输出 #1

1
2

样例 #2

样例输入 #2

()((#((#(#()

样例输出 #2

2
2
1

样例 #3

样例输入 #3

#

样例输出 #3

-1

样例 #4

样例输入 #4

(#)

样例输出 #4

-1

提示

∣ s ∣ |s| ∣s∣ denotes the length of the string s s s .

#include<bits/stdc++.h>
using namespace std;
const int N=2e6+10;
typedef long long ll;
string s;
int n,k,l=0,r=0,ss=0,lll;
int main(){cin>>s;lll=s.size();for(int i=0;i<lll;i++){if(s[i]=='('){l++;}if(s[i]==')'){l--;}if(s[i]=='#'){l--;ss++;k=i;}if(l<0){puts("-1");return 0;}}for(int i=k+1;i<lll;i++){if(s[i]=='(') r++;if(s[i]==')') r--;r=max(r,0);}if(r>0){puts("-1");return 0;}for(int i=1;i<ss;i++) puts("1");printf("%d",l+1);return 0;
}

Woodcutters

题面翻译

给 n n n 棵树在一维数轴上的坐标 x i x_i xi​,以及它们的长度 h i h_i hi​。现在要你砍倒这些树,树可以向左倒也可以向右倒,砍倒的树不能重合、当然也不能覆盖其他的树原来的位置,现在求最大可以砍倒的树的数目。

1 ≤ n ≤ 10 5 1 \le n \le {10}^5 1≤n≤105

1 ≤ x i , h i ≤ 10 9 1 \le x_i, h_i \le {10}^9 1≤xi​,hi​≤109

题目描述

Little Susie listens to fairy tales before bed every day.

Today’s fairy tale was about wood cutters and the little girl immediately started imagining the choppers cutting wood.

She imagined the situation that is described below.

There are n n n trees located along the road at points with coordinates x 1 , x 2 , . . . , x n x_{1},x_{2},...,x_{n} x1​,x2​,...,xn​ .

Each tree has its height h i h_{i} hi​ .

Woodcutters can cut down a tree and fell it to the left or to the right.

After that it occupies one of the segments [ x i − h i , x i ] [x_{i}-h_{i},x_{i}] [xi​−hi​,xi​] or [ x i ; x i + h i ] [x_{i};x_{i}+h_{i}] [xi​;xi​+hi​] .

The tree that is not cut down occupies a single point with coordinate x i x_{i} xi​ .

Woodcutters can fell a tree if the segment to be occupied by the fallen tree doesn’t contain any occupied point.

The woodcutters want to process as many trees as possible, so Susie wonders, what is the maximum number of trees to fell.

输入格式

The first line contains integer n n n ( 1 < = n < = 1 0 5 1<=n<=10^{5} 1<=n<=105 ) — the number of trees.

Next n n n lines contain pairs of integers x i , h i x_{i},h_{i} xi​,hi​ ( 1 < = x i , h i < = 1 0 9 1<=x_{i},h_{i}<=10^{9} 1<=xi​,hi​<=109 ) — the coordinate and the height of the і і і -th tree.

The pairs are given in the order of ascending x i x_{i} xi​ .

No two trees are located at the point with the same coordinate.

输出格式

Print a single number — the maximum number of trees that you can cut down by the given rules.

样例 #1

样例输入 #1

5
1 2
2 1
5 10
10 9
19 1

样例输出 #1

3

样例 #2

样例输入 #2

5
1 2
2 1
5 10
10 9
20 1

样例输出 #2

4

提示

In the first sample you can fell the trees like that:

  • fell the 1 1 1 -st tree to the left — now it occupies segment [ − 1 ; 1 ] [-1;1] [−1;1]
  • fell the 2 2 2 -nd tree to the right — now it occupies segment [ 2 ; 3 ] [2;3] [2;3]
  • leave the 3 3 3 -rd tree — it occupies point 5 5 5
  • leave the 4 4 4 -th tree — it occupies point 10 10 10
  • fell the 5 5 5 -th tree to the right — now it occupies segment [ 19 ; 20 ] [19;20] [19;20]

In the second sample you can also fell 4 4 4 -th tree to the right, after that it will occupy segment [ 10 ; 19 ] [10;19] [10;19] .

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
typedef long long ll;
ll n,x[N],h[N],s=0;
int main(){cin>>n;for(int i=1;i<=n;i++){scanf("%lld%lld",&x[i],&h[i]);   }for(int i=2;i<=n-1;i++){if(x[i]-h[i]>x[i-1]) s++;else if(x[i]+h[i]<x[i+1]){s++;x[i]+=h[i];}}if(n==1) puts("1");else printf("%lld",s+2);return 0;
}

codeforces每日5题(均1500)-第十三天相关推荐

  1. codeforces每日5题(均1500)-第八天

    Cola 题面翻译 有a瓶0.5升,b瓶1升,c瓶2升的可乐,求买n升可乐的方案数 输入 nnn , aaa , bbb , ccc ( 1<=n<=100001<=n<=10 ...

  2. codeforces每日5题(均1500)-第二十一天

    Walking Robot 题面翻译 Description 在一个数轴上,有一个机器人要从 x=0x = 0x=0 处移动到 x=nx = nx=n 处.机器人身上有两种电池,第一种是普通电池,第二 ...

  3. codeforces每日5题(均1700)-第七天

    Multicolored Cars 题面翻译 [题目描述] 定义cntx(i)cnt_{x}(i)cntx​(i)表示到iii时刻xxx出现过的个数. 现在给出nnn个数a1,a2--ana_{1}, ...

  4. 寒假每日一题题解(1.20)十三号星期五

    十三号星期五 十三号星期五真的很不常见吗? 每个月的十三号是星期五的频率是否比一周中的其他几天低? 请编写一个程序,计算 N 年内每个月的 13 号是星期日,星期一,星期二,星期三,星期四,星期五和星 ...

  5. Codeforces Round #699 (Div. 2) (A ~ F)6题全,超高质量良心题解【每日亿题】2021/2/6

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #699 (Div. 2) (A.B.C)[每日亿题]2021/2/ ...

  6. Codeforces Round #698 (Div. 2)(A ~ F)6题全,超高质量题解)【每日亿题】2021/2/4

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 [每日亿题]Codeforces Round #698 (Div. 2)(A ~ F)6题全,超 ...

  7. Codeforces Round #694 (Div. 1 + Div2)(A ~ H,8题全,超高质量题解)【每日亿题】2021/2/1、2/2

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 [每日亿题]Codeforces Round #694 (Div. 1 + Div2)(A ~ ...

  8. 每日一题——质因数个数(蓝桥杯2022年第十三届省赛真题)

    如何将一个正整数分解质因数:每日一题--将一个正整数分解质因数_笨小古的博客-CSDN博客 题目描述:给定正整数 n,请问有多少个质数是 n 的约数. 输入格式:输入的第一行包含一个整数 n. 输出格 ...

  9. 运维面试题(每日一题)

    每日一题 第一周 1.添加路由 2.如何防止Linux命令行或脚本里MySQL登录密码泄露? 3.将前端运行的服务或脚本,如何可以放置到后端进行执行 4.linux网络配置中如何给一块网卡添加多个IP ...

最新文章

  1. 导航控制器自定义返回控件及手势失效问题
  2. http_build_query的用法
  3. C语言 | 快排双向扫描:快速排序双向扫描分区法(源代码)
  4. 当时我就震惊了:无穷带来的各种悖论
  5. 模型服务:流处理与使用Java,gRPC,Apache Kafka,TensorFlow的RPC / REST
  6. 泛型技巧系列:类型字典和Type Traits
  7. TypeScript入门教程 之 枚举 Enums
  8. 图解Visual Studio 2010中的UML建模功能
  9. typora高亮_用Typora实现写作排版一体化
  10. 浏览器后退不刷新页面
  11. 计算机视觉-混合动态纹理模型(Mixtures of Dynamic Textures)
  12. leetcode之有效的括号
  13. Windows11怎么配置Maven环境变量
  14. 墨画子卿第一章第1节
  15. Sequential Recommendation with Self-Attentive Multi-Adversarial Network
  16. RK61键盘配置方法
  17. For input String: 异常记录
  18. 解决专为旧版android打造
  19. java语言基础知识点
  20. C# 数组内元素合并转换成以指定字符连接的字符串

热门文章

  1. phpMyAdmin必须启用Cookies 才能登录的解决办法
  2. c语言斐波那契数列_斐波那契数列趣闻
  3. 9.1 dfs思想 —— 【迷宫】
  4. javaweb之jsp的内置九大对象(request,response,out,session,application,pageContext,page,config,exception)的说明应用
  5. java printwriter 文件_关于java:如何将PrintWriter转换为String或写入文件?
  6. 重新的找回自己恋爱的感觉
  7. 聚观早报 | 国美电器被申请破产清算;首款太阳能汽车投入生产
  8. 亚马逊平台儿童玩具CPC认证解决方案
  9. 世间不如意事十之八九
  10. 计算机应用基础如何指导实践教学,高职计算机应用基础课实践教学.pdf