Contest 123
Contest 123
Each case contains two integers. The first integer means your position (different than 0) and
the second integer means the position of the lift. Assume that the department has 100 floors
(may be one day it will be :D).
Output
For each case, print the case number and the calculated time in seconds.
Sample
Input copy Output copy
3 Case 1: 27
1 2 Case 2: 59
3 10 Case 3: 39
5 5
#include <stdio.h>
int main() {
scanf("%d", &T);
return 0;}
Input
The first line contains one integer t𝑡 (1≤t≤1001≤𝑡≤100) – the number of queries.
The first line of each query contains three integers b𝑏, p𝑝 and f𝑓 (1≤b, p, f≤1001≤𝑏, 𝑝, 𝑓≤100) — the number of buns, beef patties and chicken
cutlets in your restaurant.
The second line of each query contains two integers hℎ and c𝑐 (1≤h, c≤1001≤ℎ, 𝑐≤100) — the hamburger and chicken burger prices in your
restaurant.
Output
For each query print one integer — the maximum profit you can achieve.
Examples
3 40
15 2 3 34
5 10 0
7 5 2
10 12
1 100 100
100 100
Note
In first query you have to sell two hamburgers and three chicken burgers. Your income is 2⋅5+3⋅10=402⋅5+3⋅10=40.
In second query you have to ell one hamburgers and two chicken burgers. Your income is 1⋅10+2⋅12=341⋅10+2⋅12=34.
In third query you can not create any type of burgers because because you have only one bun. So your income is zero.
#include <stdio.h>
int main() {
scanf("%d", &t);
while (t--) {
if (h > c) {
int numHamburgers = b / 2;
maxProfit += numHamburgers * h;
maxProfit += numChickenBurgers * c;
} else {
int numChickenBurgers = b / 2;
maxProfit += numChickenBurgers * c;
maxProfit += numHamburgers * h;
printf("%d\n", maxProfit);
return 0;
}}
Vasya is reading a e-book. The file of the book consists of n𝑛 pages, numbered from 11 to n𝑛. The screen is currently displaying the
contents of page x𝑥, and Vasya wants to read the page y𝑦. There are two buttons on the book which allow Vasya to scroll d𝑑 pages
forwards or backwards (but he cannot scroll outside the book). For example, if the book consists of 1010 pages, and d=3𝑑=3, then from the
first page Vasya can scroll to the first or to the fourth page by pressing one of the buttons; from the second page — to the first or to the
fifth; from the sixth page — to the third or to the ninth; from the eighth — to the fifth or to the tenth.
Help Vasya to calculate the minimum number of times he needs to press a button to move to page y𝑦.
Input
The first line contains one integer t𝑡 (1≤t≤1031≤𝑡≤103) — the number of testcases.
Each testcase is denoted by a line containing four integers n𝑛, x𝑥, y𝑦, d𝑑 (1≤n,d≤1091≤𝑛,𝑑≤109, 1≤x,y≤n1≤𝑥,𝑦≤𝑛) — the number of pages, the
starting page, the desired page, and the number of pages scrolled by pressing one button, respectively.
Output
If Vasya can move from page x𝑥 to page y𝑦, print the minimum number of times he needs to press a button to do it. Otherwise print −1−1.
Examples
3 4
10 4 5 2 -1
5 1 3 4 5
20 4 19 3
Note
#include <stdio.h>
#include <stdlib.h>
int main() {
scanf("%d", &t);
while (t--) {
long long n, x, y, d; // Number of pages, starting page, desired page, and scroll distance
if (diff % d == 0) {
} else {
if (leftDist % d == 0) {
if (rightDist % d == 0) {
printf("%lld\n", minPress);
return 0;
Input
The only line contains the string s𝑠, consisting of lowercase latin letters (1≤|s|≤1000001≤|𝑠|≤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".
Examples
abacaba No
iiq Yes
abba No
Note
In the first example the first player is unable to make a turn, so he loses.
In the second example first player turns the string into "q", then second player is unable to move, so he loses.
#include <stdio.h>
#include <string.h>
int main() {
scanf("%s", s);
cnt[s[i] - 'a']++;
if (cnt[i] % 2 != 0) {
return 0;
return 0;
(linear)
Input
The first line of the input contains two integers n and k (1 ≤ k ≤ n ≤ 50) separated by a single space.
The second line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 100), where ai is the score earned by the participant who got the i-th
place. The given sequence is non-increasing (that is, for all i from 1 to n - 1 the following condition is fulfilled: ai ≥ ai + 1).
Output
8 5 6
10 9 8 7 7 7 5 5
4 2 0
0 0 0 0
Note
In the first example the participant on the 5th place earned 7 points. As the participant on the 6th place also earned 7 points, there are 6
advancers.
#include <stdio.h>
int main() {
scanf("%d", &scores[i]);
advancers++;
printf("%d\n", advancers);
return 0;
Input
There will be only one set of input for this problem. The first line of input gives you a number N (1 ≤ N ≤ 50000), the number of lady chimps on
the line. In the next line you would have N integers (in the range 1 to 231 − 1 giving the heights of
the N chimps. There would be a single space after every number. You can assume that the chimps
are ordered in non-decreasing order of their heights. In the next line you would have an integer Q
(1 ≤ Q ≤ 25000) giving the number of queries. Then in the next line Q queries will follow. Then you
would have Q numbers giving the height of Luchu! Dont worry, Luchu is from the land where people
can have 3 birthdates; Q heights for a chimpanzee will make no difference here. The Q numbers are
listed on a line and their range from 1 to 231 − 1, and as before you would find a single space after
every query number. The query numbers are not supposed to come in any particular order.
Output
For each query height, print two numbers in one line. The first one would be the height of the tallest
lady chimp that is shorter than Luchu, and the next number would be the height of the shortest lady
chimp that is taller than he. These two numbers are to be separated by a single space. Whenever it is
impossible to find any of these two heights, replace that height with an uppercase ‘X’.
Sample Input
1457
4 6 8 10
Sample Output
15
57
7X
7X
#include <stdio.h>
int main() {
scanf("%d", &n);
scanf("%d", &chimps[i]);
scanf("%d", &q);
while (q--) {
scanf("%d", &luchu);
// Binary search for the tallest lady chimp shorter than Luchu
shorter = chimps[mid];
left = mid + 1;
} else {
right = mid - 1;
// Binary search for the shortest lady chimp taller than Luchu
left = 0, right = n - 1;
taller = chimps[mid];
right = mid - 1;
} else {
left = mid + 1;
if (shorter == -1) {
printf("X ");
} else {
if (taller == -1) {
printf("X\n");
} else {
printf("%d\n", taller);
}
return 0;
Input consists of a number of test cases T (less than 20). Each case starts with an integer n which is
the number of students in the course. This value can be as large as 100,000 and as low as 2. Next n
lines contain n integers where the i’th integer is the score of the i’th student. All these integers have
absolute values less than 150000. If i < j, then i’th student is senior to the j’th student.
Output
For each test case, output the desired number in a new line. Follow the format shown in sample
input-output section.
Sample Input
100
20
Sample Output
80
-1
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The only line of each test case contains one string S, consisting of digits '0' and '1' only.
Output
For each test case, output a single line containing the answer — "YES" if all the '1' digits form a single non-empty segment, and "NO"
otherwise. Don't print the quotes.
Constraints
• 1 ≤ T ≤ 10
• 1 ≤ |S| ≤ 105 (here, |S| denotes the length of S)
Subtasks
Sample 1
6 YES
001111110 NO
00110011 NO
000 YES
1111 NO
101010101 NO
101111111111
#include <stdio.h>
#include <string.h>
char* check_segment(char* S) {
int found_one = 0;
if (S[i] == '1') {
found_one = 1;
break;
int main() {
int T;
scanf("%d", &T);
while (T--) {
char S[100005];
scanf("%s", S);
printf("%s\n", check_segment(S));
return 0;
InputThe first line contains an integer n𝑛 — the number of cards with digits that you have (1≤n≤1001≤𝑛≤100).
The second line contains a string of n𝑛 digits (characters "0", "1", ..., "9") s1,s2,…,sn𝑠1,𝑠2,…,𝑠𝑛. The string will not contain any other characters, such as
leading or trailing spaces.
Output
If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Other wise, output 0.
Examples
11 1
00000000008
22 2
0011223344556677889988
11 0
31415926535
Note
In the first example, one phone number, "8000000000", can be made from these cards.
#include <stdio.h>
#include <string.h>
int main() {
int n;
scanf("%d", &n);
char s[105];
scanf("%s", s);
printf("%d\n", maxPhoneNumbers);
return 0;
Input The input will contain several test cases, each of them as described below. Consecutive test cases are separated by
a single blank line. The first line of the input contains a positive integer Y , the number of years of the period we are
interested in. The second line contains another positive integer, the number of Popes, P. Each of the remaining P lines
contains the year of election of a Pope, in chronological order. We know that P ≤ 100000 and also that the last year L in
the file is such L ≤ 1000000, and that Y ≤ L. Output For each test case, write to the output contains a single line with three
integers, separated by spaces: the largest number of Popes in a Y -year period, the year of election of the first Pope in
that period and the year of election of the last Pope in the period. Sample Input 5((ektar niche r ekte) 20 1 2 3 6 8 12 13
13 15 16 17 18 19 20 20 21 25 26 30 31 Sample Output 6 16 20
#include <stdio.h>
#include <stdlib.h>
int main() {
int Y, P;
int electionYears[P];
scanf("%d", &electionYears[i]);
int maxPopes = 0;
int firstYear = 0;
int lastYear = 0;
for (int i = 0; i < P; ++i) {
int count = 1;
count++;
} else {
break;
maxPopes = count;
firstYear = currentYear;
return 0;
}Input
Input starts with an integer T (≤ 5), denoting the number of test cases.
Each case starts with a line containing two integers n (1 ≤ n ≤ 105) and q (1 ≤ q ≤ 50000). The next line contains n space separated integers denoting the points
in ascending order. All the integers are distinct and each of them range in [0, 108].
Each of the next q lines contains two integers Ak Bk (0 ≤ Ak ≤ Bk ≤ 108) denoting a segment.
Output
For each case, print the case number in a single line. Then for each segment, print the number of points that lie in that segment.
Sample
1 Case 1:
5 3 2
1 4 6 8 10 3
0 5 2
6 10
7 100000
#include <stdio.h>
int main() {
int T;
scanf("%d", &T);
int n, q;
int points[n];
scanf("%d", &points[i]);
int A, B;
int count = 0;
count++;
printf("%d\n", count);
return 0;
Input starts with an integer T (≤ 5), denoting the number of test cases.
Each case starts with a line containing two integers n (1 ≤ n ≤ 105) and q (1 ≤ q ≤ 50000). The next line contains n space separated integers denoting the points
in ascending order. All the integers are distinct and each of them range in [0, 108].
Each of the next q lines contains two integers Ak Bk (0 ≤ Ak ≤ Bk ≤ 108) denoting a segment.
Output
For each case, print the case number in a single line. Then for each segment, print the number of points that lie in that segment.
Sample
Input copy Output copy
1 Case 1:
5 3 2
1 4 6 8 10 3
0 5 2
6 10
7 100000
#include <stdio.h>
int main() {
int T;
scanf("%d", &T);
int n, q;
int points[n];
scanf("%d", &points[i]);
int A, B;
int count = 0;
count++;
printf("%d\n", count);
return 0;
}
Input
The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile.
The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot.
The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms.
Output
Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the
number qi is.
Examples
5 1
2 7 3 4 9 5
3 3
1 25 11
#include <stdio.h>
#include <algorithm>
int main() {
int n, m;
scanf("%d", &n);
int a[n];
scanf("%d", &a[i]);
int cumSum[n];
cumSum[0] = a[0];
scanf("%d", &m);
int q;
scanf("%d", &q);
// Binary search to find the pile
printf("%d\n", pile);
return 0;
(binary)}Input The input contains several test cases. Every test case begins with a line that contains a single integer n < 500, 000 — the length of the input sequence. Each of the the following
n lines contains a single integer 0 ≤ a[i] ≤ 999, 999, 999, the i-th input sequence element. Input is terminated by a sequence of length n = 0. This sequence must not be processed. Output For
every input sequence, your program prints a single line containing an integer number op, the minimum number of swap operations necessary to sort the given input sequence. Sample Input
5 9 1 0 5 4 3 1 2 3 0 Sample Output 6 0
#include <stdio.h>
int main() {
int n;
while (1) {
scanf("%d", &n);
int a[n];
scanf("%d", &a[i]);
swaps++;
printf("%lld\n", swaps);
return 0;
}Input The input contains on the first line the number of test cases (N). Each test case consists of two input lines. The first line of a test case contains an integer L, determining the length of
the train (0 ≤ L ≤ 50). The second line of a test case contains a permutation of the numbers 1 through L, indicating the current order of the carriages. The carriages should be ordered such
that carriage 1 comes first, then 2, etc. with carriage L coming last. Output For each test case output the sentence: ‘Optimal train swapping takes S swaps.’ where S is an integer. Sample Input
3 3 1 3 2 4 4 3 2 1 2 2 1 Sample Output Optimal train swapping takes 1 swaps. Optimal train swapping takes 6 swaps. Optimal train swapping takes 1 swaps.
#include <stdio.h>
int main() {
int N;
scanf("%d", &N);
while (N--) {
int L;
scanf("%d", &L);
int carriages[L];
scanf("%d", &carriages[i]);
swaps++;
return 0;
}You are given the ages (in years) of all people of a country with at least 1 year of age. You know that
no individual in that country lives for 100 or more years. Now, you are given a very simple task of
Input
There are multiple test cases in the input file. Each case starts with an integer n (0 < n ≤ 2000000), the
total number of people. In the next line, there are n integers indicating the ages. Input is terminated
Output
For each case, print a line with n space separated integers. These integers are the ages of that country
Sample Input
34215
23231
Sample Output
12345
12233
#include <stdio.h>
int main() {
int n;
while (1) {
scanf("%d", &n);
int ages[n];
scanf("%d", &ages[i]);
swaps++;
printf("%d", ages[i]);
printf("\n");
return 0;
Sorting in computer science is an important part. Almost every problem can be solved effeciently if
sorted data are found. There are some excellent sorting algorithm which has already acheived the lower
bound n · lg n. In this problem we will also discuss about a new sorting approach. In this approach
only one operation (Flip) is available and that is you can exchange two adjacent terms. If you think a
while, you will see that it is always possible to sort a set of numbers in this way.
A set of integers will be given. Now using the above approach we want to sort the numbers in
ascending order. You have to find out the minimum number of flips required. Such as to sort ‘1 2 3’
The input will start with a positive integer N (N ≤ 1000). In next few lines there will be N integers.
Output
For each data set print ‘Minimum exchange operations : M’ where M is the minimum flip operations required to perform sorting. Use a seperate line for each case.
Sample Input
123
231
Sample Output
#include <stdio.h>
temp = arr[start];
arr[start] = arr[i];
arr[i] = temp;
start++;
i--;
int mi, i;
mi = i;
return mi;
if (mi != curr_size - 1) {
flip(arr, mi);
}
}
int main() {
pancakeSort(arr, n);
return 0;