B. Smallest number

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/55/problem/B

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 on the blackboard. During each of the next three minutes he took two numbers from the blackboard (not necessarily adjacent) and replaced them with their sum or their product. In the end he got one number. Unfortunately, due to the awful memory he forgot that number, but he remembers four original numbers, sequence of the operations and his surprise because of the very small result. Help Vladimir remember the forgotten number: find the smallest number that can be obtained from the original numbers by the given sequence of operations.

Input

First line contains four integers separated by space: 0 ≤ a, b, c, d ≤ 1000 — the original numbers. Second line contains three signs ('+' or '*' each) separated by space — the sequence of the operations in the order of performing. ('+' stands for addition, '*' — multiplication)

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

1 1 1 1
+ + *

Sample Output

3

HINT

题意

给你4个数,然后给你三个符号,问你怎么安排着4个数的顺序,可以使得最后的答案最小

4个数和三个符号,选两个数和第一个符号出来,得到一个数

3个数和2个符号,选两个数和第二个符号出来,得到一个数

2个数和1个符号,得到答案

要求使得答案最小

题解:

暴力枚举。。。

代码:

#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std; long long ans = 1LL<<;
char S[];
void solve2(long long a,long long b) {
long long x = (S[] == '+') ? (a + b) : (a * b);
if (ans > x) {
ans = x;
}
}
void solve3(long long a,long long b,long long c) {
if (S[] == '+') {
solve2(a + b, c);
solve2(a + c, b);
solve2(b + c, a);
} else {
solve2(a * b, c);
solve2(a * c, b);
solve2(b * c, a);
}
}
void solve4(long long a,long long b,long long c,long long d) {
if (S[] == '+') {
solve3(a + b, c, d);
solve3(a + c, b, d);
solve3(a + d, b, c);
solve3(b + c, a, d);
solve3(b + d, a, c);
solve3(c + d, a, b);
} else {
solve3(a * b, c, d);
solve3(a * c, b, d);
solve3(a * d, b, c);
solve3(b * c, a, d);
solve3(b * d, a, c);
solve3(c * d, a, b);
}
} int main() {
long long a, b, c, d;
cin>>a>>b>>c>>d;
for(int i=;i<=;i++)
cin>>S[i];
solve4(a,b,c,d);
cout<<ans<<endl;
}

Codeforces Beta Round #51 B. Smallest number dfs的更多相关文章

  1. Codeforces Beta Round #51 D. Beautiful numbers 数位dp

    D. Beautiful numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55/p ...

  2. Codeforces Beta Round #51 C. Pie or die 博弈论找规律 有趣的题~

    C. Pie or die Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55/problem/ ...

  3. Codeforces Beta Round #51 A. Flea travel 水题

    A. Flea travel Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55/problem ...

  4. Codeforces Beta Round #51 D. Beautiful numbers

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  5. Codeforces Beta Round #51 D. Beautiful numbers(数位dp)

    题目链接:https://codeforces.com/contest/55/problem/D 题目大意:给你一段区间[l,r],要求这段区间中可以整除自己每一位(除0意外)上的数字的整数个数,例如 ...

  6. codeforces 55d//Beautiful numbers// Codeforces Beta Round #51

    题意:一个数能整除它所有的位上的数字(除了0),统计这样数的个数. 注意离散化,为了速度更快需存入数组查找. 不要每次memset,记录下已有的长度下符合条件的个数. 数位dp肯定是从高位到低位. 记 ...

  7. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  8. Codeforces Beta Round #62 题解【ABCD】

    Codeforces Beta Round #62 A Irrational problem 题意 f(x) = x mod p1 mod p2 mod p3 mod p4 问你[a,b]中有多少个数 ...

  9. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

随机推荐

  1. 【转】C++ 内存分配(new,operator new)详解

    本文主要讲述C++ new运算符和operator new, placement new之间的种种关联,new的底层实现,以及operator new的重载和一些在内存池,STL中的应用. 一 new ...

  2. TabWidget/TabHost的两种使用方法

    1.概念 盛放Tab的容器就是TabHost.TabHost的实现有两种方式: 第一种继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost.各个Tab ...

  3. UITextView 相关知识点

    1.得到UITextView的高度 - (CGRect)contentSizeRectForTextView:(UITextView *)textView { [textView.layoutMana ...

  4. ORA-15177: cannot operate on system aliases (DBD ERROR: OCIStmtExecute)

    ASM操作的时候,删除一个文件夹,删除不了,报错如下: ASMCMD> ls -l Type Redund Striped Time Sys Name Y IPAP/ ASMCMD> rm ...

  5. web.py实现jsonp

    浏览器端请求 $.getJSON("/currenttime?callback=?", function (json){ $("#time").html(jso ...

  6. Linux虚拟机创建后如何进行登录(Windows Azure)

    Linux虚拟机创建后如何进行登录 若要管理虚拟机的设置以及在其上运行的应用程序,可以使用安全外壳 (SSH) 客户端.为此,您必须在计算机上安装要用于访问虚拟机的 SSH 客户端.您可以选择很多 S ...

  7. 集群——LVS理论(转)

    原文:http://caduke.blog.51cto.com/3365689/1544229 当单个服务器性能 不能满足日益增多访问流量时,服务器的扩展策略: Scale Up :向上扩展,提升单个 ...

  8. JDBC学习笔记(8)——数据库连接池(dbcp&C3P0)

    JDBC数据库连接池的必要性 一.在使用开发基于数据库的web程序时,传统的模式基本是按一下步骤: 1)在主程序(如servlet/beans)中建立数据库连接 2)进行sql操作 3)断开数据库连接 ...

  9. 问题-Delphi2007跟踪变量时提示“E2171 Variable 'APolygon' inaccessible here due to optimization”

    问题现象:Delphi2007跟踪变量时提示“E2171 Variable 'APolygon' inaccessible here due to optimization” . 问题原因:可能是因为 ...

  10. TQJson序列和还原clientdataset.data

    TQJson是QDAC开源项目的JSON对象,其特点是跨平台,功能强大. 序列 procedure TForm2.Button2Click(Sender: TObject);var AJson: TQ ...