doraemongrapes's blog

By doraemongrapes, 10 years ago, In English

I'm having some difficulties with this problems? Can you help me?

I think it can be solved by using Dynamic programming on tree, isn't it?

http://www.spoj.com/problems/KINGDOM/

Full text and comments »

Tags dp, spoj
  • Vote: I like it
  • +12
  • Vote: I do not like it

By doraemongrapes, 10 years ago, In English

We have an array of integer a[1], a[2], ... , a[N] and a number M.

We take a version of selection sort like this:

for i := 1 to M do
    for j := i + 1 to N do
         if (a[i] > a[j]) then swap(a[i], a[j])

After the program end, count the number of swap operator in it.

Limitation:

1 <= M <= N <= 105

abs(ai) <= 109

Example:

Input:

4 2
3 2 -1 -4

Output:

5

Explain:

After first loop of j: -4 3 2 -1

After second loop of j: -4 -1 3 2

So the number of swap operator is 5.

Full text and comments »

  • Vote: I like it
  • +34
  • Vote: I do not like it