답안 #88726

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
88726 2018-12-08T02:57:33 Z jasony123123 Kvalitetni (COCI16_kvalitetni) C++11
120 / 120
11 ms 4220 KB
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define FOR(i,start,end) for(int i=start;i<(int)(end);i++)
#define FORE(i,start,end) for(int i=start;i<=(int)end;i++)
#define RFOR(i,start,end) for(int i = start; i>end; i--)
#define RFORE(i,start,end) for(int i = start; i>=end; i--)
#define all(a) a.begin(), a.end()
#define mt make_tuple
#define v vector
#define sf scanf
#define pf printf
#define dvar(x) cout << #x << " := " << x << "\n"
#define darr(x,n) FOR(i,0,n) cout << #x << "[" << i << "]" << " := " << x[i] << "\n"
typedef long long ll;
typedef long double ld;
typedef pair<int, int > pii;
typedef pair<ll, ll> pll;
const ll MOD = 1000000007LL;
const ll PRIME = 105943LL;
const ll INF = 1e18;
//template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<class T> void minn(T &a, T b) { a = min(a, b); }
template<class T> void maxx(T &a, T b) { a = max(a, b); }
void io() {
#ifdef LOCAL_PROJECT 
	freopen("input.in", "r", stdin); freopen("output.out", "w", stdout);
#else 
	/* online submission */
#endif 
	ios_base::sync_with_stdio(false); cin.tie(NULL);
}
/**************************COCI 2016-2017 Contest 3 Problem 4 *************************/

string expr;
int ending[1000000];
int K;
ll Z[55];


double calculateSum(v<double> &vals) {
	double sum = 0;
	for (auto d : vals)
		sum += d;
	return min((double)Z[vals.size()], sum);
}

double calculateProduct(v<double> &vals) {
	sort(all(vals));
	reverse(all(vals));
	double targetSum = Z[vals.size()];
	double prod = 1;
	while (!vals.empty()) {
		double avg = targetSum / vals.size();

		if (vals.back() < avg) {
			prod *= vals.back();
			targetSum -= vals.back();
			vals.pop_back();
			continue;
		}

		FOR(i, 0, vals.size())
			prod *= avg;
		break;
	}
	return prod;
}

double solve(int l, int r) {
	v<double> vals;
	char op = '|';
	for (int i = l + 1; i < r; i++) {
		if (expr[i] == '(') {
			vals.push_back(solve(i, ending[i]));
			i = ending[i];
		}
		else if (expr[i] == '+'){
			assert(op == '|' || op == '+');
			op = '+';
		}
		else if (expr[i] == '*') {
			assert(op == '|' || op == '*');
			op = '*';
		}
		else if (expr[i] == '?')
			return Z[1];
		else
			assert(0);
	}
	assert(vals.size() > 1);
	return (op == '+' ? calculateSum(vals) : calculateProduct(vals));
}

void genEnding() {
	stack<int> open;
	FOR(i, 0, expr.size()) {
		char c = expr[i];
		if (c == '(')
			open.push(i);
		else if (c == ')') {
			ending[open.top()] = i;
			open.pop();
		}
	}
}

int main() {
	io();
	cin >> K;
	FORE(i, 1, K) cin >> Z[i];
	cin >> expr;
	genEnding();
	cout << std::fixed << std::setprecision(9) << solve(0, expr.size() - 1) << "\n";
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 2 ms 652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 652 KB Output is correct
2 Correct 2 ms 652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 728 KB Output is correct
2 Correct 3 ms 800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 800 KB Output is correct
2 Correct 2 ms 800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 800 KB Output is correct
2 Correct 3 ms 876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 876 KB Output is correct
2 Correct 7 ms 2108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 2108 KB Output is correct
2 Correct 8 ms 2700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 2700 KB Output is correct
2 Correct 11 ms 3872 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 3872 KB Output is correct
2 Correct 9 ms 4220 KB Output is correct