2014ACM/ICPC亚洲区域赛现场赛D和K题解题报告

来源:互联网 发布:贝恩财团 知乎 编辑:程序博客网 时间:2024/05/01 03:27
Domination

Time Limit: 8 Seconds      Memory Limit: 131072 KB      Special Judge

Edward is the headmaster of Marjar University. He is enthusiastic about chess and often plays chess with his friends. What's more, he bought a large decorative chessboard with N rows andM columns.

Every day after work, Edward will place a chess piece on a random empty cell. A few days later, he found the chessboard was dominated by the chess pieces. That means there is at least one chess piece in every row. Also, there is at least one chess piece in every column.

"That's interesting!" Edward said. He wants to know the expectation number of days to make an empty chessboard of N × M dominated. Please write a program to help him.

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

There are only two integers N and M (1 <= NM <= 50).

Output

For each test case, output the expectation number of days.

Any solution with a relative or absolute error of at most 10-8 will be accepted.

Sample Input

21 32 2

Sample Output

3.000000000000

2.666666666667

zoj3822

这题™坑了我们队。。。以前没做过概率dp,结果悲剧了,当成组合数学来做了,QAQ

赛后根据这题学习了下概率dp

题意:求放石子使得每行没列都有石子个数的期望
思路:先求概率,然后再用期望公式计算,设dp[i][j][k]表示放i个石子后行有j,列有k至少有一个石子的概率,然后就是4种情况的讨论,1.使得行和列都加1,2.行加1,3.列加1
4.行和列都不加1
注意当i==N并且j==M的时候没有dp[i][j][k]不能加上dp[i][j][k-1]*(i*j-k+1)/(N*M-k+1),因为如果到达dp[N][M][K-1],游戏结束,不可能由这个到达dp[N][M][K]。
#include <iostream>#include <cstdio>#include <cstring>using namespace std;double dp[60][60][2510];int main(){#ifdef xxz    freopen("in","r",stdin);#endif // xxz    int T;    cin>>T;    while(T--)    {        int n,m;        cin>>n>>m;        memset(dp,0,sizeof(dp));        dp[0][0][0] = 1.0;        for(int  i = 1; i <= n ; i++)            for(int j = 1; j <= m; j++)                for(int k = 1; k <= n*m; k++)                {                    double temp = n*m - k + 1;                    if(i == n && j == m)                    {                        dp[i][j][k] = dp[i - 1][j - 1][k - 1] *(1.0*(n-i+1)*(m - j+1) / temp)                                      + dp[i-1][j][k-1]*(1.0*(n - i+1)*j/temp)                                      +dp[i][j-1][k-1]*(1.0*i*(m - j+1 )/temp);                    }                    else                    {                        dp[i][j][k] = dp[i - 1][j - 1][k - 1] *(1.0*(n-i+1)*(m - j+1) / temp)                                      + dp[i-1][j][k-1]*(1.0*(n - i+1)*j/temp)                                      +dp[i][j-1][k-1]*(1.0*i*(m - j+1 )/temp)                        +dp[i][j][k-1]*(1.0*(i*j - k+1)/temp);                    }                }        double ans = 0;        for(int i = 1; i <= n*m; i++)        {            ans += dp[n][m][i] * i;        }        printf("%.12lf\n",ans);    }    return 0;}

Known Notation

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathematics and computer science. It is also known as postfix notation since every operator in an expression follows all of its operands. Bob is a student in Marjar University. He is learning RPN recent days.

To clarify the syntax of RPN for those who haven't learnt it before, we will offer some examples here. For instance, to add 3 and 4, one would write "3 4 +" rather than "3 + 4". If there are multiple operations, the operator is given immediately after its second operand. The arithmetic expression written "3 - 4 + 5" in conventional notation would be written "3 4 - 5 +" in RPN: 4 is first subtracted from 3, and then 5 added to it. Another infix expression "5 + ((1 + 2) × 4) - 3" can be written down like this in RPN: "5 1 2 + 4 × + 3 -". An advantage of RPN is that it obviates the need for parentheses that are required by infix.

In this problem, we will use the asterisk "*" as the only operator and digits from "1" to "9" (without "0") as components of operands.

You are given an expression in reverse Polish notation. Unfortunately, all space characters are missing. That means the expression are concatenated into several long numeric sequence which are separated by asterisks. So you cannot distinguish the numbers from the given string.

You task is to check whether the given string can represent a valid RPN expression. If the given string cannot represent any valid RPN, please find out the minimal number of operations to make it valid. There are two types of operation to adjust the given string:

  1. Insert. You can insert a non-zero digit or an asterisk anywhere. For example, if you insert a "1" at the beginning of "2*3*4", the string becomes "12*3*4".
  2. Swap. You can swap any two characters in the string. For example, if you swap the last two characters of "12*3*4", the string becomes "12*34*".

The strings "2*3*4" and "12*3*4" cannot represent any valid RPN, but the string "12*34*" can represent a valid RPN which is "1 2 * 34 *".

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

There is a non-empty string consists of asterisks and non-zero digits. The length of the string will not exceed 1000.

Output

For each test case, output the minimal number of operations to make the given string able to represent a valid RPN.

Sample Input

31*111*234***

Sample Output

102

zoj3829

这题我理解错了题意,想复杂了,正确解答是

1:当num小于等于star时候,必定要insert插入star-num+1个数字到最前面(因为一个×最少需要2个数字)

2:生下来就只需要交换了,就是如果前面数字不够,就把前面的×与后面的数字交换,使得数字尽量往前,×尽量往后,不断贪心

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int mian(){    #ifdef xxz    freopen("in","r",stdin);    #endif // xxz    int T;    cin>>T;    while(T--)    {        string  str;        cin>>str;        int len = str.length();        int  num = 0,star = 0;        for(int i = 0; i < len; i++)        {            if(str[i] == '*' ) star++;            else num++;        }        int ans = 0;        int left_num = 0;        if(num <= star)        {            left_num += star - num+1;            ans += left_num;        }        for(int i = 0,p = len-1; i < len; i++)        {            while(i < p && str[p] == '*') p--;            if(str[i] == '*')            {                left_num--;//这个的好处就是每次数字减去1,如果剩下数字个数大于1那么就可以消去一个*,比如11*表达式就合并成一个数了可以继续往下                if(left_num <= 0 )                {                    swap(str[i],str[p]);                    ans++;                    p--;                    left_num += 2;                }            }            else left_num++;        }        cout<<ans<<endl;    }    return 0;}


1 0
原创粉丝点击