Skip to content

Commit 279c3b7

Browse files
Putting all the Long Contests inside the correct year folder in the Long Contest Folder
1 parent 875e444 commit 279c3b7

File tree

55 files changed

+2762
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+2762
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
A set of integers is called good if there does not exist three distinct elements a, b, c in it such that a + b = c.
2+
3+
Your task is simple. Just output any good set of n integers. All the elements in this set should be distinct and should lie between 1 and 500, both inclusive.
4+
5+
----------------------------------------------------------------------------
6+
7+
The sum of two odd numbers is even. Odd numbers are not closed with respect to addition.
8+
9+
for(i = 1; i <= size_of_set; i++)
10+
{
11+
printf("%d ", (2*i - 1));
12+
}
13+
printf("\n");
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <stdio.h>
2+
3+
void solve()
4+
{
5+
int size_of_set, i;
6+
scanf("%d", &size_of_set);
7+
8+
for(i = 1; i <= size_of_set; i++)
9+
{
10+
printf("%d ", (2*i - 1));
11+
}
12+
printf("\n");
13+
}
14+
15+
int main()
16+
{
17+
int no_of_test_cases;
18+
scanf("%d", &no_of_test_cases);
19+
while(no_of_test_cases-- != 0)
20+
solve();
21+
22+
return 0;
23+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
Chef found a strange string yesterday - a string of signs s, where each sign is either a '<', '=' or a '>'. Let N be the length of this string.
2+
Chef wants to insert N + 1 positive integers into this sequence and make it valid.
3+
A valid sequence is a sequence where every sign is preceded and followed by an integer, and the signs are correct.
4+
That is, if a sign '<' is preceded by the integer a and followed by an integer b, then a should be less than b. Likewise for the other two signs as well.
5+
Chef can take some positive integers in the range [1, P] and use a number in the range as many times as he wants.
6+
Help Chef find the minimum possible P with which he can create a valid sequence.
7+
8+
--------------------------------------------------------------
9+
10+
Notice that if there was a sequence of equal to and greater than signs like,
11+
12+
< = < = = < ... The number of distinct numbers must be equal to at least 1 more than the number of < signs. Because each of these signs cause an inequality.
13+
14+
----------------------------------------
15+
16+
My idea was to go through the string ... Go through every run of < and =, and > and =, keeping track of the maximum number of < and > occuring in any run.
17+
18+
P = 1 + max.
19+
20+
I got a WA at first because I only considered the greater case and didn't consider that case of > and = signs. Fortunately, I was able to catch the mistake myself.
21+
I solved it in contest environment so no access to other solutions or editorials. It felt great to see the green tick after solving a problem on your own !
22+
23+
Also got strange results when the second loop was starting from i... The second loop always starts from where the first one ends.
24+
We know the entire extent of the first loop has no < signs. This ensures that the complexity is linear.
25+
26+
---------------------------------------
27+
28+
void solve()
29+
{
30+
char sequence[MAX_LENGTH];
31+
scanf("%s", sequence);
32+
33+
int maximum_sign_run = 0, length = strlen(sequence), i, j;
34+
35+
for(i = 0; i < length; i = j)
36+
{
37+
int no_of_greater_than = 0;
38+
for(j = i; sequence[j] == '<' || sequence[j] == '='; j++)
39+
{
40+
no_of_greater_than += (sequence[j] == '<');
41+
}
42+
43+
int no_of_less_than = 0;
44+
for(; sequence[j] == '>' || sequence[j] == '='; j++) //Pick off from where the previous loop ended.
45+
{
46+
no_of_less_than += (sequence[j] == '>');
47+
}
48+
49+
maximum_sign_run = max(maximum_sign_run, max(no_of_greater_than, no_of_less_than));
50+
51+
}
52+
printf("%d\n", maximum_sign_run + 1);
53+
54+
}
55+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Nitika was once reading a history book and wanted to analyze it. So she asked her brother to create a list of names of the various famous personalities in the book.
2+
Her brother gave Nitika the list. Nitika was furious when she saw the list. The names of the people were not properly formatted.
3+
She doesn't like this and would like to properly format it.
4+
A name can have at most three parts: first name, middle name and last name. It will have at least one part. The last name is always present.
5+
The rules of formatting a name are very simple:
6+
Only the first letter of each part of the name should be capital.
7+
All the parts of the name except the last part should be represented by only two characters. The first character should be the first letter of the part and should be
8+
capitalized. The second character should be ".".
9+
10+
Let us look at some examples of formatting according to these rules:
11+
gandhi -> Gandhi
12+
mahatma gandhI -> M. Gandhi
13+
Mohndas KaramChand ganDhi -> M. K. Gandhi
14+
15+
-----------------------------------------
16+
17+
Surprisingly, this problem challenged me a lot more than I thought ... Ultimately, I didn't get full AC because I didn't put a space in between the names. I realised
18+
that because I only cleared the first subtask. That was a good way of informing by CodeChef. Then, I looked at the sample cases closer and saw that there was a space
19+
in between two parts of the name. Then, I scored full AC and 100 points.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <cstdio>
2+
#include <cstring>
3+
4+
#define max(a, b) (a > b ? a : b)
5+
#define MAX_LENGTH 100000 + 1
6+
7+
void solve()
8+
{
9+
char sequence[MAX_LENGTH];
10+
scanf("%s", sequence);
11+
12+
int maximum_sign_run = 0, length = strlen(sequence), i, j;
13+
14+
for(i = 0; i < length; i = j)
15+
{
16+
int no_of_greater_than = 0;
17+
for(j = i; sequence[j] == '<' || sequence[j] == '='; j++)
18+
{
19+
no_of_greater_than += (sequence[j] == '<');
20+
}
21+
22+
int no_of_less_than = 0;
23+
for(; sequence[j] == '>' || sequence[j] == '='; j++) //Pick off from where the previous loop ended.
24+
{
25+
no_of_less_than += (sequence[j] == '>');
26+
}
27+
28+
maximum_sign_run = max(maximum_sign_run, max(no_of_greater_than, no_of_less_than));
29+
30+
}
31+
printf("%d\n", maximum_sign_run + 1);
32+
33+
}
34+
35+
int main()
36+
{
37+
int no_of_test_cases;
38+
scanf("%d", &no_of_test_cases);
39+
40+
while(no_of_test_cases--)
41+
solve();
42+
43+
return 0;
44+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <cstdio>
2+
#include <iostream>
3+
#include <vector>
4+
#include <string>
5+
#include <string.h>
6+
#include <algorithm>
7+
8+
#define all(v) (v).begin(), (v).end()
9+
#define MAX_LENGTH 50 + 1
10+
11+
using namespace std;
12+
13+
void solve()
14+
{
15+
int i, space = 0;
16+
char name[MAX_LENGTH];
17+
18+
string formatted_name;
19+
fgets(name, MAX_LENGTH, stdin);
20+
21+
if(name[0] == '\n') return;
22+
23+
for(i = strlen(name) - 2; name[i] != ' ' && i > 0; i--)
24+
{
25+
formatted_name += tolower(name[i]);
26+
}
27+
28+
while(i >= 0)
29+
{
30+
if(name[i] == ' ')
31+
{
32+
space++;
33+
34+
if(space > 1)
35+
formatted_name += name[i + 1];
36+
37+
char last_char = formatted_name[formatted_name.length() - 1]; //Make first letter of name capital
38+
formatted_name[formatted_name.length() - 1] = toupper(last_char);
39+
40+
formatted_name += " .";
41+
42+
}
43+
i--;
44+
}
45+
46+
char first_letter = name[0];
47+
formatted_name += toupper(first_letter);
48+
49+
reverse(all(formatted_name));
50+
51+
cout << formatted_name << "\n";
52+
}
53+
54+
int main()
55+
{
56+
int no_of_test_cases;
57+
scanf("%d", &no_of_test_cases);
58+
59+
no_of_test_cases++; //fgets reads the first new line as a test case.
60+
61+
while(no_of_test_cases--)
62+
solve();
63+
64+
return 0;
65+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
2+
-----------------------------------------
3+
4+
Maintain a frequency vector of each digit in the number. This can be done in O(n) time.
5+
6+
Now for each character of the alphabet check if it can be made. This can be done in O(t), where t is the number of alphabets.
7+
8+
As t <<< n ... Complexity is O(n) overall.
9+
10+
Probably, looping through all 26 alphabets and checking if the digits of it's ASCII representation are present using binary search will not work.
11+
12+
That would be O(nlog n + t log n) ... First for sorting and then t queries for each alphabet.
13+
14+
I'm not sure. It might still pass.... But, this approach is nicer except I couldn't think of a better way to write it than put all of the ASCII representations of
15+
all alphabets inside a big switch-case.
16+
17+
-------------------------------------------------
18+
19+
int possible_to_make(char ch, vector <int> &frequency)
20+
{
21+
switch(ch)
22+
{
23+
case 'A' : return (frequency[6] >= 1 && frequency[5] >= 1);
24+
case 'B' : return (frequency[6] >= 2);
25+
case 'C' : return (frequency[6] >= 1 && frequency[7] >= 1);
26+
case 'D' : return (frequency[6] >= 1 && frequency[8] >= 1);
27+
case 'E' : return (frequency[6] >= 1 && frequency[9] >= 1);
28+
case 'F' : return (frequency[7] >= 1 && frequency[0] >= 1);
29+
case 'G' : return (frequency[7] >= 1 && frequency[1] >= 1);
30+
case 'H' : return (frequency[7] >= 1 && frequency[2] >= 1);
31+
case 'I' : return (frequency[7] >= 1 && frequency[3] >= 1);
32+
case 'J' : return (frequency[7] >= 1 && frequency[4] >= 1);
33+
case 'K' : return (frequency[7] >= 1 && frequency[5] >= 1);
34+
case 'L' : return (frequency[7] >= 1 && frequency[6] >= 1);
35+
case 'M' : return (frequency[7] >= 2);
36+
case 'N' : return (frequency[7] >= 1 && frequency[8] >= 1);
37+
case 'O' : return (frequency[7] >= 1 && frequency[9] >= 1);
38+
case 'P' : return (frequency[8] >= 1 && frequency[0] >= 1);
39+
case 'Q' : return (frequency[8] >= 1 && frequency[1] >= 1);
40+
case 'R' : return (frequency[8] >= 1 && frequency[2] >= 1);
41+
case 'S' : return (frequency[8] >= 1 && frequency[3] >= 1);
42+
case 'T' : return (frequency[8] >= 1 && frequency[4] >= 1);
43+
case 'U' : return (frequency[8] >= 1 && frequency[5] >= 1);
44+
case 'V' : return (frequency[8] >= 1 && frequency[6] >= 1);
45+
case 'W' : return (frequency[8] >= 1 && frequency[7] >= 1);
46+
case 'X' : return (frequency[8] >= 2);
47+
case 'Y' : return (frequency[8] >= 1 && frequency[9] >= 1);
48+
case 'Z' : return (frequency[9] >= 1 && frequency[0] >= 1);
49+
}
50+
return false;
51+
}
52+
53+
void solve()
54+
{
55+
const int no_of_alphabets = 26;
56+
vector <int> frequency(no_of_alphabets, 0);
57+
58+
char number[MAX_SIZE];
59+
scanf("%s", number);
60+
61+
for(int i = 0; number[i] != '\0'; i++)
62+
{
63+
frequency[number[i] - '0']++;
64+
}
65+
66+
for(char current_char = 'A'; current_char <= 'Z'; current_char++)
67+
if(possible_to_make(current_char, frequency))
68+
putchar(current_char);
69+
70+
printf("\n");
71+
}
72+
73+
-------------------------------
74+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
-------------------------------------------
2+
3+
Prefix(i) + Suffix(i) = Sum(entire array) + A[i]
4+
5+
, because A[i] is counted twice.
6+
7+
To find the i that minimises Prefix(i) + Suffix(i) is the same as finding the i for which A[i] is minimum.
8+
9+
The smallest such i is needed in case of a tie.
10+
11+
----------------------------------------------
12+
13+
void solve()
14+
{
15+
int no_of_elements;
16+
scanf("%d", &no_of_elements);
17+
18+
int minimum_element_index = 0, minimum_element = 1e9;
19+
for(int i = 1; i <= no_of_elements; i++)
20+
{
21+
int element_i;
22+
scanf("%d", &element_i);
23+
24+
if(element_i < minimum_element)
25+
minimum_element_index = i, minimum_element = element_i;
26+
}
27+
28+
printf("%d\n", minimum_element_index);
29+
}

0 commit comments

Comments
 (0)