主题链接: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. Windows环境下访问NFS(33篇Storage的文章)

    Windows环境下访问NFS 使用Solaris时,如果想在两台Solaris之间共享数据,那么你想到的最省事.最方便的方法肯定是nfs.但是现在的学生们的桌面,估计99%以上都是Windows,W ...

  2. 关于jquery文件上传插件 uploadify 3.1的使用

    要使用uplaodify3.1,自然要下载相应的包,下载地址http://www.uploadify.com/download/,这里有两种包,一个是基于flash,免费的,一个是基于html5,需要 ...

  3. 嵌入式linux多进程编程

    嵌入式linux多进程编程 在主程序显示文本菜单.提供例如以下服务.要求每一个服务都通过生成子进程来提供. 服务包含:日历信息显示,日期信息显示,推断闰年服务,文件复制功能,数字排序功能.退出功能. ...

  4. Contact类解析

    Contact类 public static class Contacts implements BaseColumns, ContactsColumns, ContactOptionsColumns ...

  5. go之匿名字段

    struct,定义的时候是字段名与其类型一一对应,实际上Go支持只提供类型,而不写字段名的方式,也就是匿名字段,也称为嵌入字段. 当匿名字段是一个struct的时候,那么这个struct所拥有的全部字 ...

  6. Android studio导入第三方类库

    1.开发过程中想要导入第三方类库和Eclipse也是有差别的,我们导入SlidingMenu这个类库,从github上下载下来解压到项目目录下. 2.然后我们重启我们的android studio就会 ...

  7. EasyUI初体验--右键弹框

    在C/S中可能非常easy实现右键弹框,但在B/S中直到今天我才搞定,小小得瑟一下.只一个html页面,导入相关的Easy-UI类库就能搞定,Easy-UI类库下载地址 <!DOCTYPE ht ...

  8. 让自己的软件实现拖拽打开文件(使用WM_DROPFILES消息和DragQueryFile函数)

    //声明 protected procedure WMDROPFILES(var Msg : TMessage); message WM_DROPFILES; //实现 procedure TForm ...

  9. java多线程12设计模式

    1.Single Threaded Execution Pattern(单线程运行模式) 2.Immutable Pattern(一成不变的模式) 3.Guarded Suspension Patte ...

  10. A亚马逊WS网上系列讲座——怎么样AWS云平台上千万用户的应用建设

    用户选择云计算平台构建应用程序的一个重要原因是高弹性的云平台和可扩展性. 面向Internet应用程序通常需要支持用户使用大量,但要建立一个高度可扩展.具有一定的挑战,高度可用的应用程序,只有立足AW ...