HDU 4870 Rating Gaussian elimination method, hdu4870
Link: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 4870
Assume that you use two accounts to participate in a competition. In the initial state, both accounts have zero points. Each game uses a low-score account to compete. A probability of winning a game is P, the score of the corresponding account increases by 50 points. Otherwise, the score of the corresponding account decreases by 100 points. Ask the expected number of times of participation in the competition when the score of one account reaches 1000 points.
Train of Thought: During the competition, I thought it was a question to push the formula, and there was no result after I pushed it for half a day. After the game, I thought about it. After reading the question report, I learned that the corresponding status of each score was pushed by more than two States, and that Gaussian elimination element can be used to solve the problem instead of pushing forward from the back. Status 0 ~ Status 209 indicates that the score of a person is (0, 0), (50, 0)... (950,950), and the expected score of an account is 1000. E (X, Y) = p (E (x1, y1) + 1) + (1-p) (E (x2, y2) + 1), (x1, y1) it indicates the scores of two accounts after the competition, and (x2, y2) indicates the scores of the two accounts after the competition fails.
Status does not need to be included in the status (1000 ,?) This is because the status does not appear except (1000,950. The (1000,950) status is only related to the status (950,950), and (950,950) will not be obtained from (1000,950). The status of the 210 states is not related to (1000 ,?) Status.
Code:
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <map>#include <cstdlib>#include <queue>#include <stack>#include <vector>#include <ctype.h>#include <algorithm>#include <string>#include <set>#define PI acos(-1.0)#define maxn 210#define INF 0x7fffffff#define eps 1e-8#define MOD 1000000009typedef long long LL;typedef unsigned long long ULL;using namespace std;double a[220][220],b[220];int all[25];double gauss_elimination(int n){ int i,j,k,row; double maxp,t; for(k=0; k<n; k++) { for(maxp=0,i=k; i<n; i++) if (fabs(a[i][k])>eps) { maxp=a[row=i][k]; break; } if(fabs(maxp)<eps) return 0; if(row!=k) { for(j=k; j<n; j++) swap(a[k][j],a[row][j]); swap(b[k],b[row]); } for (int j = 0; j < n; j++) { if (k == j) continue; if (fabs(a[j][k]) > eps) { double x = a[j][k] / a[k][k]; for (int i = k; i < n; i++) { a[j][i] -= a[k][i] * x; } b[j] -=b[k]*x; } } } return 1;}int init(){ all[0]=0; for(int i=1; i<=21; i++) { all[i]=all[i-1]+i; } return 0;}int main(){ double p; init(); while(~scanf("%lf",&p)) { memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); for(int i=0; i<20; i++) { for(int j=0; j<=i; j++) { a[all[i]+j][all[i]+j]+=1; b[all[i]+j]+=1; a[all[i]+j][all[i]+max(j-2,0)]+=(p-1); if(j+1<=i) a[all[i]+j][all[i]+j+1]+=(-p); else { if(i==19&&j==19) continue; else a[all[i]+j][all[j+1]+i]+=(-p); } } } gauss_elimination(210); printf("%.6lf\n",b[0]/a[0][0]); } return 0;}
What algorithms are good for algorithm design competitions?
It should be ACM.
It is 8-10 algorithm questions for you. The more questions you have made in five hours, the higher the ranking. If the number of questions is the same, the more time you use.
The time calculation method is as follows:
For example, if you use question A for 20 minutes, then question B uses Question A for 30 minutes (this is the start of the game for 50 minutes) and question C for 30 minutes, so your time (penalty) is
20 + 50 + 80 = 150 minutes
Common algorithms used in competitions include:
1. Dynamic Planning
2. Search
3. Greedy
4. Graph Theory
5. Combined mathematics
6. Computational ry
7. Number Theory
And so on
Recommended
Acm.pku.edu.cn
Acm.zju.edu.cn
Acm.hdu.edu.cn
Acm.timus.ru
These OJ exercises
Better question classification (on POJ)
1. This is my favorite
Initial stage:
I. Basic Algorithms:
(1) enumeration. (poj1753, poj2965) (2008-10-27Done bitwise operation + wide search)
(2) greedy (poj1328, poj2109, poj2586)
(3) recursion and divide and conquer.
(4) recurrence.
(5) constructor. (poj3295)
(6) simulation method. (poj1068, poj2632, poj1573, poj2993, poj2996)
Ii. Graph Algorithm:
(1) depth first traversal and breadth first traversal.
(2) shortest path algorithm (dijkstra, bellman-ford, floyd, heap + dijkstra) (2008-08-29Done)
(Poj1860, poj3259, poj1062, poj2253, poj1125, poj2240)
(3) Minimum Spanning Tree Algorithm (prim, kruskal)
(Poj1789, poj2485, poj1258, poj3026)
(4) Topology Sorting (poj1094) (2008-09-01Done)
(5) maximum matching of bipartite graphs (Hungary algorithm) (poj3041, poj3020)
(6) augmented Path Algorithm for the maximum stream (KM algorithm). (poj1459, poj3436)
Iii. data structure.
(1) string (poj1035, poj3080, poj1936)
(2) sorting (fast sorting, Merge Sorting (related to the number of reverse orders), heap sorting) (poj2388, poj2299)
(3) Simple and query set applications.
(4) efficient search methods such as Hash table and binary search (number Hash, string Hash)
(Poj3349, poj3274, POJ2151, poj1840, poj2002, poj2503)
(5) Harman tree (poj3253) (2008-09-02Done)
(6) Heap
(7) trie tree (static and dynamic) (poj2513) (done, query set, Euler)
4. Simple search
(1) Deep Priority Search (poj2488, poj3083, poj3009, poj1321, poj2.pdf)
(2) breadth-first search (poj3278, poj1426, ...... the remaining full text>