Codeforces 351B B. Jeff and Furik (probability)

Source: Internet
Author: User

Topic Links:

Codeforces 351B

Main topic:

Give a game, the initiator select a pair of adjacent number Exchange location, the back of a random logarithm, and then toss a coin to decide whether to exchange, if not exchange, then re-find. Ask the approximate sequence of the desired number of cloth to take the optimal strategy.

Topic Analysis:
  • Definition Dp[i] represents the expectation of reducing the number of steps in the I reverse order.
  • d P[I]=1+1+d P[I?2]?0.5+d P[I?1+1]]?0.5
  • Because the initiator can choose, so it will always take action to reduce an inverse pair.
  • We get dp[i] = 4 + dp[i-2],dp[0] = 0, dp[1] = 1;
AC Code:
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#define MAX 3007using namespace STD;intN,a[max];intMain () { while( ~scanf("%d", &n)) { for(inti =0; I < n; i++)scanf("%d", &a[i]);intm =0; for(inti =1; I < n; i++) for(intj =0; J < I; J + +)if(A[j] > A[i]) m++;intAns =0;if(m&1) ans =1+ m/2*4;ElseAns = m*2;printf("%.7f\n", (Double) ans); }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Codeforces 351B B. Jeff and Furik (probability)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.