CodeForces 513G1 CodeForces 513A

来源:互联网 发布:linux加入windows域 编辑:程序博客网 时间:2024/06/05 16:01


Description

You are given a permutation of n numbers p1, p2, ..., pn. We perform k operations of the following type: choose uniformly at random two indices l and r (l ≤ r) and reverse the order of the elements pl, pl + 1, ..., pr. Your task is to find the expected value of the number of inversions in the resulting permutation.

Input

The first line of input contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 109). The next line contains n integers p1, p2, ..., pn — the given permutation. All pi are different and in range from 1 to n.

The problem consists of three subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows.

  • In subproblem G1 (3 points), the constraints 1 ≤ n ≤ 6, 1 ≤ k ≤ 4 will hold.
  • In subproblem G2 (5 points), the constraints 1 ≤ n ≤ 30, 1 ≤ k ≤ 200 will hold.
  • In subproblem G3 (16 points), the constraints 1 ≤ n ≤ 100, 1 ≤ k ≤ 109 will hold.

Output

Output the answer with absolute or relative error no more than 1e - 9.

Sample Input

Input
3 11 2 3
Output
0.833333333333333
Input
3 41 3 2
Output
1.458333333333334

Hint

Consider the first sample test. We will randomly pick an interval of the permutation (1, 2, 3) (which has no inversions) and reverse the order of its elements. With probability , the interval will consist of a single element and the permutation will not be altered. With probability  we will inverse the first two elements' order and obtain the permutation (2, 1, 3) which has one inversion. With the same probability we might pick the interval consisting of the last two elements which will lead to the permutation (1, 3, 2) with one inversion. Finally, with probability  the randomly picked interval will contain all elements, leading to the permutation (3, 2, 1) with 3 inversions. Hence, the expected number of inversions is equal to .

题意:

给定1到N的序列,进行K次操作,每次操作取两个数进行倒转,并倒转他们中间的数(两个数可以相同),求K次操作后逆序数对个数的期望。做法:用DFS搜索一下就好了,深度为K。

上代码

#include <stdio.h>#include <algorithm>using namespace std;#include <string.h>int n,a[150];double s1;double dfs(int k){int i,j;    if(k==0)    {        double p=0;        for(i=0;i<n;i++)            for(j=i+1;j<n;j++)                if(a[i]>a[j])                    p++;return p; //求逆序数    }    double ans=0;    for(i=0;i<n;i++)    {        for(j=i+1;j<=n;j++)        {            reverse(a+i,a+j);            ans+=dfs(k-1);  //深度为K。            reverse(a+i,a+j);        }    }    return ans / s1;  //返回期望值。}int main(){    int k,i;//int b;    scanf("%d%d",&n,&k);s1=0;    for(i=0;i<n;i++){scanf("%d",&a[i]);s1+=a[i];}    printf("%.15lf\n",dfs(k));//保留10位以上。   //scanf("%d",&b);}

Description

Two players play a simple game. Each player is provided with a box with balls. First player's box contains exactly n1 balls and second player's box contains exactly n2 balls. In one move first player can take from 1 to k1 balls from his box and throw them away. Similarly, the second player can take from 1 to k2 balls from his box in his move. Players alternate turns and the first player starts the game. The one who can't make a move loses. Your task is to determine who wins if both players play optimally.

Input

The first line contains four integers n1, n2, k1, k2. All numbers in the input are from 1 to 50.

This problem doesn't have subproblems. You will get 3 points for the correct submission.

Output

Output "First" if the first player wins and "Second" otherwise.

Sample Input

Input
2 2 1 2
Output
Second
Input
2 1 1 1
Output
First

Hint

Consider the first sample test. Each player has a box with 2 balls. The first player draws a single ball from his box in one move and the second player can either take 1 or 2 balls from his box in one move. No matter how the first player acts, the second player can always win if he plays wisely.

水题一道,题意:两个箱子两个人,每个箱子里面有球,第一个人可以取1到k1个,第二个1到k2个。先取完的为输。问谁赢。最优取→_→当然是一个一个取了。。题目是第一个人先取,所以只要第一个人的球数小于等于第二个人的球数,那就必输。

上代码

#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>using namespace std;int main(){int n1,n2,k1,k2;while(scanf("%d%d%d%d",&n1,&n2,&k1,&k2)!=EOF){ if(n1>n2) printf("First\n"); else if(n1<n2) printf("Second\n"); else  printf("Second\n");}}


0 0
原创粉丝点击