码迷,mamicode.com
首页 > 其他好文 > 详细

2014ACM/ICPC亚洲区域赛牡丹江站D和K题

时间:2014-10-15 00:11:49      阅读:297      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   for   strong   sp   

Known Notation

Time Limit: 2 Seconds      Memory Limit: 131072 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

3
1*1
11*234**
*

Sample Output

1
0
2

Author: CHEN, Cong
#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--;//比如11×,--之后就可以相当于运算了,1*1=1,现在只有这一关运算了
                if(left_num <= 0 )
                {
                    swap(str[i],str[p]);
                    ans++;
                    p--;
                    left_num += 2;
                }
            }
            else left_num++;
        }
        cout<<ans<<endl;
    }
    return 0;
}

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

2
1 3
2 2

Sample Output

3.000000000000
2.666666666667
渣渣的第一个概率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;
}



2014ACM/ICPC亚洲区域赛牡丹江站D和K题

标签:style   blog   color   io   os   ar   for   strong   sp   

原文地址:http://blog.csdn.net/u013445530/article/details/40084919

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!