UVA - 10690 Expression Again

Description

Problem C

Expression Again

Input: standard input

Output: standard output

TimeLimit: 6 seconds

You are given an algebraic expression of the form(x1+x2+x3+.....+xn)*(y1+y2+.........+ym) and
(n+m) integers. Youhave to find the maximum and minimum value of the expression using the givenintegers. For example if you are given
(x1+x2)*(y1+y2) and you are given1, 2, 3 and 4. Then maximum value is
(1+4)*(2+3) = 25whereas minimum value is (4+3)*(2+1) = 21.

Input

Each input set starts with two positive integers N,M (less than 51). Next line follows
(N+M) integers which are in therange of -50 to
50
. Input is terminated by end of file. Therewill be atmost 110 testcases.

Output

Output is one line for each case, maximum valuefollowed by minimum value.

SampleInput                           Outputfor Sample Input

2 2
1 2 3 4
3 1
1 2 3 4
2 2
2 2 2 2

题意:给你n+m个数,让你分成n个和m个,求它们的和的积的最大和最小

思路: 动态规划,设dp[i][j]表示用i个组成j的可能性,最后在从所有的可能里求解

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = 110;

int dp[maxn][maxn*maxn];
int n, m;

int main() {
	while (scanf("%d%d", &n, &m) != EOF) {
		vector<int> ve(n+m+1);
		int sum = 0;
		for (int i = 1; i <= n+m; i++) {
			scanf("%d", &ve[i]);
			sum += ve[i];
			ve[i] += 50;
		}
		memset(dp, 0, sizeof(dp));
		dp[0][0] = 1;
		for (int i = 1; i <= n+m; i++) {
			for (int j = min(i, n); j >= 1; j--) //反着来消除后效性
				for (int k = 0; k <= 10000; k++)
					if (dp[j-1][k])
						dp[j][k+ve[i]] = 1;
		}
		int Max = -5000;
		int Min = 5000;
		for (int i = 0; i <= 10000; i++)
			if (dp[n][i]) {
				int tmp = i - 50 * n;
				Max = max(Max, tmp*(sum-tmp));
				Min = min(Min, tmp*(sum-tmp));
			}
		printf("%d %d\n", Max, Min);
	}
	return 0;
}

UVA - 10690 Expression Again,布布扣,bubuko.com

时间: 2024-10-26 00:10:44

UVA - 10690 Expression Again的相关文章

uva 10312 - Expression Bracketing(Catalan+SuperCatalan)

Set集合的配置 数据表的创建:表关系一个员工拥有多个身份 create table EMPLOYEE ( id INT NOT NULL auto_increment, first_name VARCHAR(20) default NULL, last_name VARCHAR(20) default NULL, salary INT default NULL, PRIMARY KEY (id) ); create table CERTIFICATE ( id INT NOT NULL aut

UVA 10312 - Expression Bracketing(数论+Catalan数)

题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=1253">10312 - Expression Bracketing 题意:有n个x,要求分括号,推断非二叉表达式的个数. 思路:二叉表达式的计算方法就等于是Catalan数的,那么仅仅要计算出总数,用总数减去二叉表达式个数.得到的就是非二叉表达式的个数. 那么计算方法是什么呢. 看题目中的图,对于n = 4的情况,能够分为这几种情况来讨论

UVa 10312 - Expression Bracketing

题目:求n个元素构成的树中,不是二叉树的个数. 分析:组合,计数,卡塔兰数. n个元素组成的二叉树的个数为卡塔兰数Cn-1:有如下递推关系: n个元素组成的所有树的个数为超卡塔兰数Sn:有如下递推关系: 卡特兰数参考:http://blog.csdn.net/mobius_strip/article/details/39229895 超卡塔兰数参考:http://mathworld.wolfram.com/SuperCatalanNumber.html 输出S(n)- C(n-1)即可. 说明:

UVa 112 - Tree Summing(树的各路径求和,递归)

题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=48 Tree Summing  Background LISP was one of the earliest high-level programming languages and, with FORTRAN, is one of the olde

UVa 108 - Maximum Sum(最大连续子序列)

题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=44  Maximum Sum  Background A problem that is simple to solve in one dimension is often much more difficult to solve in more th

UVA - 112 - Tree Summing (数的求和!栈的应用!)

UVA - 112 Tree Summing Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description  Tree Summing  Background LISP was one of the earliest high-level programming languages and, with FORTRAN, is one of the oldest

UVA Tree Summing

题目如下:  Tree Summing  Background LISP was one of the earliest high-level programming languages and, withFORTRAN, is one of the oldest languages currently being used. Lists,which are the fundamental data structures in LISP, can easily be adaptedto repr

uva 10400 Game Show Math (填合适的运算符)

看到这种填合适的运算符之类的题目,第一感觉就是用dfs来枚举递归. 但邮箱道题目算法设计里面那么大的数据,想到有可能会超时. 用最直白的简单的方法dfs一遍后交上,超时. --需要判重和边界结束条件. 在所有能剪断的地方痛下狠手,狂加特判+return: 然后就炒鸡快了 #include<iostream> #include<cstring> #include<cstdio> #define ADD 32000 using namespace std; int arr[

UVA 465-- Overflow (atof 函数)

 Overflow  Write a program that reads an expression consisting of two non-negative integer and an operator. Determine if either integer or the result of the expression is too large to be represented as a ``normal'' signed integer (type integer if you