Sicily 7967. Book Stack

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

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

Sample Output

3
53
0
1234567

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;
}                                 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值