G - Jeff and Furik 逆序对 + 贪心

偏执的太偏执、 2021-12-11 08:01 276阅读 0赞

G - Jeff and Furik

Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u

Submit Status

Appoint description: System Crawler (2013-10-13)

Description

Jeff has become friends with Furik. Now these two are going to play one quite amusing game.

At the beginning of the game Jeff takes a piece of paper and writes down a permutation consisting of n numbers: p1, p2, …, p**n. Then the guys take turns to make moves, Jeff moves first. During his move, Jeff chooses two adjacent permutation elements and then the boy swaps them. During his move, Furic tosses a coin and if the coin shows “heads” he chooses a random pair of adjacent elements with indexes i and i + 1, for which an inequality p**i > p**i + 1 holds, and swaps them. But if the coin shows “tails”, Furik chooses a random pair of adjacent elements with indexes i and i + 1, for which the inequality p**i < p**i + 1 holds, and swaps them. If the coin shows “heads” or “tails” and Furik has multiple ways of adjacent pairs to take, then he uniformly takes one of the pairs. If Furik doesn’t have any pair to take, he tosses a coin one more time. The game ends when the permutation is sorted in the increasing order.

Jeff wants the game to finish as quickly as possible (that is, he wants both players to make as few moves as possible). Help Jeff find the minimum mathematical expectation of the number of moves in the game if he moves optimally well.

You can consider that the coin shows the heads (or tails) with the probability of 50 percent.

Input

The first line contains integer n(1 ≤ n ≤ 3000). The next line contains n distinct integers p1, p2, …, p**n(1 ≤ p**i ≤ n) — the permutation p. The numbers are separated by spaces.

Output

In a single line print a single real value — the answer to the problem. The answer will be considered correct if the absolute or relative error doesn’t exceed 10 - 6.

Sample Input

Input

  1. 2
  2. 1 2

Output

  1. 0.000000

Input

  1. 5
  2. 3 5 2 4 1

Output

  1. 13.000000
  2. const int INF = 1000000000;
  3. const double eps = 1e-8;
  4. const int maxn = 30000;
  5. int a[maxn];
  6. int main()
  7. {
  8. //freopen("in.txt","r",stdin);
  9. int n;
  10. while(cin>>n)
  11. {
  12. repf(i,1,n) scanf("%d",&a[i]);
  13. int sum = 0;
  14. repd(i,n,1)
  15. repf(j,1,i-1)
  16. {
  17. if(a[j] > a[i]) sum++;
  18. }
  19. double ans = sum + sum/2*2;
  20. printf("%.7lf\n",ans);
  21. }
  22. return 0;
  23. }

转载于:https://www.cnblogs.com/DreamHighWithMe/p/3451630.html

发表评论

表情:
评论列表 (有 0 条评论,276人围观)

还没有评论,来说两句吧...

相关阅读

    相关 P1908

    P1908 逆序对 题意: > 给你一个长度为 $ n $ 的数组,求其中的逆序对数量。 解法: > 数据范围很大 $ (n \\leq 5 \\t...

    相关

    题型: 编程题 语言: 不限定 Description 在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序。 一个排列

    相关 1013

    Description 给定一个长度为N的int型数组a[0,1,2,...N-1], 请计算逆序对个数.当i<j且a[i]>a[j], 则称a[i]与a[j]是一对

    相关 个数

    题目描述 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数P。并将P对1000000007取模的