http://codeforces.com/contest/1005
好久没有刷题了···今天做了这一套div3找找手感。
做题还是不能断啊,哎。
A. Tanya and Stairways
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Little girl Tanya climbs the stairs inside a multi-storey building. Every time Tanya climbs a stairway, she starts counting steps from 11 to the number of steps in this stairway. She speaks every number aloud. For example, if she climbs two stairways, the first of which contains 33steps, and the second contains 44 steps, she will pronounce the numbers 1,2,3,1,2,3,41,2,3,1,2,3,4.
You are given all the numbers pronounced by Tanya. How many stairways did she climb? Also, output the number of steps in each stairway.
The given sequence will be a valid sequence that Tanya could have pronounced when climbing one or more stairways.
Input
The first line contains nn (1≤n≤10001≤n≤1000) — the total number of numbers pronounced by Tanya.
The second line contains integers a1,a2,…,ana1,a2,…,an (1≤ai≤10001≤ai≤1000) — all the numbers Tanya pronounced while climbing the stairs, in order from the first to the last pronounced number. Passing a stairway with xx steps, she will pronounce the numbers 1,2,…,x1,2,…,x in that order.
The given sequence will be a valid sequence that Tanya could have pronounced when climbing one or more stairways.
Output
In the first line, output tt — the number of stairways that Tanya climbed. In the second line, output tt numbers — the number of steps in each stairway she climbed. Write the numbers in the correct order of passage of the stairways.
Examples
input
7 1 2 3 1 2 3 4
output
2 3 4
input
4 1 1 1 1
output
4 1 1 1 1
input
5 1 2 3 4 5
output
1 5
input
5 1 2 1 2 1
output
3 2 2 1
水题,注意一下都是1的情况和末尾的处理就好。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
B. Delete from the Left
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given two strings ss and tt. In a single move, you can choose any of two strings and delete the first (that is, the leftmost) character. After a move, the length of the string decreases by 11. You can't choose a string if it is empty.
For example:
You are required to make two given strings equal using the fewest number of moves. It is possible that, in the end, both strings will be equal to the empty string, and so, are equal to each other. In this case, the answer is obviously the sum of the lengths of the initial strings.
Write a program that finds the minimum number of moves to make two given strings ss and tt equal.
Input
The first line of the input contains ss. In the second line of the input contains tt. Both strings consist only of lowercase Latin letters. The number of letters in each string is between 1 and 2⋅1052⋅105, inclusive.
Output
Output the fewest number of moves required. It is possible that, in the end, both strings will be equal to the empty string, and so, are equal to each other. In this case, the answer is obviously the sum of the lengths of the given strings.
Examples
input
test west
output
2
input
codeforces yes
output
9
input
test yes
output
7
input
b ab
output
1
Note
In the first example, you should apply the move once to the first string and apply the move once to the second string. As a result, both strings will be equal to "est".
In the second example, the move should be applied to the string "codeforces" 88 times. As a result, the string becomes "codeforces" →→ "es". The move should be applied to the string "yes" once. The result is the same string "yes" →→ "es".
In the third example, you can make the strings equal only by completely deleting them. That is, in the end, both strings will be empty.
In the fourth example, the first character of the second string should be deleted.
题意:给你两个字符串,每次操作可以把一个串的第一个字符拿走,求最少需要几步可以让两个串相等(最坏的情况就是两个串都拿成空串就相等了)。
思路:就是从末尾开始看两个串有几个字符相等,一直匹配到第一个不相等的字符,再用两个串的总长度减去匹配数就是答案了。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
C. Summarize to the Power of Two
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
A sequence a1,a2,…,ana1,a2,…,an is called good if, for each element aiai, there exists an element ajaj (i≠ji≠j) such that ai+ajai+aj is a power of two (that is, 2d2d for some non-negative integer dd).
For example, the following sequences are good:
Note that, by definition, an empty sequence (with a length of 00) is good.
For example, the following sequences are not good:
You are given a sequence a1,a2,…,ana1,a2,…,an. What is the minimum number of elements you need to remove to make it good? You can delete an arbitrary set of elements.
Input
The first line contains the integer nn (1≤n≤1200001≤n≤120000) — the length of the given sequence.
The second line contains the sequence of integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109).
Output
Print the minimum number of elements needed to be removed from the given sequence in order to make it good. It is possible that you need to delete all nn elements, make it empty, and thus get a good sequence.
Examples
input
6 4 7 1 5 4 9
output
1
input
5 1 2 3 4 5
output
2
input
1 16
output
1
input
4 1 1 1 1023
output
0
Note
In the first example, it is enough to delete one element a4=5a4=5. The remaining elements form the sequence [4,7,1,4,9][4,7,1,4,9], which is good.
题意:给你一段整数序列,问最少需要移除几个数可以让这个序列成为一个“好序列”。一个“好序列”的定义是,这个序列中的每一个数都能找到除了它自身之外的一个数,它俩相加之后是2的幂次方。
思路:这个题给的数据1e5,我想到了n*logn的做法。我用了一个power数组存的2的n次方。每次用power[j]和a[i]作差后的值通过二分在a中寻找有没有相同的,找到了给vis[i]赋1,代表这个数不用被删除。最后统计vis里面有几个0就是答案。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
D. Polycarp and Div 3
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Polycarp likes numbers that are divisible by 3.
He has a huge number ss. Polycarp wants to cut from it the maximum number of numbers that are divisible by 33. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after mm such cuts, there will be m+1m+1 parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by 33.
For example, if the original number is s=3121s=3121, then Polycarp can cut it into three parts with two cuts: 3|1|213|1|21. As a result, he will get two numbers that are divisible by 33.
Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid.
What is the maximum number of numbers divisible by 33 that Polycarp can obtain?
Input
The first line of the input contains a positive integer ss. The number of digits of the number ss is between 11 and 2⋅1052⋅105, inclusive. The first (leftmost) digit is not equal to 0.
Output
Print the maximum number of numbers divisible by 33 that Polycarp can get by making vertical cuts in the given number ss.
Examples
input
3121
output
2
input
6
output
1
input
1000000000000000000000000000000000
output
33
input
201920181
output
4
Note
In the first example, an example set of optimal cuts on the number is 3|1|21.
In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by 33.
In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and 3333 digits 0. Each of the 3333digits 0 forms a number that is divisible by 33.
In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers 00, 99, 201201 and 8181 are divisible by 33.
题意:给你一个长度在1e5数量级的整数,你可以把它们从中间切开,要求剩下的部分是3的倍数的字串长度最多能多少个?不能包含前导0,但是单独的0也算3的倍数。
思路:一个数可以整除3,那么这个数的各个位数相加是3的倍数。所以我们用一个变量cnt从头加到尾,用长度为3的ans数组存放cnt当前余3的值,这里参考抽屉原理。如果cnt余3后ans[cnt%3]!=0那么就代表我们刚才遍历的子串是3的倍数。此时把ans变为初始值。因为cnt一开始为0,所以ans初始值应为{1,0,0}。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
E1. Median on Segments (Permutations Edition)
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given a permutation p1,p2,…,pnp1,p2,…,pn. A permutation of length nn is a sequence such that each integer between 11 and nn occurs exactly once in the sequence.
Find the number of pairs of indices (l,r)(l,r) (1≤l≤r≤n1≤l≤r≤n) such that the value of the median of pl,pl+1,…,prpl,pl+1,…,pr is exactly the given number mm.
The median of a sequence is the value of the element which is in the middle of the sequence after sorting it in non-decreasing order. If the length of the sequence is even, the left of two middle elements is used.
For example, if a=[4,2,7,5]a=[4,2,7,5] then its median is 44 since after sorting the sequence, it will look like [2,4,5,7][2,4,5,7] and the left of two middle elements is equal to 44. The median of [7,1,2,9,6][7,1,2,9,6] equals 66 since after sorting, the value 66 will be in the middle of the sequence.
Write a program to find the number of pairs of indices (l,r)(l,r) (1≤l≤r≤n1≤l≤r≤n) such that the value of the median of pl,pl+1,…,prpl,pl+1,…,pr is exactly the given number mm.
Input
The first line contains integers nn and mm (1≤n≤2⋅1051≤n≤2⋅105, 1≤m≤n1≤m≤n) — the length of the given sequence and the required value of the median.
The second line contains a permutation p1,p2,…,pnp1,p2,…,pn (1≤pi≤n1≤pi≤n). Each integer between 11 and nn occurs in pp exactly once.
Output
Print the required number.
Examples
input
Copy
5 4 2 4 5 3 1
output
Copy
4
input
Copy
5 5 1 2 3 4 5
output
Copy
1
input
Copy
15 8 1 15 2 14 3 13 4 8 12 5 11 6 10 7 9
output
Copy
48
Note
In the first example, the suitable pairs of indices are: (1,3)(1,3), (2,2)(2,2), (2,3)(2,3) and (2,4)(2,4).
题意:给你一个长度为n的序列和一个数m,长度为n的序列里包含从1-n的各个数。问以m为中位数的子段有几个。即取出一个子段从小到大排序后m是中位数,如果元素为偶数个,m则是中间左边的数。
思路:参考了别人的想法,直接上代码吧:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include