codeforces Rockethon 2015

比赛链接:http://codeforces.com/contest/513

A. Game

time limit per test 2 seconds

memory limit per test 256 megabytes

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 test(s)

input

2 2 1 2

output

Second

input

2 1 1 1

output

First

Note

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.

题目大意,两个人玩游戏,开始A的盒子有n1个球,B的盒子有n2个球,A先开始取球,A可以取1-k1个,B再取,B可以取1-k2个,直到一方没有球可取则另一方胜

题目分析:因为两个人都会选择最佳策略,那就都取1个,因为取的少剩的多可选择的余地更大,这样就与k1,k2无关了,比价一下n和m的大小即可,注意因为A先,所以n相等时A先取完则B胜

#include <cstdio>

int main()
{
    int n1, n2, k1, k2;
    scanf("%d %d %d %d", &n1, &n2, &k1, &k2);
    if(n1 > n2)
        printf("First\n");
    else
        printf("Second\n");
}

B1.B2. Permutations

time limit per test 2 seconds

memory limit per test 256 megabytes

You are given a permutation p of numbers 1,?2,?...,?n. Let‘s define f(p) as the following sum:

Find the lexicographically m-th permutation of length n in the set of permutations having the maximum possible value of f(p).

Input

The single line of input contains two integers n and m (1?≤?m?≤?cntn), where cntn is the number of permutations of length n with maximum possible value of f(p).

The problem consists of two 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 B1 (3 points), the constraint 1?≤?n?≤?8 will hold.

In subproblem B2 (4 points), the constraint 1?≤?n?≤?50 will hold.

Output

Output n number forming the required permutation.

Sample test(s)

input

2 2

output

2 1

input

3 2

output

1 3 2

Note

In the first example, both permutations of numbers {1, 2} yield maximum possible f(p) which is equal to 4. Among them, (2,?1) comes second in lexicographical order.

题目大意:如题所给公式,求和最大的所有排列中的第m个

题目分析:yy,写出3和4时的各种情况,根据各数字出现的位置找规律,得到以下yy结论

拿m-1和1 2 3 ... n-1的二进制取与运算,结果为0则从左往右放,否则从右往左放

#include <iostream>
using namespace std;
long long m, n, ans[50];

int main()
{
    cin >> n >> m;
    int l = 0, r = n - 1;
    for(int i = 1; i <= n; i++)
    {
        if(i == n)
        {
            if((m - 1) & ((1ll << n) - 1))
                ans[r--] = i;
            else
                ans[l++] = i;
        }
        else
        {
            if((m - 1) & (1ll << (n - i - 1)))
                ans[r--] = i;
            else
                ans[l++] = i;
        }
    }
    for(int i = 0; i < n; i++)
        cout << ans[i] << ' ';
    cout << endl;
}

G1. Inversions problem

time limit per test2 seconds

memory limit per test256 megabytes

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 test(s)

input

3 1

1 2 3

output

0.833333333333333

input

3 4

1 3 2

output

1.458333333333334

Note

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次操作,每次选两个数(可以相同)然后倒排他们包括他们之间的所有数,求最后总的逆序对个数的期望

题目分析:因为G1数据很小,暴力深搜一下就行了,分母就是分段的种类个数即1+2+3+...+n,分子是逆序对个数

#include <cstdio>
#include <algorithm>
using namespace std;
int n, a[105];
double all;

double sum(int num)
{
    double tmp = 0.0;
    for(int i = 1; i <= num; i++)
        tmp += i;
    return tmp;
}

double cal(int k)
{
    if(k == 0)
    {
        double tmp = 0;
        for(int i = 0; i < n; i++)
            for(int j = i + 1; j < n; j++)
                if(a[i] > a[j])
                    tmp++;
        return tmp;
    }
    double ans = 0;
    for(int i = 0; i < n; i++)
    {
        for(int j = i + 1; j <= n; j++)
        {
            reverse(a + i, a + j);
            ans += cal(k - 1);
            reverse(a + i, a + j);
        }
    }
    return ans / all;
}

int main()
{
    int k;
    scanf("%d %d", &n, &k);
    for(int i = 0; i < n; i++)
        scanf("%d", &a[i]);
    all = sum(n);
    printf("%.10f\n", cal(k));
}
时间: 2024-10-08 10:39:43

codeforces Rockethon 2015的相关文章

CODEFORCES Rockethon 2015 B. Permutations

You are given a permutation p of numbers 1,?2,?-,?n. Let's define f(p) as the following sum: Find the lexicographically m-th permutation of length n in the set of permutations having the maximum possible value of f(p). Input The single line of input

Codeforces Rockethon 2015 C

Problem N个公司在拍卖标价一件商品,每个公司会在各自的[Li,Ri]区间内等概率选取整数出价.出最高价的公司会赢得商品,若有多个公司出最高价,随机一个公司赢得商品.但是,有一条特殊规矩,赢得商品的公司所需付的钱不是它所标价的钱,而是除去它以外的其它公司所标价的钱的最大值.现求,赢得的公司所需付钱的期望. Limits TimeLimit(ms):2000 MemoryLimit(MB):256 N∈[2,5] Li,Ri∈[1,10000] Look up Original Proble

Codeforces Round Rockethon 2015

A. Game 题目大意:A有N1个球,B有N2个球,A每次可以扔1-K1个球,B每次可以扔1-K2个球,谁先不能操作谁熟 思路:.....显然每次扔一个球最优.... 1 #include<iostream> 2 #include<cstdio> 3 #include <math.h> 4 #include<algorithm> 5 #include<string.h> 6 #include<queue> 7 #define MOD

Rockethon 2015

A Game题意:A,B各自拥有两堆石子,数目分别为n1, n2,每次至少取1个,最多分别取k1,k2个, A先取,最后谁会赢. 分析:显然每次取一个是最优的,n1 > n2时,先手赢. 代码: 1 #include <bits/stdc++.h> 2 #define pb push_back 3 #define mp make_pair 4 #define esp 1e-14 5 #define lson l, m, rt<<1 6 #define rson m+1, r,

codeforces 391E2 (【Codeforces Rockethon 2014】E2)

/* 题意:有三棵树,每颗树有ni个结点,添加两条边把这三棵树连接起来,合并成一棵树,使得树中任意两点之间的最短路径 的和最大. 分析: 三棵树要合并成一棵树,则第一棵树必须选择一个点,假设为X,第二棵树必须选择两个点,假设为Y1, Y2,第三棵树必须选择一个点,假设为Z 记第一棵树中所有结点到X的路径总和为:tot1 第二棵树中所有结点到Y1,Y2的路径总和分别为:tot2, tot3 第三棵树中所有结点到Z的路径总和为:tot4; 共有四种情况: 1,每棵树内部的结点之间的距离为常数,可以求

A. Game

这是 Rockethon 2015 的一道题,也是我做codeforces的第一道题,在这里留个纪念.原题在这. 题意为:小明有n1个球,小红有n2个球,每轮游戏,小明可以扔掉a个球(1 <= a <= k1), 小红可以扔掉 (1 <= b <= k2)个球,小明先开始游戏,在小明和小红不是sb的情况下,要是轮到谁了,谁的球扔完了,那么这个人就输了. 输入 n1, n2, k1, k2 , 输出 First (如果第一个人赢)或者 Second (要是第二个人赢) 我就想,要是小

阿尔红军我让我特我问题沃特尔行业

http://www.houzz.com/ideabooks/38419124/thumbs/2015.01.04 http://www.houzz.com/ideabooks/38419135/thumbs/2015.01.04 http://www.houzz.com/ideabooks/38419147/thumbs/2015.01.04 http://www.houzz.com/ideabooks/38419107/thumbs/2015.01.04 http://www.houzz.c

哪敢跟学长这么

不少人面庞上有不由得惊呼出声http://weibo.com/09.16/2015/p/1001603887569338240338http://weibo.com/09.16/2015/p/1001603887569338268443http://weibo.com/09.16/2015/p/1001603887569342462767http://weibo.com/09.16/2015/p/1001603887569342462769http://weibo.com/09.16/2015/

右手缓缓握拢而

火红烈日炸裂的一路冲杀进去吧http://weibo.com/2015/09/16/p/1001603887216807041204http://weibo.com/2015/09/16/p/1001603887216811186273http://weibo.com/2015/09/16/p/1001603887216811186277http://weibo.com/2015/09/16/p/1001603887216811235528http://weibo.com/2015/09/16/