0% found this document useful (0 votes)
1 views2 pages

harmonics arb

hormonics details
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views2 pages

harmonics arb

hormonics details
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

harmonics arb

#include <iostream>
#include <vector>
#include <stack>
#include <climits>
#include <cctype>
using namespace std;

pair<int, int> ev(const string& exp, bool optimized, int a, int b, int c, int d) {
stack<int> s; // Stack to store numbers
stack<int> cs; // Stack to store operation costs

for (char i : exp) {


if (isdigit(i)) {
s.push(i - '0'); // Convert character to integer
cs.push(0); // Initial cost is 0 for numbers
} else {
int n2 = s.top(); s.pop();
int c2 = cs.top(); cs.pop();
int n1 = s.top(); s.pop();
int c1 = cs.top(); cs.pop();

if (i == '+') {
s.push(n1 + n2);
cs.push(c1 + c2 + a);
} else if (i == '-') {
s.push(n1 - n2);
cs.push(c1 + c2 + b);
} else if (i == '*') {
s.push(n1 * n2);
cs.push(c1 + c2 + c);
} else if (i == '/') {
s.push(n1 / n2); // Assuming integer division
cs.push(c1 + c2 + d);
}
}
}
return {s.top(), cs.top()}; // Return the result and the total cost
}

void solve() {
string exp;
cin >> exp; // Read the expression

int a, b, c, d, e;
cin >> a >> b >> c >> d >> e; // Read the costs for operations

int mn = INT_MAX; // Initialize minimum cost to infinity


int res = 0; // Variable to store the result

// Try without optimization


auto [val, cost] = ev(exp, false, a, b, c, d);
mn = cost;
res = val;

// Try optimizing each operator


for (size_t i = 0; i < exp.size(); ++i) {
if (!isdigit(exp[i])) {
string tmp = exp;
tmp[i] = tolower(tmp[i]); // Convert to lowercase for "optimization"

auto [val, cost] = ev(tmp, true, a, b, c, d);


cost += e; // Add optimization cost

if (cost < mn) {


mn = cost;
res = val;
}
}
}

cout << mn << " " << res << endl; // Print the minimum cost and result
}

int main() {
solve(); // Call the solve function
return 0;
}

You might also like