主题链接: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(数学)的更多相关文章

  1. poj 1845 POJ 1845 Sumdiv 数学模板

    筛选法+求一个整数的分解+快速模幂运算+递归求计算1+p+p^2+````+p^nPOJ 1845 Sumdiv求A^B的所有约数之和%9901 */#include<stdio.h>#i ...

  2. 生日蛋糕 POJ - 1190 搜索 数学

    http://poj.org/problem?id=1190 题解:四个剪枝. #define _CRT_SECURE_NO_WARNINGS #include<cstring> #inc ...

  3. POJ 2002 Squares 数学 + 必须hash

    http://poj.org/problem?id=2002 只能说hash比二分快很多.随便一个hash函数都可以完爆二分. 判断是否存在正方形思路如下: 1.枚举任意两个点,作为正方形的一条边,那 ...

  4. poj 1701【数学几何】

    The area Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  5. Prime Distance POJ - 2689 (数学 素数)

    The branch of mathematics called number theory is about properties of numbers. One of the areas that ...

  6. 快速切题 poj 1003 hangover 数学观察 难度:0

    Hangover Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 103896   Accepted: 50542 Descr ...

  7. Humidex POJ - 3299 (数学)

    题目大意 给定你三个变量中的两个输出剩下的那一个 题解 没有什么,就是把公式推出来即可,完全的数学题 代码 #include <iostream> #include <cmath&g ...

  8. I Think I Need a Houseboat POJ - 1005(数学)

    题目大意 在二维坐标内选定一个点,问你当洪水以半圆形扩散且每年扩散50单位,哪一年这个点被被洪水侵蚀? 解法 代码 #include <iostream> #include <cst ...

  9. poj很好很有层次感(转)

    OJ上的一些水题(可用来练手和增加自信) (POJ 3299,POJ 2159,POJ 2739,POJ 1083,POJ 2262,POJ 1503,POJ 3006,POJ 2255,POJ 30 ...

随机推荐

  1. (2)入门指南——(7)添加jquery代码(Adding our jQuery code)

    Our custom code will go in the second, currently empty, JavaScript file which we included from the H ...

  2. 【iOS发展-44】通过案例谈iOS重构:合并、格式化输出、宏观变量、使用数组来存储数据字典,而且使用plist最终的知识

    我们今天的情况下是第一个例子,下面的5一来通过切换页上一页下一页: (1)第一步,基本是以非常傻非常直接的方式来创建.这里用到的主要点有: --把对象变量设置为全局变量使得能够在其它方法中调用来设置它 ...

  3. [Android学习笔记]页面布局

    线性布局:LinearLayout 1.集成ViewGroup,故可容纳多个View 2.线性布局,可设置水平或者垂直方向 相对布局:RelativeLayout

  4. 一个通用onReady函数的实现

    define([], function(){ function onReady(fn) { var DOC = document, html = DOC.documentElement, W3C = ...

  5. 什么是Java “实例化”

    实例化:对象也是引用数据类型,只能使用new运算符从堆中分配内存: 使用已经定义好的类,创建该类对象的过程称为“实例化”. 只有先实例化类的对象,才可以访问到类中的成员(属性和方法). 使用成员运算符 ...

  6. EA强大功能之代码凝视

    前面讲了EA怎样方便我们生成代码,这次讲一下,怎样生成具体的凝视. 1.文件表头凝视 (1)点击工具----选项 在常规项里改动作者: 在代码project中改动代码project的默认语言. (2) ...

  7. POJ 3340 &amp; HDU 2410 Barbara Bennett&#39;s Wild Numbers(数学)

    题目链接: PKU:http://poj.org/problem?id=3340 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2410 Descript ...

  8. VSTO学习笔记(二)Excel对象模型

    原文:VSTO学习笔记(二)Excel对象模型 上一次主要学习了VSTO的发展历史及其历代版本的新特性,概述了VSTO对开发人员的帮助和效率提升.从这次开始,将从VSTO 4.0开始,逐一探讨VSTO ...

  9. filezilla安装

    [alexus@wcmisdlin02 bin]$ ./filezilla ./filezilla: /lib64/libstdc++.so.6: version `CXXABI_1.3.8' not ...

  10. COST CUTTING THE ALAN GREENBERG WAY

    AnatBird.com COST CUTTING THE ALAN GREENBERG WAY