赞
踩
写在前面: 疫情严重,希望武汉与中国一起加油。
这次就做出两题,第三题没读完,实在是比较慢。
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; }
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; }
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; } }
t3=
“e”:
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"); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。