poj 3399 Product(数学题)

题目链接:http://poj.org/problem?id=3399

Product

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2837   Accepted: 686   Special Judge

Description

There is an array of N integer numbers in the interval from -30000 to 30000. The task is to select K elements of this array with maximal possible product.

Input

The input consists of + 1 lines. The first line contains N and K (1 ≤ K ≤ N ≤ 100) separated by one or several spaces. The others contain values of array elements.

Output

The output contains a single line with values of selected elements separated by one space. These values must be in non-increasing order.

Sample Input

4 2
1
7
2
0

Sample Output

7 2

Source

Northeastern Europe 2001, Western Subregion

思路:每次寻找最小的两个负数和最大的两个正数的乘积中较大的;

代码如下:

#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
#define MAXN 117
int main()
{
	int n, k;
	int a[MAXN], numz[MAXN], numf[MAXN];
	int ans[MAXN];
	int i, j;
	int num1, num2;
	while(~scanf("%d %d",&n,&k))
	{
		num1 = num2 = 0;
		for(i = 0; i < n; i++)
		{
			scanf("%d",&a[i]);
			if(a[i] >= 0)
				numz[num1++] = a[i];//大于等于零
			else
				numf[num2++] = a[i];//负数
		}
		sort(numz,numz+num1);
		sort(numf,numf+num2);
		int cont = 0;
		if(k&1)
		{
			k--;
			if(num1 > 0)
				ans[cont++] = numz[--num1];//k为奇数,且有正数,那么结果中必然会有至少一个正数
			else//没有大于等于零的数,即全为负数
			{
				for(i = num2-1; i > num2-k-1; i--)
				{
					printf("%d ",numf[i]);
				}
				printf("%d\n",numf[num2-k-1]);
				continue;
			}
		}
		j = 0;
		for(i = 0; i < k/2; i++)
		{
			int t1 = -4017;//初始化为一个小于给定范围的数字
			int t2 = -4017;
			if(num1 == 1 && num2-j == 1)
			{
				ans[cont++] = numz[--num1];
				ans[cont++] = numf[++j];
			}
			else
			{
				if(num1 > 1)
					t1 = numz[num1-1]*numz[num1-2];
				if(num2-j > 1)
					t2 = numf[j] * numf[j+1];
				if(t1 > t2)
				{
					ans[cont++] = numz[--num1];
					ans[cont++] = numz[--num1];
				}
				else
				{
					ans[cont++] = numf[j++];
					ans[cont++] = numf[j++];
				}
			}
		}
		sort(ans,ans+cont);
		for(i = cont-1; i > 0; i--)//从大到小输出
		{
			printf("%d ",ans[i]);
		}
		printf("%d\n",ans[0]);
	}
	return 0;
}

poj 3399 Product(数学题)

时间: 2024-10-11 22:20:49

poj 3399 Product(数学题)的相关文章

poj 3399 Product(模拟)

# include <stdio.h> # include <string.h> # include <algorithm> using namespace std; int cmp(int x,int y) { return x>y; } int main() { int a[110],a1[110],a2[110],ans[110]; int n,k,k1,k2,i,k3; while(~scanf("%d%d",&n,&k

poj 3399 Product(数学)

主题链接:http://poj.org/problem?id=3399 Product Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2837   Accepted: 686   Special Judge Description There is an array of N integer numbers in the interval from -30000 to 30000. The task is to sele

PKU3399贪心

原题http://poj.org/problem?id=3399 Product Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2839   Accepted: 688   Special Judge Description There is an array of N integer numbers in the interval from -30000 to 30000. The task is to select 

POJ1845Sumdiv题解--约数之和

题目链接 https://cn.vjudge.net/problem/POJ-1845 分析 \(POJ\)里的数学题总是这么妙啊 首先有一个结论就是\(A=\prod{ \ {p_i}^{c_i} \ }\),那么\(A\)所有约数之和为\((1+p_1+p_1^2+..+p_1^{c_1}) * (1+p_2+p_2^2+...+p_2^{c_2}) ... (1+p_n +p_n^2 +... + p_n^{c_n})\) 这个好像数学归纳法可证,但是感性理解一下也不难 于是这道题就是求\

poj 3117 World Cup(简单数学题)

题目链接:http://poj.org/problem?id=3117 World Cup Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8634   Accepted: 4327 Description A World Cup of association football is being held with teams from around the world. The standing is based on

poj 2309 BST(数学题)

BST Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8440   Accepted: 5093 Description Consider an infinite full binary search tree (see the figure below), the numbers in the nodes are 1, 2, 3, .... In a subtree whose root node is X, we c

zoj 2358,poj 1775 Sum of Factorials(数学题)

题目poj 题目zoj //我感觉是题目表述不确切,比如他没规定xi能不能重复,比如都用1,那么除了0,都是YES了 //算了,这种题目,百度来的过程,多看看记住就好 //题目意思:判断一个非负整数n能否表示成几个数的阶乘之和 //这里有一个重要结论:n!>(0!+1!+……+(n-1)!), //证明很容易,当i<=n-1时,i!<=(n-1)!,故(0!+1!+……+(n-1)!)<=n*(n-1)!=n!. // 由于题目规定n<=1000000,而10!=362880

poj 3045 Cow Acrobats(数学题)

题目链接:http://poj.org/problem?id=3045 Description Farmer John's N (1 <= N <= 50,000) cows (numbered 1..N) are planning to run away and join the circus. Their hoofed feet prevent them from tightrope walking and swinging from the trapeze (and their last

POJ 1019 数学题

1 #include <cstdio> 2 #include <cstring> 3 4 using namespace std; 5 6 int sum[20]; 7 //sum[i]表示尾数为i的组最大可达到的数字个数 8 void init() 9 { 10 sum[0] = 0; 11 sum[1] = 9; 12 sum[2] = 189; 13 sum[3] = 2889; 14 sum[4] = 38889; 15 sum[5] = 488889; 16 sum[6]