結果

問題 No.1196 A lazy student
ユーザー square1001square1001
提出日時 2020-08-22 15:48:00
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 164 ms / 1,000 ms
コード長 2,080 bytes
コンパイル時間 1,316 ms
コンパイル使用メモリ 96,208 KB
実行使用メモリ 12,828 KB
最終ジャッジ日時 2024-04-23 10:04:44
合計ジャッジ時間 2,785 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 3 ms
6,940 KB
testcase_05 AC 3 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 3 ms
6,940 KB
testcase_08 AC 59 ms
7,144 KB
testcase_09 AC 158 ms
12,668 KB
testcase_10 AC 164 ms
12,828 KB
testcase_11 AC 149 ms
12,576 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 60 ms
6,940 KB
testcase_16 AC 118 ms
12,328 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <set>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);
	int N; long double P, Q, R; string S;
	cin >> N >> P >> Q >> R >> S;
	vector<int> node_pos; vector<long double> dp;
	for (int i = 0; i < N; ++i) {
		if (i <= N - 3 && S.substr(i, 3) == "YES") {
			node_pos.push_back(i);
			dp.push_back(1.0L);
		}
		if (i <= N - 2 && S.substr(i, 2) == "NO") {
			node_pos.push_back(i);
			dp.push_back(0.0L);
		}
	}
	vector<pair<int, int> > merger;
	int depth = 0;
	for (int i = 0; i < N; ++i) {
		if (S[i] == '(') ++depth;
		if (S[i] == ')') --depth;
		if (i <= N - 6 && S.substr(i, 6) == "random") {
			merger.push_back(make_pair(depth * 3 + 2, i));
		}
		if (i <= N - 3 && S.substr(i, 3) == "and" && (i == 0 || i > N - 5 || S.substr(i - 1, 6) != "random")) {
			merger.push_back(make_pair(depth * 3 + 1, i));
		}
		if (i <= N - 2 && S.substr(i, 2) == "or") {
			merger.push_back(make_pair(depth * 3, i));
		}
	}
	sort(merger.begin(), merger.end(), [](pair<int, int> p1, pair<int, int> p2) { return p1.first != p2.first ? p1.first > p2.first : p1.second < p2.second; });
	set<int> current;
	for (int i = 0; i < node_pos.size(); ++i) {
		current.insert(i);
	}
	for (pair<int, int> i : merger) {
		int ptr = lower_bound(node_pos.begin(), node_pos.end(), i.second) - node_pos.begin();
		set<int>::iterator it = current.lower_bound(ptr);
		if (i.first % 3 == 2) {
			// RANDOM
			int lp = *it, rp = *(++it);
			dp[lp] = dp[lp] * dp[rp] * P + (1.0L - dp[lp] * dp[rp]) * Q;
			current.erase(rp);
		}
		if (i.first % 3 == 1) {
			// AND
			int rp = *it, lp = *(--it);
			dp[lp] = dp[lp] * dp[rp] * (1.0L - R) + (1.0L - dp[lp] * dp[rp]) * R;
			current.erase(rp);
		}
		if (i.first % 3 == 0) {
			// OR
			int rp = *it, lp = *(--it);
			dp[lp] = (1.0L - dp[lp]) * (1.0L - dp[rp]) * R + (1.0L - (1.0L - dp[lp]) * (1.0L - dp[rp])) * (1.0L - R);
			current.erase(rp);
		}
	}
	long double ans = dp[0];
	cout << int(ans * 100.0 + 1.0e-15L) << endl;
	return 0;
}
0