There are N problems numbered 1..N which you need to complete. You've arranged the problems in increasing difficulty order, and the ith problem has estimated difficulty level i. You have also assigned a rating vi to each problem. Problems with similar vi values are similar in nature. On each day, you will choose a subset of the problems and solve them. You've decided that each subsequent problem solved on the day should be tougher than the previous problem you solved on that day. Also, to make it less boring, consecutive problems you solve should differ in their vi rating by at least K. What is the least number of days in which you can solve all problems?

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
There are N problems numbered 1..N which you need to complete. You've arranged the problems in increasing difficulty
order, and the ith problem has estimated difficulty level i. You have also assigned a rating vi to each problem. Problems
with similar vi values are similar in nature. On each day, you will choose a subset of the problems and solve them.
You've decided that each subsequent problem solved on the day should be tougher than the previous problem you
solved on that day. Also, to make it less boring, consecutive problems you solve should differ in their vi rating by at least
K. What is the least number of days in which you can solve all problems?
Input Format
The first line contains the number of test cases T. T test cases follow. Each case contains an integer N and K on the first
line, followed by integers v1...,.vn on the second line.
Constraints
1 <= T <= 100
1 <= N <= 300
1 <= vi <= 1000
1 <= K <= 1000
Output Format
Output T lines, one for each test case, containing the minimum number of days in which all problems can be solved.
Sample Input
2
32
547
5 1
5 3 4 5 6
Sample Output
2
1
Explanation
For the first example, you can solve the problems with rating 5 and 7 on the first day and the problem with rating 4 on
the next day. Note that the problems with rating 5 and 4 cannot be completed consecutively because the ratings should
differ by at least K (which is 2). Also, the problems cannot be completed in order 5,7,4 in one day because the problems
solved on a day should be in increasing difficulty level.
For the second example, all problems can be solved on the same day.
Transcribed Image Text:There are N problems numbered 1..N which you need to complete. You've arranged the problems in increasing difficulty order, and the ith problem has estimated difficulty level i. You have also assigned a rating vi to each problem. Problems with similar vi values are similar in nature. On each day, you will choose a subset of the problems and solve them. You've decided that each subsequent problem solved on the day should be tougher than the previous problem you solved on that day. Also, to make it less boring, consecutive problems you solve should differ in their vi rating by at least K. What is the least number of days in which you can solve all problems? Input Format The first line contains the number of test cases T. T test cases follow. Each case contains an integer N and K on the first line, followed by integers v1...,.vn on the second line. Constraints 1 <= T <= 100 1 <= N <= 300 1 <= vi <= 1000 1 <= K <= 1000 Output Format Output T lines, one for each test case, containing the minimum number of days in which all problems can be solved. Sample Input 2 32 547 5 1 5 3 4 5 6 Sample Output 2 1 Explanation For the first example, you can solve the problems with rating 5 and 7 on the first day and the problem with rating 4 on the next day. Note that the problems with rating 5 and 4 cannot be completed consecutively because the ratings should differ by at least K (which is 2). Also, the problems cannot be completed in order 5,7,4 in one day because the problems solved on a day should be in increasing difficulty level. For the second example, all problems can be solved on the same day.
8
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'problem Solving' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. INTEGER K
#
#
2. INTEGER_ARRAY V
✓def problem Solving (k, v):
# Write your code here
vif
name == __main__':
fptr = open(os.environ ['OUTPUT_PATH'], 'w')
t = int(input().strip())
first_multiple_input = input().rstrip().split()
n = int(first_multiple_input[0])
k = int(first_multiple_input[1])
V = list (map (int, input().rstrip().split()))
result = problem Solving (k, v)
fptr.write(str (result) + '\n')
fptr.close()
Transcribed Image Text:8 #!/bin/python3 import math import os import random import re import sys # # Complete the 'problem Solving' function below. # # The function is expected to return an INTEGER. # The function accepts following parameters: # 1. INTEGER K # # 2. INTEGER_ARRAY V ✓def problem Solving (k, v): # Write your code here vif name == __main__': fptr = open(os.environ ['OUTPUT_PATH'], 'w') t = int(input().strip()) first_multiple_input = input().rstrip().split() n = int(first_multiple_input[0]) k = int(first_multiple_input[1]) V = list (map (int, input().rstrip().split())) result = problem Solving (k, v) fptr.write(str (result) + '\n') fptr.close()
Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY