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 ...
随机推荐
- 无边无状态栏窗口(使用GetWindowLongPtr设置GWL_EXSTYLE)
通过SetWindowLongPtr来设置窗口样式 var NewStyle: Integer; begin Application.Initialize; Application.MainFormO ...
- OCA读书笔记(3) - 使用DBCA创建Oracle数据库
Objectives: •Create a database by using the Database Configuration Assistant (DBCA) •Generate databa ...
- MySQL 採用Xtrabackup对数据库进行全库备份
1,xtrabackup简单介绍 关于数据库备份以及备份工具.參考:http://blog.itpub.net/26230597/viewspace-1460065/,这里来介绍xtrabackup已 ...
- poj 3623 Best Cow Line, Gold
题目不算难,但是不认真想的话很容易wa,我就是wa了多次才意识到自己想法存在的缺陷. 相同的时候往后找知道出现不相同时,只能判断出当前字符的优先顺序. 这个题目如果朴素的按照这种方法做的话复杂度其实是 ...
- 第十七篇:实例分析(1)--初探WDDM驱动学习笔记(八)
第四篇(VidPN)中提到过MIRROR驱动. 在进入本篇的实际内容前, 带着好奇心, 想请教CSDN中的显卡驱动方面的大虾, 怎样才干把这个驱动玩起来, 这个驱动的作用是什么,等等, 敬请不吝赐教. ...
- 小米手机usb共享网络mac
今天.我想去FQgoogle,mac在墙上,不便于使用.甚至只是用Android手机wifi,打开墙软件.然后usb分享到mac.然后mac互联网. 在谈到一些复杂.其实,关键一,小米手机usb共享, ...
- innerHTML与appendChild(newnodeText)的区别
innerHTML和createTextNode都可以把一段内容添加到一个节点中,区别是如果这段内容中有html标签时表现就不同了,在createTextNode中会当作文本处理,不会被浏览器解析,但 ...
- Linux多线程服务端编程:使用muduo C++网络库
内容推荐本 书主要讲述采用现代C++在x86-64 Linux上编写多线程TCP网络服务程序的主流常规技术,重点讲解一种适应性较强的多线程服务器的编程模型,即one loop per thread.这 ...
- JAVA编程心得-JAVA实现CRC-CCITT(XMODEM)算法
CRC即循环冗余校验码(Cyclic Redundancy Check):是数据通信领域中最常用的一种差错校验码,其特征是信息字段和校验字段的长度可以任意选定. 1 byte checksum CRC ...
- c++实用技巧
原地址:http://www.cnblogs.com/easymind223/articles/2576904.html 晚上的时间总是习惯性的在cnblogs逛街,今天又看到了好文章,其c++味道浓 ...