Codeforces Round #616 (Div. 2)(题解)

在这里插入图片描述
写在前面: 疫情严重,希望武汉与中国一起加油。
这次就做出两题,第三题没读完,实在是比较慢。

A. Even But Not Even

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Let’s define a number ebne (even but not even) if and only if its sum of digits is divisible by 2 but the number itself is not divisible by 2. For example, 13, 1227, 185217 are ebne numbers, while 12, 2, 177013, 265918 are not. If you’re still unsure what ebne numbers are, you can look at the sample notes for more clarification.

You are given a non-negative integer s, consisting of n digits. You can delete some digits (they are not necessary consecutive/successive) to make the given number ebne. You cannot change the order of the digits, that is, after deleting the digits the remaining digits collapse. The resulting number shouldn’t contain leading zeros. You can delete any number of digits between 0 (do not delete any digits at all) and n−1.

For example, if you are given s=222373204424185217171912 then one of possible ways to make it ebne is: 222373204424185217171912 → 2237344218521717191. The sum of digits of 2237344218521717191 is equal to 70 and is divisible by 2, but number itself is not divisible by 2: it means that the resulting number is ebne.

Find any resulting number that is ebne. If it’s impossible to create an ebne number from the given number report about it.

Input
The input consists of multiple test cases. The first line contains a single integer t (1≤t≤1000) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer n (1≤n≤3000) — the number of digits in the original number.

The second line of each test case contains a non-negative integer number s, consisting of n digits.

It is guaranteed that s does not contain leading zeros and the sum of n over all test cases does not exceed 3000.

Output
For each test case given in the input print the answer in the following format:

If it is impossible to create an ebne number, print “-1” (without quotes);
Otherwise, print the resulting number after deleting some, possibly zero, but not all digits. This number should be ebne. If there are multiple answers, you can print any of them. Note that answers with leading zeros or empty strings are not accepted. It’s not necessary to minimize or maximize the number of deleted digits.
Example
inputCopy
4
4
1227
1
0
6
177013
24
222373204424185217171912
outputCopy
1227
-1
17703
2237344218521717191
Note
In the first test case of the example, 1227 is already an ebne number (as 1+2+2+7=12, 12 is divisible by 2, while in the same time, 1227 is not divisible by 2) so we don’t need to delete any digits. Answers such as 127 and 17 will also be accepted.

In the second test case of the example, it is clearly impossible to create an ebne number from the given number.

In the third test case of the example, there are many ebne numbers we can obtain by deleting, for example, 1 digit such as 17703, 77013 or 17013. Answers such as 1701 or 770 will not be accepted as they are not ebne numbers. Answer 013 will not be accepted as it contains leading zeroes.

Explanation:

1+7+7+0+3=18. As 18 is divisible by 2 while 17703 is not divisible by 2, we can see that 17703 is an ebne number. Same with 77013 and 17013;
1+7+0+1=9. Because 9 is not divisible by 2, 1701 is not an ebne number;
7+7+0=14. This time, 14 is divisible by 2 but 770 is also divisible by 2, therefore, 770 is not an ebne number.
In the last test case of the example, one of many other possible answers is given. Another possible answer is: 222373204424185217171912 → 22237320442418521717191 (delete the last digit).
思路: 把输入的数去掉几位后得到数字不能整除2,但各位之和可以,直接记录前缀和,如果某一位的前缀和是偶数且当前位是奇数则记录位置,最后把位置之前的输出即可。如果没有则输出-1.
代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <cmath>
 
using namespace std;
 
#define endl '\n'
 
typedef long long ll;
 
int main()
{
    int t;
    cin >> t;
 
    while (t--)
    {
        int n;
        cin >> n;
 
        char a[3003];
        int b[3003];
 
        int f = -1;
 
        for (int i = 1; i <= n; ++i)
        {
            cin >> a[i];
            b[i] = b[i - 1] + a[i] - '0';
 
            if ((a[i] - '0') % 2 == 1 && b[i] % 2 == 0)
                f = i;
        }
 
        if (f == -1)
            cout << "-1" << endl;
        else
        {
            for (int i = 1; i <= f; ++i)
            {
                cout << a[i];
            }
            cout << endl;
        }
    }
 
    return 0;
}

B. Array Sharpening

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You’re given an array a1,…,an of n non-negative integers.

Let’s call it sharpened if and only if there exists an integer 1≤k≤n such that a1<a2<…ak+1>…>an. In particular, any strictly increasing or strictly decreasing array is sharpened. For example:

The arrays [4], [0,1], [12,10,8] and [3,11,15,9,7,4] are sharpened;
The arrays [2,8,2,8,6,5], [0,1,1,0] and [2,5,6,9,8,8] are not sharpened.
You can do the following operation as many times as you want: choose any strictly positive element of the array, and decrease it by one. Formally, you can choose any i (1≤i≤n) such that ai>0 and assign ai:=ai−1.

Tell if it’s possible to make the given array sharpened using some number (possibly zero) of these operations.

Input
The input consists of multiple test cases. The first line contains a single integer t (1≤t≤15 000) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer n (1≤n≤3⋅105).

The second line of each test case contains a sequence of n non-negative integers a1,…,an (0≤ai≤109).

It is guaranteed that the sum of n over all test cases does not exceed 3⋅105.

Output
For each test case, output a single line containing “Yes” (without quotes) if it’s possible to make the given array sharpened using the described operations, or “No” (without quotes) otherwise.

Example
inputCopy
10
1
248618
3
12 10 8
6
100 11 15 9 7 8
4
0 1 1 0
2
0 0
2
0 1
2
1 0
2
1 1
3
0 1 0
3
1 0 1
outputCopy
Yes
Yes
Yes
No
No
Yes
Yes
Yes
Yes
No
Note
In the first and the second test case of the first test, the given array is already sharpened.

In the third test case of the first test, we can transform the array into [3,11,15,9,7,4] (decrease the first element 97 times and decrease the last element 4 times). It is sharpened because 3<11<15 and 15>9>7>4.

In the fourth test case of the first test, it’s impossible to make the given array sharpened.
思路: 找出这个数列是不是严格先增后减。
1、全部递减
2、先增后减,找到转折点位置,因为可以减任意个1,所以可大不可小,后面都要大于n - i.否则就不够数字,会出现重复。
3、注意n为偶数时,0 1 1 0,确保转折位置如果和下标一致则不能成立。
代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <cmath>
 
using namespace std;
 
#define endl '\n'
 
typedef long long ll;
 
ll a[300005];
 
int main()
{
    int kk;
    scanf("%d", &kk);
 
 
    while (kk--)
    {
        int f = -1;
        int fl = 0;
        int fg = 0;
        int n;
        scanf("%d", &n);
        for (int i = 0; i < n; ++i)
        {
            int k;
            cin >> k;
            a[i] = k;
 
            if (k < n - 1 - i)
                fg = 1;
            if (k < i && f == -1)
                f = i - 1;
 
            if (f != -1)
            {
                if (n - i - 1 > k)
                    fl = 1;
            }
 
            if (n  % 2 == 0 && f == n / 2 - 1 && a[f] == f)
                fl = 1;
        }
 
        if (fl == 1 && fg == 1)
            cout << "No" << endl;
        else
            cout << "Yes" << endl;
    }
 
    return 0;
}

C. Mind Control

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You and your
n−1
n−1
friends have found an array of integers
a
1
,
a
2
,…,
a
n
a1,a2,…,an
. You have decided to share it in the following way: All
n
n
of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the
m
m
-th position in the line. Before the process starts, you may choose up to
k
k
different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you’re doing your choices optimally. What is the greatest integer
x
x
such that, no matter what are the choices of the friends you didn’t choose to control, the element you will take from the array will be greater than or equal to
x
x
?
Please note that the friends you don’t control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
Input
The input consists of multiple test cases. The first line contains a single integer
t
t
(
1≤t≤1000
1≤t≤1000
) — the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers
n
n
,
m
m
and
k
k
(
1≤m≤n≤3500
1≤m≤n≤3500
,
0≤k≤n−1
0≤k≤n−1
) — the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains
n
n
positive integers
a
1
,
a
2
,…,
a
n
a1,a2,…,an
(
1≤
a
i

10
9
1≤ai≤109
) — elements of the array.
It is guaranteed that the sum of
n
n
over all test cases does not exceed
3500
3500
.
Output
For each test case, print the largest integer
x
x
such that you can guarantee to obtain at least
x
x
.
Example
Input
Copy
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
Copy
8
4
1
1
Note
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element.
the first person will take the last element (
5
5
) because he or she was forced by you to take the last element. After this turn the remaining array will be
[2,9,2,3,8]
[2,9,2,3,8]
;
the second person will take the first element (
2
2
) because he or she was forced by you to take the first element. After this turn the remaining array will be
[9,2,3,8]
[9,2,3,8]
;
if the third person will choose to take the first element (
9
9
), at your turn the remaining array will be
[2,3,8]
[2,3,8]
and you will take
8
8
(the last element);
if the third person will choose to take the last element (
8
8
), at your turn the remaining array will be
[9,2,3]
[9,2,3]
and you will take
9
9
(the first element).
Thus, this strategy guarantees to end up with at least
8
8
. We can prove that there is no strategy that guarantees to end up with at least
9
9
. Hence, the answer is
8
8
.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with
4
4
.

#include <bits/stdc++.h>
using namespace std;
int a[100000];
int c=1e9+1;
int main(){
	int t;
	cin>>t;
	while(t--){
		int ans=0;
		int n,m,k;
		cin>>n>>m>>k;
		k=min(k,m-1);
		for(int i=0;i<n;i++) cin>>a[i];
		for(int i=0;i<=k;i++){
			c=1e9+1;
			for(int j=0;j<m-k;j++){
				 c=min(c,max(a[i+j],a[i+j+n-m]));
			}
			ans=max(ans,c);
		}
		cout<<ans<<'\n';
		ans=0;
	}
}

D. Irreducible Anagrams

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Let’s call two strings
s
s
and
t
t
anagrams of each other if it is possible to rearrange symbols in the string
s
s
to get a string, equal to
t
t
.
Let’s consider two strings
s
s
and
t
t
which are anagrams of each other. We say that
t
t
is a reducible anagram of
s
s
if there exists an integer
k≥2
k≥2
and
2k
2k
non-empty strings
s
1
,
t
1
,
s
2
,
t
2
,…,
s
k
,
t
k
s1,t1,s2,t2,…,sk,tk
that satisfy the following conditions:
If we write the strings
s
1
,
s
2
,…,
s
k
s1,s2,…,sk
in order, the resulting string will be equal to
s
s
;
If we write the strings
t
1
,
t
2
,…,
t
k
t1,t2,…,tk
in order, the resulting string will be equal to
t
t
;
For all integers
i
i
between
1
1
and
k
k
inclusive,
s
i
si
and
t
i
ti
are anagrams of each other.
If such strings don’t exist, then
t
t
is said to be an irreducible anagram of
s
s
. Note that these notions are only defined when
s
s
and
t
t
are anagrams of each other.
For example, consider the string
s=
s=
“gamegame”. Then the string
t=
t=
“megamage” is a reducible anagram of
s
s
, we may choose for example
s
1

s1=
“game”,
s
2

s2=
“gam”,
s
3

s3=
“e” and
t
1

t1=
“mega”,
t
2

t2=
“mag”,
t
3

t3=
“e”:

On the other hand, we can prove that
t=
t=
“memegaga” is an irreducible anagram of
s
s
.
You will be given a string
s
s
and
q
q
queries, represented by two integers
1≤l≤r≤|s|
1≤l≤r≤|s|
(where
|s|
|s|
is equal to the length of the string
s
s
). For each query, you should find if the substring of
s
s
formed by characters from the
l
l
-th to the
r
r
-th has at least one irreducible anagram.
Input
The first line contains a string
s
s
, consisting of lowercase English characters (
1≤|s|≤2⋅
10
5
1≤|s|≤2⋅105
).
The second line contains a single integer
q
q
(
1≤q≤
10
5
1≤q≤105
) — the number of queries.
Each of the following
q
q
lines contain two integers
l
l
and
r
r
(
1≤l≤r≤|s|
1≤l≤r≤|s|
), representing a query for the substring of
s
s
formed by characters from the
l
l
-th to the
r
r
-th.
Output
For each query, print a single line containing “Yes” (without quotes) if the corresponding substring has at least one irreducible anagram, and a single line containing “No” (without quotes) otherwise.
Examples
Input
Copy
aaaaa
3
1 1
2 4
5 5
Output
Copy
Yes
No
Yes
Input
Copy
aabbbbbbc
6
1 2
2 4
2 2
1 9
5 7
3 5
Output
Copy
No
Yes
Yes
Yes
No
No
Note
In the first sample, in the first and third queries, the substring is “a”, which has itself as an irreducible anagram since two or more non-empty strings cannot be put together to obtain “a”. On the other hand, in the second query, the substring is “aaa”, which has no irreducible anagrams: its only anagram is itself, and we may choose
s
1

s1=
“a”,
s
2

s2=
“aa”,
t
1

t1=
“a”,
t
2

t2=
“aa” to show that it is a reducible anagram.
In the second query of the second sample, the substring is “abb”, which has, for example, “bba” as an irreducible anagram.

#include<cstdio>
int a[200100][30],Q,l,r;
char S[200100];
int main(){
	scanf("%s",S+1);
	for(int i=1;S[i];i++){
		for(int j=0;j<26;j++)
			a[i][j]=a[i-1][j];
		a[i][S[i]-'a']++;
	}
	scanf("%d",&Q);
	while(Q--){
		scanf("%d%d",&l,&r);
		int cnt=0;
		for(int i=0;i<26;i++)
			if(a[l-1][i]!=a[r][i])cnt++;
		if(cnt>2||S[l]!=S[r]||l==r)puts("Yes");
		else puts("No");
	}
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值