HDU 4870 Rating 2014 Multi-University Training Contest 1 J题 概率DP+高斯消元
2015-01-29 16:39
495 查看
题目大意:
就是现在有一个比赛,初始Rating是0, 每次参加比赛有P的概率Rating变为 Min(Rating + 50, 1000) 有(1 - P)的概率变为 Max(0, Rating -100)
为现在一个人用两个账号,每次选择用其中Rating较低的号参加比赛, 使得其中1个号的Rating达到1000所需要比赛的期望场数
大致思路:
很明显的概率DP, 由于变量数不多可以用高斯消元
不过这题的误差问题表示有些不能理解, Gauss消元误差较大
代码如下:
Result : Accepted Memory : 1812 KB Time : 1794 ms
/*
* Author: Gatevin
* Created Time: 2015/1/29 14:14:54
* File Name: Iris_Fleyja.cpp
*/
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;
double a[300][300];
double x[300];
int hash[22][22];
int var, equ;
/*
* hash[i][j]为E[i][j]对应的变量编号
* 最终解的结果存在x[hash[i][j]]中
*/
/*
* 如果用E[i][j] (i >= j)表示当前分数分别为i和j时到达目标状态的概率
* E[i][j] = P*(1 + E[i][j + 1]) + (1 - P)*(1 + E[i][Max(j - 2, 0)]) (i >= j)
* 很明显E[i][j] = E[j][i], 所以高斯消元做的话只需要一半的变量就够了
* 另外感觉这题误差还是有些....不好说..
*/
int Gauss()//正常的高斯消元的想法
{
for(int k = 0, col = 0; k < equ && col < var; k++, col++)
{
int max_r = k;
for(int i = k + 1; i < equ; i++)
if(fabs(a[i][col]) > fabs(a[max_r][col]))
max_r = i;
//if(fabs(a[max_r][col]) < eps) return 0;
/*
* 对于eps 为1e-8精度不够,方程组会被判无解
* 注释掉之后莫名过了...
*/
if(k != max_r)
{
for(int j = col; j < var; j++)
swap(a[k][j], a[max_r][j]);
swap(x[k], x[max_r]);
}
x[k] /= a[k][col];
for(int j = col + 1; j < var; j++) a[k][j] /= a[k][col];
a[k][col] = 1;
for(int i = 0; i < equ; i++)
if(i != k)
{
x[i] -= x[k]*a[i][col];
for(int j = col + 1; j < var; j++) a[i][j] -= a[k][j]*a[i][col];
a[i][col] = 0;
}
}
return 1;
}
void buildEquation(double P)//建立方程组
{
memset(a, 0, sizeof(a));
memset(x, 0, sizeof(x));
var = equ = 0;
memset(hash, -1, sizeof(hash));
for(int i = 0; i <= 20; i++)
{
hash[20][i] = var++;
x[equ] = 0;
a[equ++][hash[20][i]] += 1;
}
for(int i = 0; i < 20; i++)
for(int j = 0; j <= i; j++)
{
if(hash[i][j] == -1)
hash[i][j] = var++;
if(j + 1 <= i && hash[i][j + 1] == -1)
hash[i][j + 1] = var++;
if(j + 1 > i && hash[j + 1][i] == -1)
hash[j + 1][i] = var++;
if(j < 2)
{
if(hash[i][0] == -1)
hash[i][0] = var++;
a[equ][hash[i][0]] -= (1 - P);
a[equ][hash[i][j]] += 1;
if(j + 1 <= i)
a[equ][hash[i][j + 1]] -= P;
else
a[equ][hash[j + 1][i]] -= P;
x[equ++] = 1;
}
else
{
if(hash[i][j - 2] == -1)
hash[i][j - 2] = var++;
a[equ][hash[i][j - 2]] -= (1 - P);
a[equ][hash[i][j]] += 1;
if(j + 1 <= i)
a[equ][hash[i][j + 1]] -= P;
else
a[equ][hash[j + 1][i]] -= P;
x[equ++] = 1;
}
}
return;
}
int main()
{
double p;
while(scanf("%lf", &p) != EOF)
{
buildEquation(p);
Gauss();
printf("%.6f\n", x[hash[0][0]]);
}
return 0;
}
就是现在有一个比赛,初始Rating是0, 每次参加比赛有P的概率Rating变为 Min(Rating + 50, 1000) 有(1 - P)的概率变为 Max(0, Rating -100)
为现在一个人用两个账号,每次选择用其中Rating较低的号参加比赛, 使得其中1个号的Rating达到1000所需要比赛的期望场数
大致思路:
很明显的概率DP, 由于变量数不多可以用高斯消元
不过这题的误差问题表示有些不能理解, Gauss消元误差较大
代码如下:
Result : Accepted Memory : 1812 KB Time : 1794 ms
/*
* Author: Gatevin
* Created Time: 2015/1/29 14:14:54
* File Name: Iris_Fleyja.cpp
*/
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;
double a[300][300];
double x[300];
int hash[22][22];
int var, equ;
/*
* hash[i][j]为E[i][j]对应的变量编号
* 最终解的结果存在x[hash[i][j]]中
*/
/*
* 如果用E[i][j] (i >= j)表示当前分数分别为i和j时到达目标状态的概率
* E[i][j] = P*(1 + E[i][j + 1]) + (1 - P)*(1 + E[i][Max(j - 2, 0)]) (i >= j)
* 很明显E[i][j] = E[j][i], 所以高斯消元做的话只需要一半的变量就够了
* 另外感觉这题误差还是有些....不好说..
*/
int Gauss()//正常的高斯消元的想法
{
for(int k = 0, col = 0; k < equ && col < var; k++, col++)
{
int max_r = k;
for(int i = k + 1; i < equ; i++)
if(fabs(a[i][col]) > fabs(a[max_r][col]))
max_r = i;
//if(fabs(a[max_r][col]) < eps) return 0;
/*
* 对于eps 为1e-8精度不够,方程组会被判无解
* 注释掉之后莫名过了...
*/
if(k != max_r)
{
for(int j = col; j < var; j++)
swap(a[k][j], a[max_r][j]);
swap(x[k], x[max_r]);
}
x[k] /= a[k][col];
for(int j = col + 1; j < var; j++) a[k][j] /= a[k][col];
a[k][col] = 1;
for(int i = 0; i < equ; i++)
if(i != k)
{
x[i] -= x[k]*a[i][col];
for(int j = col + 1; j < var; j++) a[i][j] -= a[k][j]*a[i][col];
a[i][col] = 0;
}
}
return 1;
}
void buildEquation(double P)//建立方程组
{
memset(a, 0, sizeof(a));
memset(x, 0, sizeof(x));
var = equ = 0;
memset(hash, -1, sizeof(hash));
for(int i = 0; i <= 20; i++)
{
hash[20][i] = var++;
x[equ] = 0;
a[equ++][hash[20][i]] += 1;
}
for(int i = 0; i < 20; i++)
for(int j = 0; j <= i; j++)
{
if(hash[i][j] == -1)
hash[i][j] = var++;
if(j + 1 <= i && hash[i][j + 1] == -1)
hash[i][j + 1] = var++;
if(j + 1 > i && hash[j + 1][i] == -1)
hash[j + 1][i] = var++;
if(j < 2)
{
if(hash[i][0] == -1)
hash[i][0] = var++;
a[equ][hash[i][0]] -= (1 - P);
a[equ][hash[i][j]] += 1;
if(j + 1 <= i)
a[equ][hash[i][j + 1]] -= P;
else
a[equ][hash[j + 1][i]] -= P;
x[equ++] = 1;
}
else
{
if(hash[i][j - 2] == -1)
hash[i][j - 2] = var++;
a[equ][hash[i][j - 2]] -= (1 - P);
a[equ][hash[i][j]] += 1;
if(j + 1 <= i)
a[equ][hash[i][j + 1]] -= P;
else
a[equ][hash[j + 1][i]] -= P;
x[equ++] = 1;
}
}
return;
}
int main()
{
double p;
while(scanf("%lf", &p) != EOF)
{
buildEquation(p);
Gauss();
printf("%.6f\n", x[hash[0][0]]);
}
return 0;
}
相关文章推荐
- HDU 4870 Rating (2014 Multi-University Training Contest 1)
- 2014(高斯消元 DP 数论)Multi-University Training Contest 1 题解
- hdu 4870 Rating 2014 Multi-University Training Contest 1
- HDU 4870 Rating 概率DP(高斯消元)
- HDU 4900 NO ACM NO LIFE(概率+枚举+搜索)(2014 Multi-University Training Contest 4)
- HDU 5755 Gambler Bo(高斯消元)2016 Multi-University Training Contest 3
- HDU 4899 Hero meet devil(状压DP)(2014 Multi-University Training Contest 4)
- HDU 4870 Rating(概率dp+高斯消元)
- hdu 4870 Rating (高斯消元解概率dp)
- HDU 5755 Gambler Bo(高斯消元裸题)——2016 Multi-University Training Contest 3
- hdu 4893 Wow! Such Sequence!(线段树和树状数组实现)--2014 Multi-University Training Contest 3
- 【HDU 2014 Multi-University Training Contest 1 1002】/【HDU 4862】Jump
- hdu--4870 Rating(高斯消元,概率)
- HDU 4864 (2014 Multi-University Training Contest 1 )
- 2014多校第一场J题 || HDU 4870 Rating(DP || 高斯消元)
- hdu 4891The Great Pan -----------2014 Multi-University Training Contest 3
- HDU 4870 Rating (概率+高斯消元)
- hdu4870 Rating 2014 Multi-University Training Contest 1
- 【HDU 2014 Multi-University Training Contest 1 1002】/【HDU 4862】Jump
- hdu 4876 ZCC loves cards 2014 Multi-University Training Contest 2