CodeChef Starters 157 Solutions –
Glass Prices
Chef is buying spectacles, and is now deciding on which frame to purchase.
Chef has narrowed his options down to two choices: a plastic frame that costs XXrupees, and a metal frame that costs YY rupees.
Chef will buy the metal frame if and only if it costs at most twice the price of the plastic frame – otherwise he will buy the plastic frame.
Can you decide which frame Chef will buy?
Input Format
- The first and only line of input will contain two integers XX and YY – the price of the plastic frame and the metal frame, respectively.
Output Format
For each test case, output on a new line the answer: the type of frame Chef will purchase.
This is either the string "PLASTIC"
or the string "METAL"
(without quotes) depending on the outcome.
Each letter of the output may be printed in either uppercase or lowercase – for example, the strings METAL
, MeTaL
, metal
, and mEtAL
will all be treated as equivalent.
Constraints
- 1≤X≤20001≤X≤2000
- 1≤Y≤20001≤Y≤2000
Sample 1:
Input 499 999
Explanation:
The plastic frame costs 499499 rupees, and the metal one costs 999999 rupees.
2×499=9982×499=998 is less than 999999, so Chef will prefer the plastic frame.
Sample 2:
Input 499 998
Explanation:
The plastic frame costs 499499 rupees, and the metal one costs 998998 rupees.
2×499=9982×499=998 is equal to 998998, so Chef will prefer the metal frame.
Sample 3:
Input 698 1099
Explanation:
The plastic frame costs 698698 rupees, and the metal one costs 10991099 rupees.
2×698=13962×698=1396 is greater than 10991099, so Chef will prefer the metal frame.
Solution in C
#include <stdio.h>
int main() {
int X, Y;
scanf(“%d %d”, &X, &Y);
if (Y <= 2 * X) {
printf(“METAL\n”);
} else {
printf(“PLASTIC\n”);
}
return 0;
}
Non-matching Name
Alice and Bob invented a brand-new algorithm – but they can’t decide what to name it!
Alice suggested the name SASA, and Bob suggested the name SBSB. (SASA and SBSB are both strings of lowercase English letters.)
However, Alice thinks Bob’s naming sense is really bad – she’ll only be happy if the name given to the algorithm is not close to SBSB at all.
More specifically, Alice will be happy if and only if the algorithm’s name does not share any characters with SBSB.
Similarly, Bob thinks Alice’s naming sense is really bad, and will only be happy if the algorithm’s name doesn’t share any characters with SASA.
Is there a way to name the algorithm (using only lowercase English letters) so that both Alice and Bob are happy?
Input Format
- The first line of input will contain a single integer TT, denoting the number of test cases.
- Each test case consists of three lines of input.
- The first line of each test case contains two space-separated integers NN and MM — the length of SASA and the length of SBSB, respectively.
- The second line contains the string SASA.
- The third line contains the string SBSB.
Output Format
For each test case, output on a new line the answer: "YES"
if a valid name exists for the algorithm, and "NO"
otherwise.
Each letter of the output may be printed in either uppercase or lowercase, i.e, the strings NO
, No
, nO
, and no
will all be treated as equivalent.
Constraints
- 1≤T≤10001≤T≤1000
- 1≤N≤501≤N≤50
- 1≤M≤501≤M≤50
- SASA and SBSB contain only lowercase English letters, i.e, the characters
'a'
through'z'
.
Sample 1:
Input
Output
3 8 7 dijkstra blossom 24 19 fastquicklazypropagation westmajixhoverboard 34 45 supercalifragilisticexpialidocious pneumonoultramicroscopicsilicovolcanoconiosis
Yes No Yes
Explanation:
Test case 11: We have SA=dijkstraSA=dijkstra and SB=blossomSB=blossom.
They can name the algorithm queenqueen for example – you may verify that it shares no characters with both SASA and SBSB.
Many other names are possible too – some examples are puff, yen, newpuff, yen, new.
Test case 22: It can be shown that no matter what the algorithm is named, either Alice or Bob will be unhappy.
Test case 33: One valid name for the algorithm is zzzzzz.
Solution in C++
#include <bits/stdc++.h>
using namespace std;
void solve() {
int t;
cin >> t;
while (t–) {
int n,m;
cin >> n >>m ;
string s1, s2;
cin >> s1 >> s2;
int arr1[26] = {0};
int arr2[26] = {0};
for (auto ch : s1) {
arr1[ch – ‘a’] = 1;
}
for (auto ch : s2) {
arr2[ch – ‘a’] = 1;
}
bool hasCommon = false;
for (int i = 0; i < 26; ++i) {
if (arr1[i] == 0 && arr2[i] == 0) {
hasCommon = true;
break;
}
}
if (hasCommon) {
cout << “YES” << endl;
} else {
cout << “NO” << endl;
}
}
}
int main() {
solve();
return 0;
}
Not-too-far Replacement
You are given an array AA of length N+1N+1, denoted [A1,A2,…,AN+1][A1,A2,…,AN+1].
You can perform the following operation on the array AA:
- Choose an index ii (1≤i≤N1≤i≤N) such that Ai≤2⋅AN+1Ai≤2⋅AN+1.
Then, swap the values of AiAi and AN+1AN+1.
That is, you can swap the element at index N+1N+1 with some element of the array that’s at most twice as large as it.
You may perform this operation as many times as you like (even zero times).
Find the minimum possible sum of the first NN elements of AA after the operations, i.e, the minimum possible value of
Input Format
- The first line of input will contain a single integer TT, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains a single integer NN — meaning the length of the array is N+1N+1.
- The second line contains N+1N+1 space-separated integers A1,A2,…,AN,AN+1A1,A2,…,AN,AN+1, the initial values of the array.
Output Format
For each test case, output on a new line the minimum possible sum of AA after performing some replacement operations.
Constraints
- 1≤T≤2001≤T≤200
- 1≤N≤2001≤N≤200
- 1≤Ai≤10001≤Ai≤1000
Sample 1:
4 4 5 2 1 2 3 3 3 10 30 1 4 16 4 8 2 1 4 20 100 30 49 15
8 43 15 165
Explanation:
Test case 11: Initially, A=[5,2,1,2,3]A=[5,2,1,2,3].
Perform one operation with i=1i=1, which is allowed since 5=A1≤2⋅AN+1=65=A1≤2⋅AN+1=6.
After the swap, A=[3,2,1,2,5]A=[3,2,1,2,5].
The sum of the first NN elements is 88, which is the minimum possible.
Test case 22: Initially, A=[3,10,30,1]A=[3,10,30,1]. AN+1AN+1 cannot be swapped with anything since it’s too small. So, the answer is just the sum of the first NN elements, which is 3+10+30=433+10+30=43.
Test case 33: Initially, A=[16,4,8,2,1]A=[16,4,8,2,1]. Consider the following sequence of operations:
- Choose i=4i=4, which is valid since 2≤2⋅12≤2⋅1. Now, A=[16,4,8,1,2]A=[16,4,8,1,2].
- Choose i=2i=2, which is valid since 4≤2⋅24≤2⋅2. Now, A=[16,2,8,1,4]A=[16,2,8,1,4].
- Choose i=3i=3, which is valid since 8≤2⋅48≤2⋅4. Now, A=[16,2,4,1,8]A=[16,2,4,1,8].
- Choose i=1i=1, which is valid since 16≤2⋅816≤2⋅8. Now, A=[8,2,4,1,16]A=[8,2,4,1,16].
The sum of the first NN elements is 1515 which is the best we can do.
Small, Smaller, Smallest
You are given a binary string SS of length NN.
You can perform the following two-step operation on it:
- Choose a non-empty contiguous substring of SS.
- If the substring contains an odd number of ones, delete every 00 in it.
If it instead contains an even number of ones, delete every 11 in it.
For example, suppose S=1010001S=1010001,
- If you choose the substring 101101 from indices 11 to 33, it has two ones, which is an even number.
So, delete every 11 in the substring, turning the string into S=00001S=00001. - If you instead choose the substring 10001000 from indices 33 to 66, the number of ones is 11, which is odd.
So, you’d delete every 00 in the substring, turning the string into S=1011S=1011.
You can perform this two-step operation as many times as you like.
Find the minimum possible length of the final string.
Input Format
- The first line of input will contain a single integer TT, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains a single integer NN — the length of the binary string.
- The second line contains the binary string SS of length NN.
Output Format
For each test case, output on a new line the minimum possible length of SS after performing several operations.
Constraints
- 1≤T≤1051≤T≤105
- 1≤N≤3⋅1051≤N≤3⋅105
- Si∈{0,1}Si∈{0,1}
- The sum of NN over all test cases won’t exceed 3⋅1053⋅105.
Sample 1:
3 2 11 3 010 5 01101
0 1 1
Explanation:
Test case 11: Choose the substring 1111, which is the entire string.
It has two ones, which is even – so we delete every occurrence of 11. The string becomes empty.
Test case 22: One optimal sequence of operations is as follows:
- Choose the substring 1010 from index 22 to index 33. It has one 11, which is an odd number – so every occurrence of 00 gets deleted. The string is now 0101.
- Choose the entire string, which is 0101. The 00 gets deleted again, making the string 11.
This is optimal.
Normal is Good (Easy)
This is the easy version of the problem. The only difference is the constraint on AiAi — in this version, 1≤Ai≤21≤Ai≤2.
An array BB of length MM is said to normal if its mean equals its median.
The mean of BB is defined to be
For example, the mean of [1,2][1,2] is 32=1.523=1.5, and the mean of [2,2,3,1,3][2,2,3,1,3] is 115=2.2511=2.2.
The median of BB is defined to be the element at index ⌈M2⌉⌈2M⌉ when the elements of BB are sorted in ascending order.
That is, the median of an odd-length array is the middle element after sorting, and the median of an even-length array is the left of the two middle elements after sorting.
For example, the median of [2,1,2][2,1,2] is 22 (sorted order is [1,2,2][1,2,2]), and the median of [3,1,2,2,1,1][3,1,2,2,1,1] is 11 (sorted order is [1,1,1,2,2,3][1,1,1,2,2,3], take the left of the two middle elements which is 11.)
You’re given an array AA containing NN integers, each of which lies between 11and 22.
Count the number of contiguous subarrays of AA that are normal.
Input Format
- The first line of input will contain a single integer TT, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains a single integer NN — the length of the array AA.
- The second line contains NN space-separated integers A1,A2,…,ANA1,A2,…,AN.
Output Format
For each test case, output on a new line the number of normal subarrays of AA.
Constraints
- 1≤T≤1051≤T≤105
- 1≤N≤3⋅1051≤N≤3⋅105
- 1≤Ai≤21≤Ai≤2
- The sum of NN over all test cases won’t exceed 3⋅1053⋅105.
Sample 1:
3 3 2 2 2 4 1 2 1 2 5 2 2 1 1 2
6 4 7
Explanation:
Test case 11: Every subarray contains only twos, so the mean and median are always 22.
Every subarray is normal, so the answer is the count of subarrays, which is 66.
Test case 22: The only normal subarrays are those with length 11.
Unmedian
You are given an array AA of length NN.
You can perform the following operation on it:
- Choose any contiguous subarray of AA such that its length is greater than 11 and odd.
- Then, delete the median†† of this subarray from it.
If there are multiple occurrences of the median within the subarray, delete the leftmost one.
Note that the length of the array decreases by 11 after the operation.
For example, if the array is A=[1,3,1,4,1,5]A=[1,3,1,4,1,5] and you choose the subarray [1,4,1][1,4,1] from indices 33 to 55, the median is 11.
The leftmost occurrence of 11 within the subarray is at index 33, so it will be deleted and the array becomes [1,3,4,1,5][1,3,4,1,5].
Is it possible to perform this operation several times (possibly, zero) so that the final array AA is sorted in non-descending order?
If it is possible, also find any valid sequence of operations.
Note that you do not need to minimize the number of operations: you only need to find any valid sequence.
†† The median of an odd-length array is the middle element when the array is written in sorted order.
For example, if A=[3,1,4,1,5]A=[3,1,4,1,5], the median is 33 since the sorted array is [1,1,3,4,5][1,1,3,4,5].
Input Format
- The first line of input will contain a single integer TT, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of input contains a single integer NN, the length of the array AA.
- The second line contains NN space-separated integers A1,A2,…,ANA1,A2,…,AN.
Output Format
For each test case,
- If it is not possible to obtain a sorted array no matter what, print a single line containing the integer −1−1.
- Otherwise,
- First, print a single line containing one integer KK: the number of operations you wish to perform.
- The next KK lines should describe the operations you perform.
On the ii-th of these KK lines, print two space-separated integers LiLi and RiRi, meaning that the operation is performed on the subarray [ALi,ALi+1,…,ARi][ALi,ALi+1,…,ARi] of the current array AA.
These integers should satisfy 1≤Li<Ri≤N−i+11≤Li<Ri≤N−i+1, and also Ri−Li+1Ri−Li+1should be odd.
Constraints
- 1≤T≤50001≤T≤5000
- 3≤N≤50003≤N≤5000
- 1≤Ai≤1091≤Ai≤109
- The sum of NN over all test cases won’t exceed 50005000.
Sample 1:
3 5 3 4 1 4 5 3 3 2 1 6 1 4 2 4 5 3
2 2 4 1 3 -1 2 2 4 1 5
Explanation:
Test case 11: Consider the following sequence of operations:
- Choose the subarray [4,1,4][4,1,4], from indices 22 to 44. The median is 44, deleting its leftmost occurrence results in A=[3,1,4,5]A=[3,1,4,5].
- Choose the subarray [3,1,4][3,1,4] from indices 11 to 33. The median is 33, deleting it results in
A=[1,4,5]A=[1,4,5] which is sorted.
Test case 22: AA cannot be sorted by performing the given operation.
Normal is Good
An array BB of length MM is said to normal if its mean equals its median.
The mean of BB is defined to be
For example, the mean of [1,2][1,2] is 32=1.523=1.5, and the mean of [2,2,3,1,3][2,2,3,1,3] is 115=2.2511=2.2.
The median of BB is defined to be the element at index ⌈M2⌉⌈2M⌉ when the elements of BB are sorted in ascending order.
That is, the median of an odd-length array is the middle element after sorting, and the median of an even-length array is the left of the two middle elements after sorting.
For example, the median of [2,1,2][2,1,2] is 22 (sorted order is [1,2,2][1,2,2]), and the median of [3,1,2,2,1,1][3,1,2,2,1,1] is 11 (sorted order is [1,1,1,2,2,3][1,1,1,2,2,3], take the left of the two middle elements which is 11.)
You’re given an array AA containing NN integers, each of which lies between 11and 33.
Count the number of contiguous subarrays of AA that are normal.
Input Format
- The first line of input will contain a single integer TT, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains a single integer NN — the length of the array AA.
- The second line contains NN space-separated integers A1,A2,…,ANA1,A2,…,AN.
Output Format
For each test case, output on a new line the number of normal subarrays of AA.
Constraints
- 1≤T≤1051≤T≤105
- 1≤N≤3⋅1051≤N≤3⋅105
- 1≤Ai≤31≤Ai≤3
- The sum of NN over all test cases won’t exceed 3⋅1053⋅105.
Sample 1:
3 3 3 1 2 4 1 2 1 2 5 3 2 2 3 1
4 4 8
Explanation:
Test case 11: Looking at every subarray of AA:
- [3][3] has both mean and median 33, so it is normal.
- [1][1] has both mean and median 11, so it is normal.
- [2][2] has both mean and median 22, so it is normal.
- [3,1][3,1] has mean 22 and median 11, so it is not normal.
- [1,2][1,2] has mean 1.51.5 and median 11, so it is not normal.
- [3,1,2][3,1,2] has mean and median both equal to 22, so it is normal.
Test case 22: The only normal subarrays are those with length 11.
MEXtreme Divisibility
The div-MEX of an array is defined to be the smallest positive integer that doesn’t divide any element of the array.
For example, the div-MEX of [4,10,1,9,21][4,10,1,9,21] is 66, because every integer from 11 to 55 divides at least one element of the array, but 66 doesn’t divide any of them.
You’re given an array AA of length NN, and an integer MM. The elements of AA all lie between 11 and MM.
At most once, you can choose an index ii (1≤i≤N1≤i≤N) and an integer xx (1≤x≤M1≤x≤M), and replace AiAi with xx.
Find the maximum possible div-MEX of the final array AA.
Input Format
- The first line of input will contain a single integer TT, denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test case contains two space-separated integers NN and MM — the length of the array and the maximum allowed value, respectively.
- The second line of each test case contains NN space-separated integers A1,A2,…,ANA1,A2,…,AN — the elements of array AA.
Output Format
For each test case, output on a new line the maximum possible div-MEX of array AA after making at most one replacement.
Constraints
- 1≤T≤2⋅1051≤T≤2⋅105
- 1≤N,M≤5⋅1051≤N,M≤5⋅105
- 1≤Ai≤M1≤Ai≤M
- The sum of NN and the sum of MM over all test cases both won’t exceed 5⋅1055⋅105.
Sample 1:
4 4 1 1 1 1 1 5 5 1 3 4 2 2 4 5 2 1 5 2 7 20 15 20 13 2 17 20 12
2 6 4 8
Explanation:
Test case 11: Since M=1M=1, all the elements of AA must be 11 no matter what.
The div-MEX is thus 22.
Test case 22: Replace A4=2A4=2 with 55, to obtain the array [1,3,4,5,2][1,3,4,5,2].
Every number from 11 to 55 is present in AA, while 66 doesn’t divide anything in AA.
Test case 33: Replace A2=1A2=1 with 33, to obtain [2,3,5,2][2,3,5,2].
1,2,31,2,3 all have multiples in the array, but 44 does not.
This is optimal.
Saturated Subarrays
For an array BB of length MM, let f(B)f(B) denote its maximum subarray sum.
Note that in this problem, the empty subarray with a sum of 00 is considered to be a subarray of BB.
We say the array BB is saturated if f(B)=∑i=1MBif(B)=∑i=1MBi, that is, if the maximum subarray sum of BB equals the sum of BB.
You’re given an array AA of length NN. Answer QQ queries on it of the following form:
- Given LL and RR (1≤L≤R≤N1≤L≤R≤N), count the number of pairs (l,r)(l,r) such that:
- L≤l≤r≤RL≤l≤r≤R, and
- The array [Al,Al+1,…,Ar][Al,Al+1,…,Ar] is saturated.
That is, given LL and RR, count the number of saturated subarrays of the array [AL,AL+1,…,AR][AL,AL+1,…,AR].
Input Format
- The first line of input will contain a single integer TT, denoting the number of test cases.
- Each test case consists of multiple lines of input.
- The first line of each test case contains two space-separated integers NN and QQ — the length of AA and the number of queries, respectively.
- The second line contains NN space-separated integers A1,A2,…,ANA1,A2,…,AN.
- The next QQ lines describe the queries. The ii-th of these lines contains space-separated integers LiLi and RiRi, denoting the bounds for the ii-th query.
Output Format
For each query, output the answer on a new line.
Constraints
- 1≤T≤1051≤T≤105
- 1≤N,Q≤2⋅1051≤N,Q≤2⋅105
- −109≤Ai≤109−109≤Ai≤109
- The sum of NN and the sum of QQ over all test cases both won’t exceed 2⋅1052⋅105.
Sample 1:
2 4 3 17 -15 3 18 2 3 1 4 2 4 10 3 8 5 7 20 -18 12 -13 -3 -6 -16 5 7 3 8 1 10
1 5 3 1 4 11
Explanation:
Test case 11: Let’s go over all the queries.
- Query 11: The subarray is [−15,3][−15,3]. The only saturated subarray is [3][3].
- Query 22: The subarray is [17,−15,3,18][17,−15,3,18]. The saturated subarrays are:
- [17][17]
- [3][3]
- [18][18]
- [3,18][3,18]
- [17,−15,3,18][17,−15,3,18]
- Query 33: The subarray is [−15,3,18][−15,3,18]. the saturated subarrays are the same as the previous query, but excluding [17][17] and the full array.