Skip to main content

Magical Candy Store november long challenge problem solution code with explaination

 Magical Candy Store november long challenge problem solution code with explaination-

Magical Candy Store November long challenge problem solution code with explanation
Let's Read the problem statement.

Chef and Chefu are at a magical candy store playing a game with the following rules:

  • There are two candy counters; each of them stores an infinite number of candies. At any time, only one of the counters is open and the other is closed.
  • Exactly one player is present at each of the counters. Initially, Chef is at the open counter and Chefu is at the closed counter.
  • There is a sequence of N distinct integers A1,A2,,AN. The game consists of R turns; in the i-th turn, the open counter offers only C=A(i1)%N+1 candies to the player present at this counter. This player should choose a positive number of candies M to accept, where 1MC.
  • If this player accepts an odd number of candies, the players have to swap their positions (each player goes to the other counter).
  • After each N turns, the counter which was currently open is closed and the counter which was currently closed is opened.
  • The primary goal of each player is to maximise his own number of candies after R turns. As a second priority, each player wants to minimise the number of candies his opponent has after R turns.

You should process Q queries. In each query, you are given R and you should find the number of candies Chef has after R turns, assuming that both players play the game optimally. Since this number could be very large, compute it modulo 109+7.

Input

  • The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
  • The first line of each test case contains a single integer N.
  • The second line contains N space-separated integers A1,A2,,AN.
  • The third line contains a single integer Q.
  • Each of the next Q lines contains a single integer R describing a query.

Output

For each query, print a single line containing one integer ― the maximum number of candies Chef can get, modulo 109+7.

Constraints

  • 1T25
  • 1N105
  • 1Ai109 for each valid i
  • A1,A2,,AN are pairwise distinct
  • 1Q105
  • 1R1012
  • the sum of N+Q over all test cases does not exceed 106

Subtasks

Subtask #1 (15 points):

  • N10
  • Q35
  • R35

Subtask #2 (85 points): original constraints

Example Input

1
4
4 10 2 1
2
4
5

Example Output

17
21

Magical Candy Store november long challenge problem solution code with explaination

Explanation

Example case 1: In the 1-st, 2-nd and 3-rd turn, Chef takes 410 and 2 candies (16 in total) respectively. In the 4-th turn, Chef takes 1 candy (17 in total; this is the answer to the first query), which is odd and hence he has to go to the counter which is closed. However, since N=4 turns are just completed, the counter which was currently open closes and the other one (where Chef went) opens. In the 5-th round, Chef can take 4 candies, so he has 21 candies.

link-https://www.codechef.com/NOV20B/problems/CNDYGAME


Solution-

Must Join Telegram channel for Editorial -

Hint- 
Here is major role of element 1.

for input-
3
5
1 2 3 4 5
7
1
2
3
5
6
10
11
5
2 3 4 5 1
10
1
2
3
5
6
7
10
11
20
21
5
2 3 1 4 5
12
1
2
3
4
5
6
7
8
9
10
11
12
output should be-
1
1
1
1
1
2
2
2
5
8
13
15
18
26
28
52
54
2
5
5
9
14
16
19
19
23
28
30
33

Solution -

I hope you read the hint actually this question is totally based on logical thinking. 
once you get the logic it is very easy to code.
Here are many cases which you have to think and handle them.
First of all, assume that there is no 1 in the given sequence since the chef is so cleaver  this means that the chef  never want to handover any turn to chefu. 

The chef always try to handover his turn to chefu if anytime 1 occurred. because for element 1 there is no selection choice left for chef and chefu. 
Since Chef is playing first so he will choose even number and he will choose odd when next element is 1. so that chefu have to choose 1 because he has no choice left.

We can make 4 cases here and ans can differ according to the poison of 1.
 
 Case 1- if No 1 present.
Case 2- if 1 present at the beginning.
case 3- if 1 present at the mid.
case 4 if 1 present at the last.

If you are able to handle these cases successfully you can solve this question easily.

To perform this task more effectively make a prefix array.And chose always even except the last position.

For Video editorial follow video below-


Now see my code-



Comments

Popular posts from this blog

codeforces rating system | Codeforces rating Newbie to Legendary Grandmaster

 Codeforces rating system | Codeforces rating Newbie to Legendary Grandmaster- Codeforces is one of the most popular platforms for competitive programmers and  codeforces rating matters a lot  .  Competitive Programming  teaches you to find the easiest solution in the quickest possible way. CP enhances your problem-solving and debugging skills giving you real-time fun. It's brain-sport. As you start solving harder and harder problems in live-contests your analytical and rational thinking intensifies. To have a good codeforces profile makes a good impression on the interviewer. If you have a good  codeforces profile so it is very easy to get a referral for product base company like amazon, google , facebook etc.So in this blog I have explained everything about codeforces rating system. What are different titles on codeforces- based on rating codeforces divide rating into 10 part. Newbie Pupil Specialist Expert Candidate Codemaster Master International Master Grandmaster Internat

Apple Division CSES Problem Set Solution | CSES Problem Set Solution Apple division with code

 Apple Division CSES Problem Set Solution | CSES Problem Set Solution Apple division with code - Apple Division CSES Problem Solution Easy Explanation. Apple division is problem is taken form cses introductory problem set.Let's Read Problem statement first. Problem Statement- Time limit:  1.00 s   Memory limit:  512 MB There are  n n  apples with known weights. Your task is to divide the apples into two groups so that the difference between the weights of the groups is minimal. Input The first input line has an integer  n n : the number of apples. The next line has  n n  integers  p 1 , p 2 , … , p n p 1 , p 2 , … , p n : the weight of each apple. Output Print one integer: the minimum difference between the weights of the groups. Constraints 1 ≤ n ≤ 20 1 ≤ n ≤ 20 1 ≤ p i ≤ 10 9 1 ≤ p i ≤ 10 9 Example Input: 5 3 2 7 4 1 Output: 1 Explanation: Group 1 has weights 2, 3 and 4 (total weight 9), and group 2 has weights 1 and 7 (total weight 8). Join Telegram channel for code discussi

Movie Festival CSES problem set solution

 Movie Festival CSES problem set solution - Problem statement-  Time limit:  1.00 s   Memory limit:  512 MB In a movie festival  n n  movies will be shown. You know the starting and ending time of each movie. What is the maximum number of movies you can watch entirely? Input The first input line has an integer  n n : the number of movies. After this, there are  n n  lines that describe the movies. Each line has two integers  a a  and  b b : the starting and ending times of a movie. Output Print one integer: the maximum number of movies. Constraints 1 ≤ n ≤ 2 ⋅ 10 5 1 ≤ n ≤ 2 ⋅ 10 5 1 ≤ a < b ≤ 10 9 1 ≤ a < b ≤ 10 9 Example Input: 3 3 5 4 9 5 8 Output: 2 Solution - Step -1 take input in a vector as a pair first element is ending time ans second element is starting time Step -2 sort the vector in increasing order because we will watch that movie which is ending first. Step -3 iterate over vector and calculate ans .           follow code below-