Submission #244048

# Submission time Handle Problem Language Result Execution time Memory
244048 2020-07-02T13:56:57 Z Vimmer Kvalitetni (COCI16_kvalitetni) C++14
120 / 120
24 ms 11532 KB
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("no-stack-protector")

#define F first
#define S second
#define sz(x) int(x.size())
#define pb push_back
#define N 300101
#define M ll(1e9 + 7)
#define inf 1e9 + 1e9

using namespace std;
//using namespace __gnu_pbds;

typedef long double ld;
typedef long long ll;
typedef short int si;
typedef pair <int, int> par;
typedef array <int, 2> a2;

//typedef tree <int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;


ld z[N], f[N];

ll znak[N];
vector <ll> g[N];

void dfs(ll v)
{
    if (sz(g[v]) == 0) {f[v] = z[0]; return;}

    if (znak[v] == 1) f[v] = 1;

    vector <ld> gr; gr.clear();

    ld sum = 0;

    for (auto it : g[v])
    {
        dfs(it);

        gr.pb(f[it]);

        sum += f[it];
    }

    if (znak[v] == 0) {f[v] = min(sum, z[sz(gr) - 1]); return;}

    sort(gr.begin(), gr.end());

    sum = z[sz(gr) - 1];

    for (int i = 0; i < sz(gr); i++)
    {
        ld cur = sum / ld(sz(gr) - i);

        if (gr[i] - cur >= 0)
        {
            for (int j = i; j < sz(gr); j++) gr[j] = cur;

            break;
        }
        else sum -= gr[i];
    }

    for (auto it : gr) f[v] *= it;
}
int main()
{
    //freopen("input.txt", "r", stdin); //freopen("output4.txt", "w", stdout);

    ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    ll n;

    cin >> n;

    for (ll i = 0; i < n; i++) cin >> z[i];

    string s;

    cin >> s;

    stack <ll> st;

    ll id = 1;

    for (ll i = 0; i < sz(s); i++)
    {
        if (s[i] == '(') {if (id != 1) g[st.top()].pb(id); st.push(id++);}

        if (s[i] == ')') st.pop();

        if (s[i] == '*') znak[st.top()] = 1;

        if (s[i] == '+') znak[st.top()] = 0;
    }

    dfs(1);

    cout << setprecision(6) << fixed;

    cout << f[1] << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 10 ms 7424 KB Output is correct
2 Correct 10 ms 7424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7424 KB Output is correct
2 Correct 9 ms 7424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7552 KB Output is correct
2 Correct 9 ms 7424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7424 KB Output is correct
2 Correct 10 ms 7552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 7424 KB Output is correct
2 Correct 9 ms 7424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7424 KB Output is correct
2 Correct 10 ms 7680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7424 KB Output is correct
2 Correct 18 ms 9612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 8320 KB Output is correct
2 Correct 17 ms 9996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 9228 KB Output is correct
2 Correct 24 ms 11532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 8704 KB Output is correct
2 Correct 19 ms 11156 KB Output is correct