Session 2 Sheet
Problem A: Elephant
Input file: standard input Time limit: 1 second
Output file: standard output Memory limit: 256 megabytes
An elephant decided to visit his friend. It turned out that the elephant's
house is located at point 0 and his friend's house is located at point x (x >0)
of the coordinate line. In one step the elephant can move 1, 2, 3, 4 or 5
positions forward. Determine, what is the minimum number of steps he
need to make in order to get to his friend's house.
Input
The first line of the input contains an integer x (1 ≤ x ≤1 000 000) — The
coordinate of the friend's house.
Output
Print the minimum number of steps that elephant needs to make to get
from point 0 to point x .
Standard Input Standard Output
5 1
12 3
Note
In the first sample the elephant needs to make one step of length 5 to reach
the point x .
In the second sample the elephant can get to point x if he moves by 3, 5
and 4 . There are other ways to get the optimal answer but the elephant
cannot reach x in less than three moves.
2
#include <bits/stdc++.h>
using namespace std;
int main()
{
int counter = 0;
int x;
cin >> x;
while (x > 0)
{
x -= 5;
counter++;
}
cout << counter << '\n';
return 0;
}
Answer of Problem A
Problem B: Square String?
3
Input file: standard input Time limit: 1 second
Output file: standard output Memory limit: 256 megabytes
A string is called square if it is some string written twice in a row. For
example, the strings "aa", "abcabc", "abab" and "baabaa" are
square. But the strings "aaa", "abaaab" and "abcdabc" are not square.
For a given string s determine if it is square.
Input
The first line of input data contains an integer t (1≤ t ≤ 100) — the number of
test cases.
This is followed by t lines, each containing a description of one test case.
The given strings consist only of lowercase Latin letters and have lengths
between 1 and 100 inclusive.
Output
For each test case, output on a separate line:
YES if the string in the corresponding test case is square,
NO otherwise.
You can output YES and NO in any case (for example,
strings yEs, yes, Yes and YES will be recognized as a positive response).
Standard Input Standard Output
10 NO
4
a YES
aa NO
aaa YES
aaaa YES
abab YES
abcabc NO
abacaba NO
xxyy NO
xyyx YES
xyxy
Answer of Problem B
5
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
string str;
cin >> str;
if ([Link]() % 2 != 0)
{
cout << "NO\n";
continue;
}
else
{
string leftStr = [Link](0,
[Link]() / 2);
string rightStr = [Link]([Link]() /
2);
if (leftStr == rightStr)
{
cout << "YES\n";
}
else
cout << "NO\n";
}
}
return 0;
}
6
Problem C: Smallest Pair
Input file: standard input Time limit: 1 second
Output file: standard output Memory limit: 256 megabytes
Given a number N and an array A of N numbers.
Print the smallest possible result of Ai + A j + j−i, where 1 ≤i< j≤ N .
Input
The first line contains a number T (1≤ T ≤ 100) number of test cases.
Each test case contains two lines:
The first line consists a number N (2 ≤ N ≤ 100) number of elements.
The second line contains N numbers (−106 ≤ A i ≤ 106 ).
Output
For each test case print a single line contains the smallest possible sum
for the corresponding test case.
Standard Input Standard Output
1 7
4
20 1 9 4
Note
First Case :
7
All possibles (i , j) where (1 ≤i< j≤ N ) are :
i=1 , j=2 then result ¿ a 1+a 2+ j−i=20+1+2−1=22.
i=1 , j=3 then result ¿ a 1+a 3+ j−i=20+ 9+3−1=31.
i=1 , j=4 then result ¿ a 1+a 4+ j−i=20+ 4+ 4−1=27 .
i=2 , j=3 then result ¿ a 2+ a3+ j−i=1+ 9+3−2=11.
i=2 , j=4 then result ¿ a 2+ a 4+ j−i=1+ 4+ 4−2=7 .
i=3 , j=4 then result ¿ a 3+ a 4+ j−i=9+4 +4−3=14.
So the smallest possible result is 7.
Answer of Problem C
#include <bits/stdc++.h>
8
using namespace std;
int main()
int t;
cin >> t;
while (t--)
int n;
cin >> n;
int A[n];
for (int i = 0; i < n; i++)
cin >> A[i];
int smallestPair = INT_MAX, pair;
for (int i = 0; i < n - 1; i++)
for (int j = i + 1; j < n; j++)
pair = A[i] + A[j] + (j + 1) - (i + 1);
if (smallestPair > pair)
smallestPair = pair;
cout << smallestPair << '\n';
return 0;
Problem D: Taxi
9
Input file: standard input Time limit: 1 second
Output file: standard output Memory limit: 256 megabytes
After the lessons n groups of schoolchildren went outside and decided to
visit Polycarpus to celebrate his birthday. We know that the i-th group
consists of sifriends (1 ≤ s i ≤ 4), and they want to go to Polycarpus together.
They decided to get there by taxi. Each car can carry at most four
passengers. What minimum number of cars will the children need if all
members of each group should ride in the same taxi (but one taxi can take
more than one group)?
Input
The first line contains integer n( 1≤ n ≤10 5) — the number of groups of
schoolchildren.
The second line contains a sequence of integers s1, s2, ..., sn (1 ≤ s i ≤ 4). The
integers are separated by a space, si is the number of children in the i-th
group.
Output
Print the single number — the minimum number of taxis necessary to drive
all children to Polycarpus.
Standard Input Standard Output
5 4
12433
8 5
2 3 4 4 21 3 1
Note
In the first test we can sort the children into four cars like this:
10
the third group (consisting of four children),
the fourth group (consisting of three children),
the fifth group (consisting of three children),
the first and the second group (consisting of one and two children,
correspondingly).
There are other ways to sort the groups into four cars.
Answer of Problem D
#include <bits/stdc++.h>
11
using namespace std;
int main()
ios_base::sync_with_stdio(false);
[Link](nullptr);
int n, value, min = 0;
cin >> n;
int frequency[5] = {0};
for (int i = 0; i < n; i++)
cin >> value;
frequency[value]++;
min += frequency[4];
while (frequency[3] > 0 && frequency[1] > 0)
frequency[3]--;
frequency[1]--;
min++;
if (frequency[3] > 0)
min += frequency[3];
frequency[3] = 0;
min += frequency[2] / 2;
12
frequency[2] %= 2;
if (frequency[2] != 0)
if (frequency[1] <= 2)
frequency[1] = 0;
frequency[2] = 0;
min++;
else
frequency[1] -= 2;
frequency[2] = 0;
min++;
min += frequency[1] / 4;
frequency[1] %= 4;
if (frequency[1] != 0)
frequency[1] = 0;
min++;
cout << min << '\n';
return 0;
Problem E: Decoding
13
Input file: standard input Time limit: 1 second
Output file: standard output Memory limit: 256 megabytes
Polycarp is mad about coding, that is why he writes Sveta encoded
messages. He calls the median letter in a word the letter which is in the
middle of the word. If the word's length is even, the median letter is the left
of the two middle letters. In the following examples, the median letter is
highlighted: contest, info. If the word consists of single letter, then
according to above definition this letter is the median letter.
Polycarp encodes each word in the following way: he writes down the
median letter of the word, then deletes it and repeats the process until
there are no letters left. For example, he encodes the
word volga as logva.
You are given an encoding s of some word, your task is to decode it.
Input
The first line contains a positive integer n(1≤ n ≤ 2000) — the length of the
encoded word.
The second line contains the string s of length n consisting of lowercase
English letters — the encoding.
Output
Print the word that Polycarp encoded.
Standard Input Standard Output
14
5 volga
logva
2 no
no
4 baba
abba
Note
In the first example Polycarp encoded the word volga. At first, he wrote
down the letter l from the position 3, after that his word looked like voga.
After that Polycarp wrote down the letter o from the position 2, his word
became vga. Then Polycarp wrote down the letter g which was at the
second position, the word became va. Then he wrote down the letter v,
then the letter a. Thus, the encoding looked like logva.
In the second example Polycarp encoded the word no. He wrote down the
letter n, the word became o, and he wrote down the letter o. Thus, in this
example, the word and its encoding are the same.
In the third example Polycarp encoded the word baba. At first, he wrote
down the letter a, which was at the position 2, after that the word looked
like bba. Then he wrote down the letter b, which was at the position 2, his
word looked like ba. After that he wrote down the letter b, which was at the
position 1, the word looked like a, and he wrote down that letter a. Thus,
the encoding is abba.
Answer of Problem E
15
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
string str;
cin >> n;
cin >> str;
string result = "";
for (int i = 0; i < n; i++)
{
[Link](i / 2, string(1, str[n - i -
1]));
}
cout << result << '\n';
return 0;
}
16
Problem F: Game with string
Input file: standard input Time limit: 1 second
Output file: standard output Memory limit: 256 megabytes
Two people are playing a game with a string s, consisting of lowercase latin
letters.
On a player's turn, he should choose two consecutive equal letters in the
string and delete them.
For example, if the string is equal to "xaax" than there is only one
possible turn: delete "aa", so the string will become "xx". A player not
able to make a turn loses.
Your task is to determine which player will win if both play optimally.
Input
The only line contains the string s, consisting of lowercase latin letters
(1 ≤∨s∨≤100000), where ¿ s∨¿ means the length of a string s.
Output
If the first player wins, print "Yes". If the second player wins, print "No".
Standard Input Standard Output
abacaba No
iiq Yes
abba No
abba
Note
In the first example the first player is unable to make a turn, so he loses.
17
In the second example first player turns the string into "q", then second
player is unable to move, so he loses.
Answer of Problem E
18