Блог пользователя Neev4321

Автор Neev4321, история, 7 недель назад, По-английски

I am stuck on this problem.

Problem Synopsis:
You are given an array A of size N.
Consider an undirected edge-weighted graph with N nodes numbered from 1 to N such that for any two nodes i and j there is an undirected edge between i and j with weight |i-j| * max(A[i], A[j]).
Find the weight of the shortest path between two given nodes X and Y.

Input Format:
The first line contains a single integer T, the number of test cases.
The first line of each test case contains three integers N, X and Y.
The second line of each test case contains N integers, A[1] A[2] ... A[N].

Output Format
For each test case, print a single integer, the weight of the shortest path, on a separate line.

I have a working solution for the first 3 subtasks but have run out of ideas for the last one.
Any help would be appreciated.

my solution
  • Проголосовать: нравится
  • +3
  • Проголосовать: не нравится

»
6 недель назад, # |
  Проголосовать: нравится +13 Проголосовать: не нравится

I found the solution (which surprisingly, builds upon the last solution)!
Leaving this here for anyone also searching for the solution.

my code
»
6 недель назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Hello, Did you come up with any solution to solve Problem B: Two Arrays of the same contest? I would like to know your approach.

  • »
    »
    6 недель назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    I used DP (dynamic programming) with the recurrence:

    f(n, k) = min(
        f(n-1, k-1) + cost(n, n),
        f(n-2, k-1) + cost(n-1, n),
        ...,
        f(1, k-1) + cost(2, n)
    )
    

    This recurrence can be simplified to:

    f(n, k) = min(best_a, best_b)
    best_a = min(
        f(n-1, k-1) + sum_a(n, n),
        f(n-2, k-1) + sum_a(n-1, n),
        ...,
        f(1, k-1) + sum_a(2, n),
    )
    best_b = min(
        f(n-1, k-1) + sum_b(n, n),
        f(n-2, k-1) + sum_b(n-1, n),
        ...,
        f(1, k-1) + sum_b(2, n),
    )
    
    my code