Sicily 7967. Book Stack

来源:互联网 发布:万得数据库 编辑:程序博客网 时间:2024/06/05 06:37

7967. Book Stack

Constraints

Time Limit: 1 secs, Memory Limit: 256 MB

Description

John has a big stack of books of various sizes. Such a stack is stable if the books have nondecreasing sizes (viewed from top to bottom); otherwise, it is unstable, and likely to fall over.
To prevent this, John wants to sort the books in the stack by size. He does so by pulling out a book from somewhere in the middle (or bottom) of the stack and then putting it back on top. However, he can only pull out a book safely if the books on top of it already form a stable stack.
For example, if John has a stack of four books with sizes 3, 4, 1 and 2 (from top to bottom) then he can sort them as follows:

Your task is to determine how many steps are required to sort a given stack of books. In the example above, which corresponds to the first sample case, the answer is 3.

Input

On the first line one positive number: the number of test cases, at most 100. After that per test case:

  • one line with an integer n (1 <= n <= 50): the number of books.
  • one line containing n space-separated integers si (1 <= si <= 1 000 for 1 <= i <= n): the sizes of the books, as they appear in the initial stack from top to bottom.

Output

Per test case:

  • one line with an integer: the minimum number of steps required to sort the stack using the algorithm described above.

Sample Input

443 4 1 283 1 4 1 5 9 2 651 42 42 42 1000224 1 2 5 6 7 9 10 3 13 17 11 12 14 19 20 22 8 15 16 18 21

Sample Output

35301234567

Problem Source

2013年每周一赛第五场暨校赛模拟赛III/BAPC 2012

// Problem#: 7967// Submission#: 3593635// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University// Solution to John's Book Stack// Author: Thomas Beuman// Time complexity: O(n^2)// Memory: O(n)// @EXPECTED_RESULTS@: CORRECT/*Solution outline:Look for topmost unsorted book, put it in its correct place, calculate how many steps this takes.Repeat until the entire stack is sorted.This solution uses a list.*/#include <cstdio>#include <list>using namespace std;typedef long long i64;int main(){ int cases, casenr, n, i, k, b;  i64 steps, s;  list<int>::iterator p, q, p2, q2;  scanf("%d\n", &cases);  for (casenr = 1; casenr <= cases; casenr++)  { scanf("%d\n", &n);    list<int> Stack;    for (i = 0; i < n; i++)    { scanf("%d", &b);      Stack.push_back(b);    }    steps = 0;    while (true)    { for (p = p2 = Stack.begin(), p++; p != Stack.end() && *p >= *p2; p++, p2++); // Look for topmost unsorted book      if (p == Stack.end()) // Done        break;      // Compute number of steps needed to put it in the right place      s = 1ll;      k = 2;      for (q = q2 = Stack.begin(), q2++; *q < *p; q++, q2++)      { if (*q == *q2)          k++;        else        { s *= k;          k = 2;        }      }      steps += s;      // Put it (p) in its correct place (q)      Stack.insert(q, *p);      Stack.erase(p);    }    printf("%lld\n", steps);  }  return 0;}                                 

0 0