Another Frog-Jumping problem

3.7

3 votes
, Dynamic Programming, Algorithms
Problem

Note: This is an approximate problem. There is no exact solution. You must find the most optimal solution. Please, take a look at the sample explanation to ensure that you understand the problem correctly. Also, try not to use custom input, because the checker is specialized on test cases to check your submissions faster. Try to check your code on your own. 

Problem statement

There are N+1 stones, numbered 1,2,,N+1. For each i(1iN), the height of stone i is hi and the value of stone i is ai. There is a frog who is initially on stone 1. He will repeat the following action some number of times to reach stone N+1 : If the frog is currently on stone i, he can jump to the j -the stone if  all of the following conditions are met:

  • ijn
  • The height of the jump is no more than the width of the jump. Here, the height of the jump is defined as a maximum hi,hi+1,hj, and width as ji+1.
  • The cost of the jump is jl=ijr=i(lr)alar.
  • After, he will automatically go to j+1 -the stone.

Also, the frog has his lucky number k and he wants to reach stone N+1 in k steps so that sum of costs was minimized. Your task is to calculate the minimum sum of costs for him and find optimal jumps.

Input format 

  • The first line contains two integers N and k (1N104,1kmin(n,50)) — the length of the arrays  and the number of jumps
  • The second line contains N integers h1,h2,,hn(1hin) — the array h
  • The third line contains N integers a1,a2,,an(1ai105) — the array a
  • It is guaranteed that we can achieve N+1-th stone in k steps.

Output format 

  • The first line contains one integer - the optimal sum of costs. Next k lines contain two numbers - pairs i1,j1,...ik,jk, where l1=1,rk=n,itjt , and lt=rt1+1 for all t>1– the jumps of the frog. 

Verdict and scoring :

  • There are 6 test cases(with sample). Your score will be calculated by the following rule: for each non-sample test case, you will get min(20,20ja+1pa+1)  scores, and 0 scores for the sample. Here ja - jury's sum of costs, pa - your sum of costs. If your output does not coincide with the output format, you will get 0 scores on test.
Time Limit: 3
Memory Limit: 256
Source Limit:
Explanation

We can see that the first jump from 1 to met all of the above-mentioned conditions. The maximum in this range is 3, which is no more than the width. The cost of this jump is 6. Now frog on the 4th stone.

We can see that the first jump from 4 to met all of the above-mentioned conditions. The maximum in this range is 2, which is no more than the width. The cost of this jump is 2. Now frog on the 6th stone.

We can see that the first jump from 6 to met all of the above-mentioned conditions. The maximum in this range is 2, which is no more than the width. The cost of this jump is 2. Now frog on the 8th stone.

We can see that the first jump from to 10 met all of the above-mentioned conditions. The maximum in this range is 3, which is no more than the width. The cost of this jump is 6. Now frog on the 11th stone and finish.

Totally we have 6+2+2+6=16.

 

Editor Image

?