Sunday, 19 December 2021

Team Problem !!!

 PROBLEM 40: Team


Problem Reference: Codeforces 


One day three best friends Petya, Vasya and Tonya decided to form a team and take part in programming contests. Participants are usually offered several problems during programming contests. Long before the start the friends decided that they will implement a problem if at least two of them are sure about the solution. Otherwise, the friends won't write the problem's solution.

This contest offers n problems to the participants. For each problem we know, which friend is sure about the solution. Help the friends find the number of problems for which they will write a solution.


Input:

The first input line contains a single integer n (1 ≤ n ≤ 1000) — the number of problems in the contest. Then n lines contain three integers each, each integer is either 0 or 1. If the first number in the line equals 1, then Petya is sure about the problem's solution, otherwise he isn't sure. The second number shows Vasya's view on the solution, the third number shows Tonya's view. The numbers on the lines are separated by spaces.


Output:

Print a single integer — the number of problems the friends will implement on the contest.


Solution: 


  1. n = int(input())
  2. k=0
  3. for i in range(0,n):
  4. a , b , c = input().split()
  5. if(int(a) == 1 and int(b) == 1 and int(c) == 1):
  6. k = k + 1
  7. elif(int(a) == 0 and int(b) == 1 and int(c) == 1):
  8. k = k + 1
  9. elif(int(a) == 1 and int(b) == 0 and int(c) == 1):
  10. k = k + 1
  11. elif(int(a) == 1 and int(b) == 1 and int(c) == 0):
  12. k = k + 1
  13. else:
  14. pass
  15. print(k)



The above solution is in python language.


Examples
input
3
1 1 0
1 1 1
1 0 0
output
2
input
2
1 0 0
0 1 1
output
1

Saturday, 18 December 2021

Sereja and Dima Problem !!!

 PROBLEM 39: Sereja and Dima


Problem Reference: Codeforces


Sereja and Dima play a game. The rules of the game are very simple. The players have n cards in a row. Each card contains a number, all numbers on the cards are distinct. The players take turns, Sereja moves first. During his turn a player can take one card: either the leftmost card in a row, or the rightmost one. The game ends when there is no more cards. The player who has the maximum sum of numbers on his cards by the end of the game, wins.

Sereja and Dima are being greedy. Each of them chooses the card with the larger number during his move.

Inna is a friend of Sereja and Dima. She knows which strategy the guys are using, so she wants to determine the final score, given the initial state of the game. Help her.


Input:

The first line contains integer n (1 ≤ n ≤ 1000) — the number of cards on the table. The second line contains space-separated numbers on the cards from left to right. The numbers on the cards are distinct integers from 1 to 1000.


Output:

On a single line, print two integers. The first number is the number of Sereja's points at the end of the game, the second number is the number of Dima's points at the end of the game.


Soutions:


  1. n = int(input())
  2. a = [int(a) for a in input().split()]
  3. s = []
  4. d = []
  5. i = 0
  6. j = len(a) - 1
  7. t = 0
  8. while i <= j :
  9. if t == 0:
  10. if a[i] > a[j]:
  11. s.append(a[i])
  12. i = i + 1
  13. else:
  14. s.append(a[j])
  15. j = j - 1
  16. t = 1
  17. else:
  18. if a[i] > a[j]:
  19. d.append(a[i])
  20. i = i + 1
  21. else:
  22. d.append(a[j])
  23. j = j - 1
  24. t = 0
  25.  
  26. print(sum(s),sum(d))


The above solution is in python language



Examples
input
4
4 1 2 10
output
12 5
input
7
1 2 3 4 5 6 7
output
16 12

Friday, 17 December 2021

Do Not Be Distracted Problem !!!

 PROBLEM 38: Do Not Be Distracted!


Problem Reference: Codeforces 


Polycarp has  tasks. Each task is designated by a capital letter of the Latin alphabet.

The teacher asked Polycarp to solve tasks in the following way: if Polycarp began to solve some task, then he must solve it to the end, without being distracted by another task. After switching to another task, Polycarp cannot return to the previous task.

Polycarp can only solve one task during the day. Every day he wrote down what task he solved. Now the teacher wants to know if Polycarp followed his advice.

For example, if Polycarp solved tasks in the following order: "DDBBCCCBBEZ", then the teacher will see that on the third day Polycarp began to solve the task 'B', then on the fifth day he got distracted and began to solve the task 'C', on the eighth day Polycarp returned to the task 'B'. Other examples of when the teacher is suspicious: "BAB", "AABBCCDDEEBZZ" and "AAAAZAAAAA".

If Polycarp solved the tasks as follows: "FFGZZZY", then the teacher cannot have any suspicions. Please note that Polycarp is not obligated to solve all tasks. Other examples of when the teacher doesn't have any suspicious: "BA", "AFFFCC" and "YYYYY".

Help Polycarp find out if his teacher might be suspicious.


Input:

The first line contains an integer  (). Then  test cases follow.

The first line of each test case contains one integer () — the number of days during which Polycarp solved tasks.

The second line contains a string of length , consisting of uppercase Latin letters, which is the order in which Polycarp solved the tasks.


Output:

For each test case output:

  • "YES", if the teacher cannot be suspicious;
  • "NO", otherwise.

You may print every letter in any case you want (so, for example, the strings yEsyesYes and YES are all recognized as positive answer).


Solution:


  1. t = int(input())
  2. while 0 < t :
  3. n = int(input())
  4. s = input()
  5. a = set()
  6. i = 1
  7. while i < len(s):
  8. if s[i-1] != s[i]:
  9. a.add(s[i-1])
  10. if s[i] in a:
  11. print("NO")
  12. break
  13. i = i + 1
  14. if s[i-1] not in a:
  15. print("YES")
  16. t = t - 1


The above solution is in python language.


Example
input
5
3
ABA
11
DDBBCCCBBEZ
7
FFGZZZY
1
Z
2
AB
output
NO
NO
YES
YES
YES

Soft Drinking Problem !!!

 PROBLEM 37: Soft Drinking


Problem Reference : Codeforces


This winter is so cold in Nvodsk! A group of n friends decided to buy k bottles of a soft drink called "Take-It-Light" to warm up a bit. Each bottle has l milliliters of the drink. Also they bought c limes and cut each of them into d slices. After that they found p grams of salt.

To make a toast, each friend needs nl milliliters of the drink, a slice of lime and np grams of salt. The friends want to make as many toasts as they can, provided they all drink the same amount. How many toasts can each friend make?


Input:

The first and only line contains positive integers nklcdpnlnp, not exceeding 1000 and no less than 1. The numbers are separated by exactly one space.


Output:

Print a single integer — the number of toasts each friend can make.


Solution: 


  1. n, k, l, c, d, p, nl, np = input().split()
  2. print(min((int(k) * int(l)) // int(nl) , int(c) * int(d) , int(p) // int(np)//int(n))


The above solution is in python language.


Examples
input
3 4 5 10 8 100 3 1
output
2
input
5 100 10 1 19 90 4 3
output
3
input
10 1000 1000 25 23 1 50 1
output
0

Friday, 10 December 2021

Dislike of Threes Problem !!!

 PROBLEM 36: Dislike of Threes


Problem Reference: Codeforces


Polycarp doesn't like integers that are divisible by  or end with the digit  in their decimal representation. Integers that meet both conditions are disliked by Polycarp, too.

Polycarp starts to write out the positive (greater than  Output the -th element of this sequence (the elements are numbered from ).


Input:

The first line contains one integer  () — the number of test cases. Then  test cases follow.

Each test case consists of one line containing one integer  ().


Output:

For each test case, output in a separate line one integer  — the -th element of the sequence that was written out by Polycarp.


Solution:


  1. t = int(input())
  2. while 0 < t:
  3. n = int(input())
  4. i = 1
  5. j = 1
  6. while j == 1:
  7. if i % 3 == 0 or i % 10 == 3:
  8. i = i + 1
  9. else:
  10. n = n - 1
  11. if n == 0:
  12. print(i)
  13. break
  14. i = i + 1
  15. t = t - 1


The above solution is in python language.



Example
input
10
1
2
3
4
5
6
7
8
9
1000
output
1
2
4
5
7
8
10
11
14
1666

Rearrange an array with O(1) extra space

  PROBLEM 61:  Rearrange an array with O(1) extra space (For best view experience, view in windows version) Problem Reference : GeeksForGeek...