結果

問題 No.174 カードゲーム(Hard)
ユーザー LayCurseLayCurse
提出日時 2015-01-23 11:39:01
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 183 ms / 2,000 ms
コード長 4,948 bytes
コンパイル時間 1,460 ms
コンパイル使用メモリ 150,420 KB
実行使用メモリ 16,516 KB
最終ジャッジ日時 2023-09-21 02:23:51
合計ジャッジ時間 3,779 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,576 KB
testcase_01 AC 3 ms
5,740 KB
testcase_02 AC 183 ms
16,244 KB
testcase_03 AC 183 ms
16,244 KB
testcase_04 AC 182 ms
16,356 KB
testcase_05 AC 183 ms
16,408 KB
testcase_06 AC 183 ms
16,316 KB
testcase_07 AC 183 ms
16,516 KB
testcase_08 AC 182 ms
16,376 KB
testcase_09 AC 183 ms
16,380 KB
testcase_10 AC 2 ms
5,632 KB
testcase_11 AC 2 ms
5,752 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘double brute_dfs(int, int*, int*, double, int, int)’:
main.cpp:92:35: warning: ‘pb[1]’ may be used uninitialized in this function [-Wmaybe-uninitialized]
     if(j==0) pp *= pb[0]; else pp *= pb[1];
                                ~~~^~~~~~~~
main.cpp:91:35: warning: ‘pa[1]’ may be used uninitialized in this function [-Wmaybe-uninitialized]
     if(i==0) pp *= pa[0]; else pp *= pa[1];
                                ~~~^~~~~~~~

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)

#define mygc(c) (c)=getchar_unlocked()
#define mypc(c) putchar_unlocked(c)

#define ll long long
#define ull unsigned ll

void reader(int *x){int k,m=0;*x=0;for(;;){mygc(k);if(k=='-'){m=1;break;}if('0'<=k&&k<='9'){*x=k-'0';break;}}for(;;){mygc(k);if(k<'0'||k>'9')break;*x=(*x)*10+k-'0';}if(m)(*x)=-(*x);}
void reader(ll *x){int k,m=0;*x=0;for(;;){mygc(k);if(k=='-'){m=1;break;}if('0'<=k&&k<='9'){*x=k-'0';break;}}for(;;){mygc(k);if(k<'0'||k>'9')break;*x=(*x)*10+k-'0';}if(m)(*x)=-(*x);}
void reader(double *x){scanf("%lf",x);}
int reader(char c[]){int i,s=0;for(;;){mygc(i);if(i!=' '&&i!='\n'&&i!='\r'&&i!='\t'&&i!=EOF) break;}c[s++]=i;for(;;){mygc(i);if(i==' '||i=='\n'||i=='\r'||i=='\t'||i==EOF) break;c[s++]=i;}c[s]='\0';return s;}
template <class T, class S> void reader(T *x, S *y){reader(x);reader(y);}
template <class T, class S, class U> void reader(T *x, S *y, U *z){reader(x);reader(y);reader(z);}
template <class T, class S, class U, class V> void reader(T *x, S *y, U *z, V *w){reader(x);reader(y);reader(z);reader(w);}

void writer(int x, char c){int s=0,m=0;char f[10];if(x<0)m=1,x=-x;while(x)f[s++]=x%10,x/=10;if(!s)f[s++]=0;if(m)mypc('-');while(s--)mypc(f[s]+'0');mypc(c);}
void writer(ll x, char c){int s=0,m=0;char f[20];if(x<0)m=1,x=-x;while(x)f[s++]=x%10,x/=10;if(!s)f[s++]=0;if(m)mypc('-');while(s--)mypc(f[s]+'0');mypc(c);}
void writer(double x, char c){printf("%.15f",x);mypc(c);}
void writer(const char c[]){int i;for(i=0;c[i]!='\0';i++)mypc(c[i]);}
void writer(const char x[], char c){int i;for(i=0;x[i]!='\0';i++)mypc(x[i]);mypc(c);}
template<class T> void writerLn(T x){writer(x,'\n');}
template<class T, class S> void writerLn(T x, S y){writer(x,' ');writer(y,'\n');}
template<class T, class S, class U> void writerLn(T x, S y, U z){writer(x,' ');writer(y,' ');writer(z,'\n');}
template<class T> void writerArr(T x[], int n){int i;if(!n){mypc('\n');return;}rep(i,n-1)writer(x[i],' ');writer(x[n-1],'\n');}

#define RAND (rand()/(RAND_MAX+1.0))

int N;
double pA, pB;
int A[30], B[30];

double monte(int tm){
  int i, j, k, loop;
  int a[30], b[30], n;
  double all = 0, resA = 0, resB = 0;

  rep(loop, tm){
    n = N;
    rep(i,n) a[i] = A[i], b[i] = B[i];
    all++;

    while(n){
      if(n==1){
        i = j = 0;
      } else {
        if(RAND < pA) i = 0; else i = RAND * (n-1) + 1;
        if(RAND < pB) j = 0; else j = RAND * (n-1) + 1;
      }
      if(a[i] > b[j]) resA += a[i]+b[j];
      if(a[i] < b[j]) resB += a[i]+b[j];

      n--;
      REP(k,i,n) a[k] = a[k+1];
      REP(k,j,n) b[k] = b[k+1];
    }
  }

  return resA / all;
}



double brute_dfs(int n, int a[], int b[], double p, int sA, int sB){
  int i, j, k;
  int sa[30], sb[30];
  double pa[2], pb[2], res = 0;
  double pp;

  if(n==0){
    return p * sA;
  }

  if(n==1) pa[0] = pb[0] = 1;
  if(n>=2){
    pa[0] = pA; pb[0] = pB;
    pa[1] = (1-pA) / (n-1);
    pb[1] = (1-pB) / (n-1);
  }

  rep(i,n) rep(j,n){
    rep(k,n) sa[k] = a[k], sb[k] = b[k];
    REP(k,i,n-1) sa[k] = sa[k+1];
    REP(k,j,n-1) sb[k] = sb[k+1];

    pp = p;
    if(i==0) pp *= pa[0]; else pp *= pa[1];
    if(j==0) pp *= pb[0]; else pp *= pb[1];
    res += brute_dfs(n-1, sa, sb, pp, sA+((a[i]>b[j])?a[i]+b[j]:0), sB+((a[i]<b[j])?a[i]+b[j]:0));
  }
  return res;
}

double brute(void){
  int i, a[30], b[30];
  double res;
  rep(i,N) a[i] = A[i], b[i] = B[i];
  res = brute_dfs(N, a, b, 1, 0, 0);
  return res;
}


int bc[1111111];
double dp[1111111];
double dpA[30][30], dpB[30][30];

double solve(void){
  int i, j, k, f;
  double p[2];
  double res = 0, sum;

  bc[0] = 0;
  REP(i,1,1<<N) bc[i] = bc[i&(i-1)]+1;

  rep(i,N) rep(j,N) dpA[i][j] = dpB[i][j] = 0;

  rep(i,1<<N) dp[i] = 0;
  dp[0] = 1;
  rep(i,1<<N){
    k = N - bc[i];
    if(k==0) continue;
    if(k==1) p[0] = 1, p[1] = 0;
    if(k>=2){
      p[0] = pA;
      p[1] = (1-pA) / (k-1);
    }
    f = 0;
    rep(j,N) if(!(i&1<<j)){
      dp[i|(1<<j)] += dp[i] * p[f];
      dpA[bc[i]][j] += dp[i] * p[f];
      f = 1;
    }
  }

  rep(i,1<<N) dp[i] = 0;
  dp[0] = 1;
  rep(i,1<<N){
    k = N - bc[i];
    if(k==0) continue;
    if(k==1) p[0] = 1, p[1] = 0;
    if(k>=2){
      p[0] = pB;
      p[1] = (1-pB) / (k-1);
    }
    f = 0;
    rep(j,N) if(!(i&1<<j)){
      dp[i|(1<<j)] += dp[i] * p[f];
      dpB[bc[i]][j] += dp[i] * p[f];
      f = 1;
    }
  }

  rep(i,N) rep(j,N) if(A[i] > B[j]){
    rep(k,N) res += dpA[k][i] * dpB[k][j] * (A[i] + B[j]);
  }
  return res;
}

int main(){
  int i, j, k;
  double res;

  reader(&N, &pA, &pB);
  rep(i,N) reader(A+i);
  rep(i,N) reader(B+i);
  sort(A,A+N);
  sort(B,B+N);
  REP(i,1,N) assert(A[i-1] != A[i]);
  REP(i,1,N) assert(A[i-1] != A[i]);
  rep(i,N) rep(j,N) assert(A[i] != B[j]);

  srand(time(NULL));
//  res = monte(10000000);
//  writerLn(res);
//  res = brute();
//  writerLn(res);
  res = solve();
  writerLn(res);

  return 0;
}
0