結果

問題 No.309 シャイな人たち (1)
ユーザー t98slidert98slider
提出日時 2023-05-16 03:28:38
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,783 ms / 4,000 ms
コード長 2,417 bytes
コンパイル時間 1,871 ms
コンパイル使用メモリ 178,096 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-21 00:03:45
合計ジャッジ時間 20,391 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,101 ms
4,376 KB
testcase_01 AC 2,273 ms
4,376 KB
testcase_02 AC 116 ms
4,376 KB
testcase_03 AC 1,653 ms
4,376 KB
testcase_04 AC 22 ms
4,380 KB
testcase_05 AC 295 ms
4,380 KB
testcase_06 AC 113 ms
4,380 KB
testcase_07 AC 169 ms
4,380 KB
testcase_08 AC 1,721 ms
4,380 KB
testcase_09 AC 2,169 ms
4,376 KB
testcase_10 AC 2,783 ms
4,380 KB
testcase_11 AC 2,344 ms
4,380 KB
testcase_12 AC 1,713 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 23 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int h, w;
    cin >> h >> w;
    vector<vector<double>> P(h, vector<double>(w));
    vector<vector<int>> S(h, vector<int>(w));
    for(int y = 0; y < h; y++){
        for(int x = 0; x < w; x++){
            cin >> P[y][x];
            P[y][x] /= 100;
        }
    }
    for(int y = 0; y < h; y++){
        for(int x = 0; x < w; x++){
            cin >> S[y][x];
        }
    }
    vector<double> pre(1 << w), dp(1 << w);
    pre[0] = 1;
    queue<int> que;
    double ans = 0;
    for(int y = 0; y < h; y++){
        //S1:現在の列が知っているか否か
        //S0:前の列が手を上げたか否か
        auto f = [&](int S0, int S1, int visited, int x){
            int tmp = (S0 >> x & 1) + (visited >> x + 1 & 1) + (x ? visited >> x - 1 & 1 : 0);
            if(S1 >> x & 1) tmp += 4 - S[y][x];
            return (tmp >= 4);
        };
        for(int S1 = 0; S1 < (1 << w); S1++){
            double prob = 1;
            for(int x = 0; x < w; x++){
                if(S1 >> x & 1) prob *= P[y][x];
                else prob *= 1 - P[y][x];
            }
            for(int S0 = 0; S0 < (1 << w); S0++){
                if(pre[S0] == 0) continue;
                int visited = 0;
                for(int x = 0; x < w; x++){
                    if((S0 >> x & 1) + (S1 >> x & 1 ? 4 - S[y][x] : 0) >= 4){
                        visited |= 1 << x;
                        que.emplace(x);
                    }
                }
                while(!que.empty()){
                    int x = que.front();
                    que.pop();
                    if(x + 1 < w && (~visited >> x + 1 & 1) && f(S0, S1, visited, x + 1)){
                        visited |= 1 << x + 1;
                        que.emplace(x + 1);
                    }
                    if(x >= 1 && (~visited >> x - 1 & 1) &&  f(S0, S1, visited, x - 1)){
                        visited |= 1 << x - 1;
                        que.emplace(x - 1);
                    }
                }
                dp[visited] += prob * pre[S0];
            }
        }
        for(int bit = 0; bit < (1 << w); bit++){
            ans += __builtin_popcount(bit) * dp[bit];
            pre[bit] = dp[bit];
            dp[bit] = 0;
        }
    }
    cout << fixed << setprecision(15) << ans << '\n';
}
0