标签:style blog color io os ar for strong sp
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:
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 *".
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.
For each test case, output the minimal number of operations to make the given string able to represent a valid RPN.
3 1*1 11*234** *
1 0 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--;//比如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; }
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.
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 <= N, M <= 50).
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.
2 1 3 2 2
3.000000000000 2.666666666667
渣渣的第一个概率dp。。。。
#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; }
标签:style blog color io os ar for strong sp
原文地址:http://blog.csdn.net/u013445530/article/details/40084919