UVA - 10700 - Camel trading (简单贪心)

UVA - 10700

Camel trading

Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

Submit Status

Description

Problem E - Camel trading

Time Limit: 1 second

Background

Aroud 800 A.D., El Mamum, Calif of Baghdad was presented the formula 1+2*3*4+5, which had its origin in the financial accounts of a camel transaction. The formula lacked parenthesis and was ambiguous. So, he
decided to ask savants to provide him with a method to find which interpretation is the most advantageous for him, depending on whether is is buying or selling the camels.

The Problem

You are commissioned by El Mamum to write a program that determines the maximum and minimum possible interpretation of a parenthesis-less expression.

Input

The input consists of an integer N, followed by N lines, each containing an expression. Each expression is composed of at most 12 numbers, each ranging between 1 and 20,
and separated by the sum and product operators +and *.

Output

For each given expression, the output will echo a line with the corresponding maximal and minimal interpretations, following the format given in the sample output.

Sample input

3
1+2*3*4+5
4*18+14+7*10
3+11+4*1*13*12*8+3*3+8

Sample output

The maximum and minimum are 81 and 30.
The maximum and minimum are 1560 and 156.
The maximum and minimum are 339768 and 5023.

Source

Root :: Competitive Programming 3: The New Lower Bound of Programming Contests (Steven & Felix Halim) :: Problem Solving Paradigms :: Greedy :: Non
Classical, Usually Harder

Root :: AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 4. Algorithm Design

Root :: Competitive Programming 2: This increases the lower bound of Programming Contests. Again (Steven & Felix Halim) :: Problem Solving Paradigms :: Greedy
- Standard

Submit Status

思路:很明显,当加法优于乘法先算出时有最大,乘法先算出时有最小

AC代码(略挫,没用stack写):

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#define LL long long
using namespace std;

int N;
char str[100];
LL ope[20];
LL num[20];
LL sum, osum;

LL fun(char s[], int n) {	//字符串转换为数字
	LL tmp = 0, cnt = 0;
	for(int i = n; isdigit(s[i]); i++) {
		tmp = tmp * 10 + (s[i] - '0');
		cnt ++;
	}
	num[sum++] = tmp;
	return cnt - 1;
}

int main() {
	scanf("%d", &N);
	while(N--) {
		sum = osum = 0;
		memset(num, 0, sizeof(num));
		memset(ope, 0, sizeof(ope));
		scanf("%s", str);
		for(int i = 0; str[i] != '\0'; i++) {
			if(isdigit(str[i])) {
				i += fun(str, i);
			}
			else
			{
				if(str[i] == '*') ope[osum++] = 0;
				else ope[osum++] = 1;
			}
		}

		//for(int i = 0; i < sum; i++) printf("%d ", num[i]);
		//printf("\n");
		//for(int i = 0; i < osum; i++) printf("%d ", ope[i]);
		//printf("\n");

		LL MAX = 1, MIN = 0;
		//找最小
		LL tnum[20];
		for(int i = 0; i < 20; i++) tnum[i] = num[i];
		for(int i = 0; i < osum; i++) {
			if(ope[i] == 0) {
				int t = i;
				LL tmp = tnum[i] * tnum[i + 1];
				tnum[i + 1] = 0;
				for(int j = i + 1; j < osum && ope[j] == 0; j++) {
					tmp *= tnum[j + 1];
					i = j;
					tnum[j + 1] = 0;
				}
				tnum[t] = tmp;
			}
		}
		//for(int i= 0; i < sum; i++) printf("%d ", tnum[i]);
		for(int i = 0; i < sum; i++) MIN += tnum[i];
		//找最大
		for(int i = 0; i < osum; i++) {
			if(ope[i] == 1) {
				int t = i;
				LL tmp = num[i] + num[i + 1];
				num[i + 1] = 1;
				for(int j = i + 1; j < osum && ope[j] == 1; j++) {
					tmp += num[j + 1];
					i = j;
					num[j + 1] = 1;
				}
				num[t] = tmp;
			}
		}
		for(int i = 0; i < sum; i++) MAX *= num[i];

		printf("The maximum and minimum are %lld and %lld.\n", MAX, MIN);
	}
	return 0;
}
时间: 2024-10-14 06:34:36

UVA - 10700 - Camel trading (简单贪心)的相关文章

uva:10700 - Camel trading(贪心)

题目:10700 - Camel trading 题目大意:给出一些表达式,表达式由数字和加号乘号组成,数字范围[1,20].这些表达式可能缺少了括号,问这样的表达式加上括号后能得到的最大值和最小值. 解题思路:因为这些数的都是正整数,所以可以用贪心.不然看出最大值就是先做完加法在做乘法,最小值就是先做乘法在做加法.注意这里的数值要用long long 因为比表达式的值可能会超过int. 代码: #include <stdio.h> #include <string.h> cons

UVA 10700 Camel trading(计算式子加减乘除的优先级处理)

Camel trading Time Limit: 1 second Background Aroud 800 A.D., El Mamum, Calif of Baghdad was presented the formula 1+2*3*4+5, which had its origin in the financial accounts of a camel transaction. The formula lacked parenthesis and was ambiguous. So,

UVA 10700 Camel trading

1 #include <iostream> 2 #include <cstdio> 3 #include <stack> 4 #define sc(x) scanf("%d",&x) 5 #define sc1(x) scanf("%lld",&x) 6 #define pf(x) printf("%d\n",x) 7 #define FOR(i,b,e) for(int i=b;i<N;

uva:10700 - Camel trading(贪婪)

题目:10700 - Camel trading 题目大意:给出一些表达式,表达式由数字和加号乘号组成,数字范围[1,20].这些表达式可能缺少了括号,问这种表达式加上括号后能得到的最大值和最小值. 解题思路:由于这些数的都是正整数,所以能够用贪心.不然看出最大值就是先做完加法在做乘法,最小值就是先做乘法在做加法.注意这里的数值要用long long 由于比表达式的值可能会超过int. 代码: #include <stdio.h> #include <string.h> const

POJ 2393 Yogurt factory(简单贪心)

http://poj.org/problem?id=2393 题意:任务规定,一个酸奶制造厂,在n个星期内,分别要向外提供y[i]unit的酸奶.已知这个制造厂第i周制造每unit酸奶的费用为c[i],储存室储存每1unit酸奶1星期的费用为s.问要完成这个任务的最小费用是多少. . . . . . . . . . . . . . 思路:简单贪心.维护一个目前最优的代价minc = min(c, minc + s),然后求和. #include <iostream> using namespa

uva 1521 - GCD Guessing Game(贪心)

题目链接:uva 1521 - GCD Guessing Game 题目大意:给定一个数N,现在又一个数x,在1~N之间,现在每次可以猜一个数a,返回gcd(x,a),问说最少猜几次可以确定x. 解题思路:其实就将1~N里面的素数都要考虑一遍,因为有一个N的限制,所以每次选出来的素数的积不大于N即可. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const

POJ 3069 Saruman&#39;s Army (简单贪心)

Saruman's Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5343   Accepted: 2733 Description Saruman the White must lead his army along a straight path from Isengard to Helm's Deep. To keep track of his forces, Saruman distributes se

USACO SECTION1 2.1 Milking Cows 简单贪心

题目链接: http://train.usaco.org/usacoprob2?a=p8taXWtZBpU&S=milk2 题目描述: 给出n组数, 求出最长连续有数区间与最长连续无数区间 解题思路: 简单贪心, 先排序, 然后如果有交集判断右端点,如果右端点大则延伸, 否则continue, 如果无交集更新两个ans. 代码: http://train.usaco.org/usacoprob2?a=p8taXWtZBpU&S=milk2 思考: 简单的贪心我的代码却WA了, 记住一切从简

uva 10026 Shoemaker&#39;s Problem(贪心+排序)

虽然是个水题,但是在一些细节上wa了几次,好像不支持'\b'退格符号,我用在了输出空格那,结果wa了...白白 wa了几次...题意是看的题解..今天只写了两道题,速度有点慢,得加快了,以后得先认真读懂题目,题目读懂了 就相当于做出来一半然后仔细动脑想想,有想法了再敲,不能盲目的做题.另外,热烈祝贺今天c++ primer看到 了100页 思路: 这道题是让给的数据是每件工作需要做的天数和每耽误一天所需要的费用,让求一个序列使得付费最小,如果有相同答 案把字典树最小的输出...输出的是序号,该件