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. 【Mysql】安装 mysql-5.7.5 指南

    因为同学需要安装mysql,安装过程,一路百度,在这里记录一下步奏.以后还会用到. 1.mysql-5.7.5-m15-winx64.zip下载 官方网站下载地址:http://cdn.mysql.c ...

  2. 给table中某一列的文字右对齐

    一般来说,没写过jquery的前端人员,肯定是定义一个class,给每一行的那列加上align_r{text-align:right}.这是很麻烦的. 所以用jquery来写,可以$("ta ...

  3. handler.post 为什么要将thread对象post到handler中执行呢?

    转载网址:http://www.cnblogs.com/crazypebble/archive/2011/03/23/1991829.html在Android中使用Handler和Thread线程执行 ...

  4. jsoup使用选择器语法来查找元素

    问题 你想使用类似于CSS或jQuery的语法来查找和操作元素. 方法 可以使用Element.select(String selector) 和 Elements.select(String sel ...

  5. ECSHOP - 二次开发指南---购物车篇

    第一个问题 保存用户购物车数据ECSHOP的购物车数据,是以Session 方式存储在数据库里,并在Session结束后 ,Distroy 掉,解决方法是: 1.购物车内容读取方式. 更改登陆后购物车 ...

  6. ajax-Ajax试题

    ylbtech-doc:ajax-Ajax试题 Ajax 1.A,Ajax试题返回顶部 001.{Ajax题目}使用Ajax可带来便捷有()(选择3项)      A)减轻服务器的负担      B) ...

  7. 带宽计算-大B与小b的区别

    原文来自:http://blog.sina.com.cn/s/blog_4b9c0e3601008yf9.html 在计算机网络.IDC机房中,其宽带速率的单位用bps(或b/s)表示:换算关系为:1 ...

  8. Asp.net TextBox常规输入验证

    Asp.net TextBox只能输入数字<asp:textbox id="TextBox1" onkeyup="if(isNaN(value))execComma ...

  9. iOS开发常用输入校验

    1.数字字符校验 #define NUMBERSPERIOD @"0123456789." - (BOOL)CheckInput:(NSString *)string { NSCh ...

  10. 如何将自定义RPM包加入YUM

    1 前言 在很多时候进行编译了自己的RPM包,在搭建YUM的时候,希望将自定义的RPM加入到YUM源中,从而出现了下列方法. 2. 将RPM包加入YUM源 2.1 查看目前repodata位置 YUM ...