Submission #630660

# Submission time Handle Problem Language Result Execution time Memory
630660 2022-08-16T20:43:45 Z inksamurai Kvalitetni (COCI16_kvalitetni) C++17
120 / 120
7 ms 2152 KB
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rng(i,c,n) for(int i=c;i<n;i++)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3phCa4T ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
// e

signed main(){
_3phCa4T;
	int n;
	cin>>n;
	vi a(n);
	rep(i,n){
		cin>>a[i];
	}
	string s;
	cin>>s;
	const int m=sz(s);
	vi rp(m);
	vi stk;
	rep(i,m){
		if(s[i]=='('){
			stk.pb(i);
		}else if(s[i]==')'){
			rp[stk.back()]=i;
			stk.pop_back();
		}
	}
	using ld=long double;
	auto dfs=[&](auto self,int l,int r)->ld{
		if(l==r-2){
			return a[0];
		}
		vec(ld) rbts;
		char expr;
 		for(int i=l+1;i<r;i++){
			if(s[i]=='('){
				rbts.pb(self(self,i,rp[i]));
				i=rp[i];
			}else{
				expr=s[i];
			}
		}
		ld sun=0;
		for(auto v:rbts){
			sun+=v;
		}
		ld need=a[sz(rbts)-1];
		if(expr=='+'){
			return min(need,sun);
		}
		if(sun>need){
			sort(rbts.rbegin(), rbts.rend());
			ld x=rbts[0];
			rng(i,1,sz(rbts)+1){
				if(i<sz(rbts) and sun-(x-rbts[i])*i>=need){
					sun-=(x-rbts[i])*i;
					x=rbts[i];
				}else{
					rep(j,i){
						rbts[j]=x;
					}
					ld k=(sun-need)/i;
					rep(j,i){
						rbts[j]-=k;
					}
					break;
				}
			}
		}
		ld _prod=1;
		for(auto v:rbts){
			_prod*=v;
		}
		return _prod;
	};
	cout<<fixed<<setprecision(16);
	ld res=dfs(dfs,0,rp[0]);
	print(res);
}

Compilation message

kvalitetni.cpp: In lambda function:
kvalitetni.cpp:60:3: warning: 'expr' may be used uninitialized in this function [-Wmaybe-uninitialized]
   60 |   if(expr=='+'){
      |   ^~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 7 ms 1376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 900 KB Output is correct
2 Correct 5 ms 1504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1248 KB Output is correct
2 Correct 7 ms 2152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 980 KB Output is correct
2 Correct 6 ms 2000 KB Output is correct