POJ 3701--Football (probability dp)

Topic portal

Meaning of the title: give you an n, tell you there are 2 n 2^n2n football teams, and give you a2 n ∗ 2 n 2^n * 2^n2n2matrix of n ,iijj inrow iColumn j indicates theiii team winsjjThe probability of j teams, data guaranteep [i] [j] + p [j] [i] = 1 (i ≠ j), p [i] [j] = 0 (i = j) p[i][ j]+p[j][i]=1 (i≠j), p[i][j]=0(i=j)p[i][j]+p[j][i]=1(i=j)p[i][j]=0(i=j ) , and each match will be played in pairs in a relative order. The winner advances and the relative order remains the same, such as(1, 2, 3, 4, 5, 6, 7, 8) (1,2,3, 4,5,6,7,8)(1,2,3,4,5,6,7,8 ) After the first game, (1,4,6,7) is left, and the second time is1 11 vs 4 4 4 , 6 6 6 vs 7 7 7 . Ask you who has the highest probability of winning in the end?

Idea: We use f [i] [j] f[i][j]f [ i ] [ j ] meansjjj person iniiThe probability of winning in round i . Then,f [i] [j] = f [i − 1] [j] ∗ (∑ a [j] [k] ∗ f [i − 1] [k]) f[i][j]=f[ i-1][j]*(\sum a[j][k]*f[i-1][k])f[i][j]=f[i1][j](a[j][k]f[i1][k])。( k k k represents all teams that can compete with the j-th individual in the i-th game) You will find that the winner of the i-th game must be a length of2 i 2^i2One of the segments of i (we call it the effective segment), and these segments start from 1. Sokkk isjjThe first half or the second half of the effective segment where j is located (jjj is in the first halfkkk is in the second half).

Code:

#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ls p<<1
#define rs p<<1|1
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ll long long
#define int long long
#define lowbit(x) x&-x
#define pii pair<int,int>
#define ull unsigned long long
#define pdd pair<double,double>
#define sz(x) (int)(x).size()
#define all(x) (x).begin(),(x).end()
#pragma GCC optimize("-Ofast","-funroll-all-loops")
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
char *fs,*ft,buf[1<<20];
#define gc() (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<20,stdin),fs==ft))?0:*fs++;
inline int read()
{
    
    
    int x=0,f=1;
    char ch=gc();
    while(ch<'0'||ch>'9')
    {
    
    
        if(ch=='-')
            f=-1;
        ch=gc();
    }
    while(ch>='0'&&ch<='9')
    {
    
    
        x=x*10+ch-'0';
        ch=gc();
    }
    return x*f;
}
using namespace std;
const int N=655;
const int inf=0x3f3f3f3f;
const int mod=998244353;
const double eps=1e-6;
const double PI=acos(-1);

double a[222][222],f[222][222];

signed main()
{
    
    
    int n;
    while(cin>>n)
    {
    
    
        if(n==-1)
            break;
        n=1<<n;
        memset(f,0,sizeof f);
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
                cin>>a[i][j],f[0][j]=1.0;
        double ma=-1;
        int pos=-1;
        for(int i=1; (1<<i)<=n; i++)
        {
    
    
            for(int j=1; j<=n; j++)
            {
    
    
                int cnt=(j-1)/(1<<i);
                int l=cnt*(1<<i)+1,r=(cnt+1)*(1<<i);
                if(j>(l+r-1)/2)
                    r=(l+r-1)/2;
                else
                    l=(l+r-1)/2+1;
                double sum=0;
                for(int k=l; k<=r; k++)
                {
    
    
                    sum+=a[j][k]*f[i-1][k];
                    //cout<<i<<' '<<j<<' '<<k<<' '<<a[j][k]<<' '<<f[i-1][k]<<' '<<sum<<endl;
                }
                sum*=f[i-1][j];
                f[i][j]=sum;
                if(1<<i==n)
                {
    
    
                    if(f[i][j]>ma)
                        ma=f[i][j],pos=j;
                }
            }
        }
        cout<<pos<<endl;
    }

    return 0;
}

Guess you like

Origin blog.csdn.net/Joker_He/article/details/110496179