昨天打了一轮div2的codeforces,发现div2里面也是有一些差距在的,至少昨天做的很是难受,解题缺少关键思路,可以称得上题题卡壳。
此文的解题方略参考了editorial。
首先是B题,原题如下:
Consider the array a composed of all the integers in the range [l,r]. For example, if l=3 and r=7, then a=[3,4,5,6,7].
Given l, r, and k, is it possible for gcd(a) to be greater than 1 after doing the following operation at most k times?
- Choose 2 numbers from a.
- Permanently remove one occurrence of each of them from the array.
- Insert their product back into a.
gcd(b) denotes the greatest common divisor (GCD) of the integers in b.
The first line of the input contains a single integer t (1≤t≤105) — the number of test cases. The description of test cases follows.
The input for each test case consists of a single line containing 3 non-negative integers l, r, and k (1≤l≤r≤109,0≤k≤r−l).
For each test case, print "YES" if it is possible to have the GCD of the corresponding array greater than 1 by performing at most k operations, and "NO" otherwise (case insensitive).
题目大意是对一个连续非负整数数组进行“选取两个数,将这两个数移除出数组,将他们的乘积添加进数组”的一系列操作,如果在重复不超过k次后,数组内元素的最大公因数大于1,则输出YES,反之为NO。
我做这道题的时候,思路始终被局限在“选取两个数”这一步操作上面,对于如何选取数字没有头绪,事实上这道题如果按照正向思维就容易困在一开始的算法上,为了能够使得最后数组的GCD大于1并且使得操作次数尽可能少,我们应该将初始数组里频次最多的非1因子挑出来,然后将有这个因子和没有这个因子的数相乘。而在这之后,注意到题目剩下的条件,即连续非负整数,可以想到频次最多的非1因子是2。于是,我们只需要将数组元素个数和奇数个数之差与k作类比即可。
然后是C题,原题如下:
Mihai has just learned about the MEX concept and since he liked it so much, he decided to use it right away.
Given an array a of n non-negative integers, Mihai wants to create a new array b that is formed in the following way:
While aa is not empty:
- Choose an integer k (1≤k≤|a|).
- Append the MEX of the first k numbers of the array a to the end of array b and erase them from the array a, shifting the positions of the remaining numbers in a.
But, since Mihai loves big arrays as much as the MEX concept, he wants the new array b to be the lexicographically maximum. So, Mihai asks you to tell him what the maximum array b that can be created by constructing the array optimally is.
An array x is lexicographically greater than an array y if in the first position where x and y differ xi>yi or if |x|>|y| and y is a prefix of x (where |x| denotes the size of the array x).
The MEX of a set of non-negative integers is the minimal non-negative integer such that it is not in the set. For example, MEX({1,2,3}) =0 and MEX({0,1,2,4,5}) =3.
The first line of the input contains a single integer t (1≤t≤100) — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer n (1≤n≤2⋅105) — the number of elements in the array a.
The second line of each test case contains nn non-negative integers a1,…,an (0≤ai≤n), where ai is the i-th integer from the array a.
It is guaranteed that the sum of nn over all test cases does not exceed 2⋅105.
For each test case print m — the length of the maximum array b Mihai can create, followed by m integers denoting the elements of the array b.
题目要求对一个非负整数数组a的前k个元素求他们的MEX,MEX是除了这些元素之外最小的非负整数,然后将这个MEX添加到一个新的数组b中,并移除前k个元素,对剩下的元素重复上述操作,使得数组b的字典序排序最大。
这道题的突破口很明显在于字典序排序上,字典序最大,因此可以用贪心算法。当时我想的是,将所有出现过的非负整数做标记,然后根据没有出现过的数字来确定b,然后这样做会因为先后顺序导致难以判断元素的顺序,以及包含了{0,1,3,4}这样类型的数组会使得3,4两个元素的0难以确定(因为只记录了出现次数)。
事实上我当时没有想到贪心算法,解答应该是,首先将数组整体的MEX求得,然后将数组a逐元素求当前MEX,直到与整体的MEX相等,这就是b数组的第一个元素,可谓豁然开朗。
最后是D题,原题如下:
Mihai plans to watch a movie. He only likes palindromic movies, so he wants to skip some (possibly zero) scenes to make the remaining parts of the movie palindromic.
You are given a list s of n non-empty strings of length at most 3, representing the scenes of Mihai's movie.
A subsequence of s is called awesome if it is non-empty and the concatenation of the strings in the subsequence, in order, is a palindrome.
Can you help Mihai check if there is at least one awesome subsequence of s?
A palindrome is a string that reads the same backward as forward, for example strings "z", "aaa", "aba", "abccba" are palindromes, but strings "codeforces", "reality", "ab" are not.
A sequence a is a non-empty subsequence of a non-empty sequence b if a can be obtained from b by deletion of several (possibly zero, but not all) elements.
The first line of the input contains a single integer t (1≤t≤100) — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer n (1≤n≤105) — the number of scenes in the movie.
Then follows n lines, the i-th of which containing a single non-empty string si of length at most 3, consisting of lowercase Latin letters.
It is guaranteed that the sum of n over all test cases does not exceed 105.
For each test case, print "YES" if there is an awesome subsequence of s, or "NO" otherwise (case insensitive).
对于这道题,我当时犯了一个很严重的错误,居然没有看到字符串长度不超过3(我在赣神魔),然后直接就gg,今天看了editorial才知道这回事。字符串的回文判断比较简单,这道题的难点在于如果枚举法比较,那很容易就导致超时,因此需要缩小范围。
这里首先需要想到如果一组字符串中存在能够构成回文的字符串组,那么首尾两个字符串也能够构成回文,这是充分必要的(只对字符串长度小于等于3成立)。
在想到这一点以后,才能够大大简化运算量。之后先对每个字符串本身判断回文,如果都不是回文,再对于每一个字符串进行判断。分为长度2和长度3两类,判断在他们之前的字符串中是否存在能够构成回文的字符串,如果有,输出YES,如果对于最后一个字符串也不存在能够构成回文的其他字符串,输出NO。
浙公网安备 33010602011771号