for _ in range(int(input())):
n,k=map(int,input().split())
f=list(map(int,input().split()))
dp=[]
dp.append(0)
for i in range(1,n):
dp.append(k+dp[i-1])
dic={}
c=0
for j in range(i,0,-1):
if f[j] not in dic.keys():
dic[f[j]]=1
else:
dic[f[j]]+=1
if(dic[f[j]]==2):
c+=2
elif dic[f[j]]>2:
c+=1
dp[i]=min(dp[i],dp[j-1]+k+c)
print(dp[-1])
can someone tell me why my code is not giving the correct answer in all the test cases?its showing WA.
assume 1 guest sits in one table(n guests on n tables) find the answer for it ,then assume 2 guests sit on one table(n guests on n/2 tables) find answer for it … keep doing this uptill n guests sit on one table and keep track of the minimum inefficiency you can do the same from back of the array.
May I have the Test Case for Sub-Task 2, Task# 2 for the problem with its answer for which my following code shows ‘WA’ ? It would be a great help for me.
I have done this using greedy and I got 100 marks,maybe the test cases were weak.
Here’s my approach:-
–> initially use 1 table for each person(i.e N tables for N persons) and I stored this information in a vector called ‘table’ and table.size() represents the number of table I have been using.
–> now run a loop while(table.size()>=1)
–> in each while loop iteration check whether we can merge 2 table or not, In a single loop iteration check all the possibility of merging two adjacent table and choose one which results in minimum total cost.
–> doing this we will be decreasing number of table used by one(merging 2 table into 1) in each iteration.
–> our ans will be the minimum of all the iteration.
NOTE: this version of my solution was not working when the optimal ans will consist of using exactly 2 table So i handled that separately(just dividing the whole array into two segment in all possible manner first try to divide at index 1 then 2 then 3 and so on… and choose the optimal out of those O(n) possibility).
Can we solve this question without using dp ???
I tried but 2 testcase of subtask 2 are failed !!
Please tell me where it’s going wrong!! https://www.codechef.com/viewsolution/36585127
I got 8 of 10 subtasks correct. Got wrong on test case 2 and 3.
So my approach was to calculate first inefficiency for one table with all members and also the overall frequency of members from each family.
Then I iterated through the list of members, adding one to a variable say NOS whenever I encountered the first member from a family which would have an argument. I iterated till i encountered the second member from a family, at which i checked whether NOS would be greater or equal than cost of a table (since its feasible to split only when no. of arguments are less than cost of adding a table). If it was less than cost of table, i continued until another such condition took place. Then whenever i found a member which is second last, i added one to NOS. And when i encountered the last member from a family, i checked whether NOS was greater than or equal to cost of table.
Any good soul could spend some time to help out?
I solved it without DP and just with maps. It passed every test cases except two(2 AND 3). Can anyone help me with this? CodeChef: Practical coding for everyone
This is my code
…
I have one question, instead of passing one index to getAns() function, I passed two indices, a start and an end and then did two recursive calls but that gives a TLE, why is it redundant to pass two indices. Can you explain?