zoukankan      html  css  js  c++  java
  • Codeforces 351B Jeff and Furik

    题意:对于一个序列,两个人轮流玩游戏 ,第一个人先玩,它会交换选择相邻的并且交换,第二个后玩,他有0各有.5的机会使得逆序数加一或者加一,序列单调递增时结束,第一个人总想要玩得步数最小,那么步数的期望值是多少。

    解题思路:原来是个数学题 哭了。

    解题代码:

     1 // File Name: 351b.cpp
     2 // Author: darkdream
     3 // Created Time: 2015年03月25日 星期三 10时34分48秒
     4 
     5 #include<vector>
     6 #include<list>
     7 #include<map>
     8 #include<set>
     9 #include<deque>
    10 #include<stack>
    11 #include<bitset>
    12 #include<algorithm>
    13 #include<functional>
    14 #include<numeric>
    15 #include<utility>
    16 #include<sstream>
    17 #include<iostream>
    18 #include<iomanip>
    19 #include<cstdio>
    20 #include<cmath>
    21 #include<cstdlib>
    22 #include<cstring>
    23 #include<ctime>
    24 #define LL long long
    25 #define maxn 3005
    26 using namespace std;
    27 int a[maxn]; 
    28 int tree[maxn];
    29 int lowbit(int x)
    30 {
    31    return x & -x ; 
    32 }
    33 int n ;
    34 void update(int x)
    35 {
    36     while(x <= n)
    37     {
    38        tree[x] += 1;
    39        x += lowbit(x);
    40     }
    41 }
    42 int  getsum(int x)
    43 {
    44   int ans = 0 ; 
    45   while(x >= 1 )
    46   {
    47      ans += tree[x];
    48      x -= lowbit(x);
    49   }
    50   return ans ;
    51 }
    52 int main(){
    53     scanf("%d",&n);
    54     int nx = 0 ; 
    55     for(int i= 1;i <= n;i ++)
    56     {
    57       scanf("%d",&a[i]);
    58       nx += i - getsum(a[i]) - 1;
    59       //printf("%d
    ",nx);
    60       update(a[i]);
    61     }
    62     if(nx % 2 == 1 )
    63         printf("%lf",2*nx-1.0);
    64     else printf("%lf",2.0*nx);
    65          
    66 return 0;
    67 }
    View Code
    没有梦想,何谈远方
  • 相关阅读:
    浏览器原理
    jQ插件编写
    [转]清理浮动的全家
    百度面试~~
    LeetCode-222. Count Complete Tree Nodes
    LeetCode-236. Lowest Common Ancestor of a Binary Tree
    LeetCode-235. Lowest Common Ancestor of a Binary Search Tree
    LeetCode-102. Binary Tree Level Order Traversal
    LeetCode-404. Sum of Left Leaves
    LeetCode-257. Binary Tree Paths
  • 原文地址:https://www.cnblogs.com/zyue/p/4375846.html
Copyright © 2011-2022 走看看