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;
}

 

转载于:https://www.cnblogs.com/Commence/p/3974714.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用c++解决You surely know that there are N different currencies you can deal with in our city. Let us assign unique integer number from 1 to N to each currency. Then each exchange point can be described with 6 numbers: integer A and B - numbers of currencies it exchanges, and real RAB, CAB, RBA and CBA - exchange rates and commissions when exchanging A to B and B to A respectively. Nick has some money in currency S and wonders if he can somehow, after some exchange operations, increase his capital. Of course, he wants to have his money in currency S in the end. Help him to answer this difficult question. Nick must always have non-negative sum of money while making his operations. Input The first line contains four numbers: N - the number of currencies, M - the number of exchange points, S - the number of currency Nick has and V - the quantity of currency units he has. The following M lines contain 6 numbers each - the description of the corresponding exchange point - in specified above order. Numbers are separated by one or more spaces. 1 ≤ S ≤ N ≤ 100, 1 ≤ M ≤ 100, V is real number, 0 ≤ V ≤ 103. For each point exchange rates and commissions are real, given with at most two digits after the decimal point, 10-2 ≤ rate ≤ 102, 0 ≤ commission ≤ 102. Let us call some sequence of the exchange operations simple if no exchange point is used more than once in this sequence. You may assume that ratio of the numeric values of the sums at the end and at the beginning of any simple sequence of the exchange operations will be less than 104. Output If Nick can increase his wealth, output YES, in other case output NO.
06-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值