poj 3399 Product(数学)
主题链接:http://poj.org/problem?id=3399
| 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 N + 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
思路:每次寻找最小的两个负数和最大的两个正数的乘积中较大的。
代码例如以下:
#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(数学)的更多相关文章
- poj 1845 POJ 1845 Sumdiv 数学模板
筛选法+求一个整数的分解+快速模幂运算+递归求计算1+p+p^2+````+p^nPOJ 1845 Sumdiv求A^B的所有约数之和%9901 */#include<stdio.h>#i ...
- 生日蛋糕 POJ - 1190 搜索 数学
http://poj.org/problem?id=1190 题解:四个剪枝. #define _CRT_SECURE_NO_WARNINGS #include<cstring> #inc ...
- POJ 2002 Squares 数学 + 必须hash
http://poj.org/problem?id=2002 只能说hash比二分快很多.随便一个hash函数都可以完爆二分. 判断是否存在正方形思路如下: 1.枚举任意两个点,作为正方形的一条边,那 ...
- poj 1701【数学几何】
The area Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- Prime Distance POJ - 2689 (数学 素数)
The branch of mathematics called number theory is about properties of numbers. One of the areas that ...
- 快速切题 poj 1003 hangover 数学观察 难度:0
Hangover Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 103896 Accepted: 50542 Descr ...
- Humidex POJ - 3299 (数学)
题目大意 给定你三个变量中的两个输出剩下的那一个 题解 没有什么,就是把公式推出来即可,完全的数学题 代码 #include <iostream> #include <cmath&g ...
- I Think I Need a Houseboat POJ - 1005(数学)
题目大意 在二维坐标内选定一个点,问你当洪水以半圆形扩散且每年扩散50单位,哪一年这个点被被洪水侵蚀? 解法 代码 #include <iostream> #include <cst ...
- poj很好很有层次感(转)
OJ上的一些水题(可用来练手和增加自信) (POJ 3299,POJ 2159,POJ 2739,POJ 1083,POJ 2262,POJ 1503,POJ 3006,POJ 2255,POJ 30 ...
随机推荐
- 特殊的Windows消息
WM_CREATE消息 该消息是Windows发送给视图的第一个消息.由于当应用程序框架调用Create函数时该消息就会被发送,而此时窗口创建还未完成,窗口还不可见,因此在控制函数OnCreate内部 ...
- Android菜鸟的成长笔记(10)——使用Bundle在Activity之间传值
原文:[置顶] Android菜鸟的成长笔记(10)——使用Bundle在Activity之间传值 前面我们了解了如何启动一个Activity,一个Activity在启动另外一个Activity的时候 ...
- 分析javascript关闭
1.什么是闭包 1)官方解释 一个拥有多个变量和绑定了这些变量的环境的表达式(一般是一个函数).因而这些变量也是该表达式的一部分. 我的理解:所谓的闭包就是连接函数内部和函数外部的一座桥梁.使得在外部 ...
- centos 安装 redis3.2.0 集群
这里创建6个redis节点,其中三个为主节点,三个为从节点. redis和端口对应关系: 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 从: 127.0.0 ...
- 彻底卸载McAfee和Agent的方法
1.控制面板中-添加或删除程序中-删除"McAfee VirusScan Enterprise"和"McAfee AntiSpyware Enterprise Modul ...
- linux 下opensplice的简易安装
http://www.prismtech.com/opensplice/opensplice-dds-community/software-downloads 下载对应我选择的是: OpenSplic ...
- UITableView性能优化及手工绘制UITableViewCell
提高表视图的性能 UITableView作为应用中最常用的视图,它的性能优化问题几乎是经常提及.下面对在非网络访问情况下的表视图性能优化进行了主要的几点说明: 1.自定义类或XIB文件时 在系统提供的 ...
- ThinkPHP多应用/项目配置技巧(使用同一配置文件)--(十六)
原文:ThinkPHP多应用/项目配置技巧(使用同一配置文件)--(十六) ThinkPHP多应用配置技巧(没有使用分组,这是通过入口文件产生的Home.Admin)----很实用! 比如:现在有Ho ...
- H2O是开源基于大数据的机器学习库包
H2O是开源基于大数据的机器学习库包 H2O能够让Hadoop做数学,H2O是基于大数据的 统计分析 机器学习和数学库包,让用户基于核心的数学积木搭建应用块代码,采取类似R语言 Excel或JSON等 ...
- Linux源文件夹结构呈现
1.arch文件夹: - 相关的存储在不同平台上的代码,每个平台都采用了不同的文件夹来区分. ******Alpha平台 ******Arm平台 ******Arv32平台 ******X86平台 2 ...