UVA 10471 Gift Exchanging

题意:就5种盒子,给出每个盒子个数,盒子总数,每个人选择这个盒子的概率。求这个人选择哪个盒子取得第一个朋友的概率最大,最大多少

dp[N][sta]表示当前第N个人面临状态sta(选择盒子的状态可以用13进制数表示)时的概率,

那么转移就是dp[N][sta]=sum(dp[N-1][sta-1]*G[n][k]) (表示第N个人选择第k个盒子)

那么答案应该是max(P(第1个人选择i号盒子)/总状态概率)(i<=5)

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == 0 ? b : gcd(b, a % b);}
#define MAXN 350000
#define MAXD 15
#define MAXB 10
double p[MAXD][MAXB],dp[MAXD][MAXN];
bool vis[MAXD][MAXN];
int N,res[MAXB],gift[MAXB];
double calcu(int cur,int st)
{
    if (vis[cur][st]) return dp[cur][st];
    vis[cur][st]=true;
    double ans=0;
    int sta=st;
    for (int i=5;i>0;i--)  {res[i]=sta%13;sta/=13;}
    for (int i=1;i<=5;i++)
      if (res[i])
    {
        res[i]--;
        sta=0;
        for (int j=1;j<=5;j++) sta=sta*13+res[j];
        ans+=calcu(cur+1,sta)*p[cur][i];
        res[i]++;
    }
    return dp[cur][st]=ans;
}
int main()
{
    //freopen("sample.txt","r",stdin);
    int T;
    scanf("%d",&T);
    while (T--)
    {
        scanf("%d",&N);
        int sta=0;
        for (int i=1;i<=5;i++) { scanf("%d",&gift[i]);sta=sta*13+gift[i];}
        for (int i=1;i<=N;i++) for (int j=1;j<=5;j++) scanf("%lf",&p[i][j]);
        memset(vis,false,sizeof(vis));
        vis[N+1][0]=true;dp[N+1][0]=1;
        double tmp=calcu(1,sta);
        double ans=-1.0,sym;int ide;
        for (int i=1;i<=5;i++)
            if (gift[i])
        {
            gift[i]--;
            sta=0;
            for (int j=1;j<=5;j++) sta=sta*13+gift[j];
            gift[i]++;
            sym=dp[2][sta]*p[1][i]/gift[i];
            if (sym/tmp>ans)
            {
                ans=sym/tmp;
                ide=i;
            }
        }
        printf("%d %.3lf\n",ide,ans);
    }
    return 0;
}
时间: 2024-10-14 00:35:53

UVA 10471 Gift Exchanging的相关文章

UVa 10120 - Gift?!

题目大意 美丽的村庄里有一条河,N个石头被放置在一条直线上,从左岸到右岸编号依次为1,2,...N.两个相邻的石头之间恰好是一米,左岸到第一个石头的距离也是一米,第N个石头到右岸同样是一米.礼物被放置在第M个石头上,Frank从左岸开始跳跃,对于第i步,必须跳2*i-1米,每次可以向左方向跳,也可以向右方向跳,跳到河岸跳跃就结束了,问Frank能否拿到礼物. 分析 搜索题,N>=49直接输答案,N<49时搜索,原因点击这里 http://www.algorithmist.com/index.p

UVA - 10120 Gift?! 暴力+规律

题目大意:有n块石头,礼物在第m块石头上,相邻石头的距离为1,规定小青蛙第一步跳到第一块石头上,接下来的跳跃要符合该规则,假设这是第n次跳跃,那么小青蛙跳跃的距离为(2 * n - 1),且每次跳跃都必须跳到石头上 解题思路:石头数量如果超过49的话,小青蛙就可以跳到任意一块石头上,其他的情况只需暴力dfs就可以解决了 #include<cstdio> #include<cstring> int pos, len; bool dfs(int cur, int time) { if(

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

《算法竞赛入门经典——训练指南》第二章题库

UVa特别题库 UVa网站专门为本书设立的分类题库配合,方便读者提交: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=442 注意,下面注有"extra"的习题并没有在书中出现,但在上面的特别题库中有,属于附加习题. 基础练习 (Basic Problems) UVa11388 GCD LCM UVa11889 Benefit UVa10943 How do y

UVa 10889 - The Lost Gift

题目:给你R个红球和B个黑球,从这些球中取出相同颜色的概率是50%: 然后丢了一些黑球,剩下的黑球不少于原来的70%: 现在给你红球和剩下的黑球个数,求可能丢了几个黑球. 分析:数学题. 首先,根据组合数学列出等式2*[C(n,2)+C(m,2)] = C(m+n,2): 解得 m = 0.5*(2*n+1±sqrt(8*n+1)): 然后,进行判断: 1.m不是整数,则红球非法: 2.m是整数,丢的球不在0-30%之间,黑球非法: 3.都合法的情况下,输出丢失的黑球个数即可. 方法2:通项公式

UVA - 11987 - Almost Union-Find (又是并查集~)

UVA - 11987 Almost Union-Find Time Limit: 1000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Problem A Almost Union-Find I hope you know the beautiful Union-Find structure. In this problem, you're to implement som

uva 11995 - I Can Guess the Data Structure!

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=3146&mosmsg=Submission+received+with+ID+14262472 I Can Guess the Data Structure! There is a bag-like data structure, supporti

(DS 《算法入门经典》)UVA 11991 Easy Problem from Rujia Liu?(求第k个v出现的索引)

题目大意: 求第k个v出现的索引 解题思路: 如果能构造出一个数据结构,使得data[v][k]就是第k个v出现的索引值即可求解.data[v]表示数v出现的索引数组, data[v][k]表示第k个v出现的索引. Problem E Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for example, regional contests like Xi'an 200

[UVA] 11991 - Easy Problem from Rujia Liu? [STL应用]

11991 - Easy Problem from Rujia Liu? Time limit: 1.000 seconds Problem E Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for example, regional contests like Xi'an 2006, Beijing 2007 and Wuhan 2009, or UVa OJ con