#5386. [GCJ 2014 #2] Up and Down

[GCJ 2014 #2] Up and Down

[GCJ 2014 #2] Up and Down

题目描述

You are given a sequence of distinct integers $A = [A_1, A_2, ..., A_N]$, and would like to rearrange it into an up and down sequence (one where $A_1 < A_2 < ... A_{m+1} > ... > A_N$ for some index $m$, with $m$ between 1 and $N$ inclusive).

The rearrangement is accomplished by swapping two adjacent elements of the sequence at a time. Predictably, you are particularly interested in the minimum number of such swaps needed to reach an up and down sequence.

输入格式

The first line of the input gives the number of test cases, $T$. $T$ test cases follow. Each test case begins with a line containing a single integer: $N$. The next line contains $N$ distinct integers: $A_1, ..., A_N$.

输出格式

For each test case, output one line containing "Case #$x$: $y$", where $x$ is the test case number (starting from 1) and $y$ is the minimum number of swaps required to rearrange $A$ into an up and down sequence.
2
3
1 2 3
5
1 8 10 3 7
Case #1: 0
Case #2: 1

提示

**Sample Explanation**

In the first case, the sequence is already in the desired form (with m=N=3m=N=3) so no swaps are required.

In the second case, swapping 33 and 77 produces an up and down sequence (with m=3m=3).

Limits

  • 1≤T≤1001 \leq T \leq 100.
  • 1≤Ai≤1091 \leq A_i \leq 10^9.
  • The AiA_i will be pairwise distinct.

Small dataset(7 Pts)

  • Time limit: 60 3 seconds.
  • 1≤N≤101 \leq N \leq 10.

Large dataset(11 Pts)

  • Time limit: 120 5 seconds.
  • 1≤N≤10001 \leq N \leq 1000. 标签: P13257|贪心|2014|Google Code Jam

来源

P13257|[GCJ 2014 #2] Up and Down