0
点赞
收藏
分享

微信扫一扫

CodeForces 55B Smallest number(DFS)

双井暮色 2023-06-09 阅读 48


题意:给你4个数,然后给你三个符号,问你怎么安排着4个数的顺序,可以使得最后的答案最小,4个数和三个符号,选两个数和第一个符号出来,得到一个数,3个数和2个符号,选两个数和第二个符号出来,得到一个数,2个数和1个符号,得到答案,要求使得答案最小

思路:直接爆搜就好了



#include<bits/stdc++.h>
using namespace std;
#define LL long long
char s[4];
LL ans = 1LL<<60;
void dfs3(LL a,LL b)
{
	LL temp=0;
    if (s[2]=='+')
	    temp = a+b;
    else
	    temp = a*b;
    ans = min(ans,temp);	
}
void dfs2(LL a,LL b,LL c)
{
	if (s[1]=='+')
	{
		dfs3(a+b,c);
		dfs3(a+c,b);
		dfs3(b+c,a);
	}

	else
	{
		dfs3(a*b,c);
		dfs3(a*c,b);
		dfs3(b*c,a);
	}
}
void dfs1(LL a,LL b,LL c,LL d)
{
	if (s[0]=='+')
	{
		dfs2(a+b,c,d);
        dfs2(a+c,b,d);
		dfs2(a+d,b,c);
		dfs2(b+d,a,c);
		dfs2(b+c,a,d);
		dfs2(c+d,a,b);
	}
	else
	{
		dfs2(a*b,c,d);
        dfs2(a*c,b,d);
		dfs2(a*d,b,c);
		dfs2(b*d,a,c);
		dfs2(b*c,a,d);
		dfs2(c*d,a,b);
	}
}
int main()
{
	LL a,b,c,d;
	cin >> a >> b >> c >> d;
    cin >> s[0] >> s[1]>>s[2];
	dfs1(a,b,c,d);
	cout <<ans << endl;
}




Description



Recently, Vladimir got bad mark in algebra again. To avoid such unpleasant events in future he decided to train his arithmetic skills. He wrote four integer numbers abcd



Input



First line contains four integers separated by space: 0 ≤ a, b, c, d ≤ 1000



Output



Output one integer number — the minimal result which can be obtained.

Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cin (also you may use %I64d).



Sample Input



Input



1 1 1 1 + + *



Output



3



Input



2 2 2 2 * * +



Output



8



Input



1 2 3 4 * + +



Output



9






举报

相关推荐

0 条评论