题意:给定 n 个数,然后让从中选取一些数使得它们的总乘积最大。如果有多个,要求这些数尽量少,如果还有多个,随便输出一组即可。

析:一个贪心题,根据乘法的性质,很容易知道,如果一个数大于1,那么肯定要选的,然后如果有两个负数乘积大于1,也要选上,其他的尽量不要选。

最后如果没有这样数,那么就只要计算最小的两个数乘积与最大的比较即可,主要是因为是最小的两个可能是负数,如果不是也不影响结果。

这个题在比赛时,竟然没有AC,。。。主要原因是我没有控制精度,卡了好久,如果控制一下精度就AC了,可以把所有的实数都乘以100,然后再计算。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e4 + 5;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
struct node{
int id;
LL val;
bool operator < (const node &p) const{
return val < p.val;
}
};
node a[maxn];
vector<int> ans; int main(){
while(scanf("%d", &n) == 1){
double x;
for(int i = 0; i < n; ++i){
scanf("%lf", &x);
a[i].val = x * 100.0;
a[i].id = i+1;
}
sort(a, a+n);
ans.clear();
int cnt = 0;
for(int i = 0; i < n; ++i){
if(a[i].val < -100LL){
if(i+1 < n && a[i].val * a[i+1].val > 10000LL){
ans.push_back(a[i].id), ans.push_back(a[i+1].id), ++i;
}
}
else if(a[i].val > 100LL) ans.push_back(a[i].id);
} if(ans.empty()){
LL s = a[0].val * a[1].val;
if(s > a[n-1].val * 100LL) ans.push_back(a[0].id), ans.push_back(a[1].id);
else ans.push_back(a[n-1].id);
}
printf("%d\n", ans.size());
sort(ans.begin(), ans.end());
for(int i = 0; i < ans.size(); ++i){
if(i) putchar(' ');
printf("%d", ans[i]);
}
printf("\n");
}
return 0;
}

CodeForces Gym 100685E Epic Fail of a Genie (贪心,控制精度)的更多相关文章

  1. CF Gym 100685E Epic Fail of a Genie

    传送门 E. Epic Fail of a Genie time limit per test 0.5 seconds memory limit per test 64 megabytes input ...

  2. codeforce Gym 100685E Epic Fail of a Genie(MaximumProduction 贪心)

    题意:给出一堆元素,求一个子集,使子集的乘积最大,如有多个,应该使子集元素个数尽量小. 题解:贪心,如果有大于1的正数,那么是一定要选的,注意负数也可能凑出大于1的正数,那么将绝对值大于1的负数两两配 ...

  3. Codeforces gym 100685 E. Epic Fail of a Genie 贪心

    E. Epic Fail of a GenieTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685 ...

  4. codeforces gym 100286 H - Hell on the Markets (贪心算法)

    题目链接 题意:n个数分别为a[i],问是否存在一组对应的b[i],b[i]=1 || b[i]=-1,使得ai*bi的n项和为0. 题解: 先证明一个结论吧,对于1≤ai≤i+1,前面ai个数一定可 ...

  5. 程序员的Epic Fail [0]

    作为程序员,我们经常会被客户问的一个问题一定是不是说很容易么,为什么花了这么长时间.不得不说,程序员可能是最糟糕的计划者,按时按点按计划完成的软件项目永远是下一个项目.一个项目的延期,有很多这样那样的 ...

  6. Codeforces Gym 101252D&&floyd判圈算法学习笔记

    一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...

  7. Codeforces Gym 101190M Mole Tunnels - 费用流

    题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...

  8. Codeforces Gym 101623A - 动态规划

    题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...

  9. 【Codeforces Gym 100725K】Key Insertion

    Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...

随机推荐

  1. kubernetes滚动更新

    系列目录 简介 当kubernetes集群中的某个服务需要升级时,传统的做法是,先将要更新的服务下线,业务停止后再更新版本和配置,然后重新启动并提供服务.如果业务集群规模较大时,这个工作就变成了一个挑 ...

  2. oracle基础操作(1)

    以前只是简单用程序操作数据,现在才发现自己没有系统的学习过,所以想在这里记录一下自己在数据库方面遇到的问题,会参考查询网络上一些资料,现在看的是韩顺平oracle视频的笔记: 一.关于oracle系统 ...

  3. js json按key值排序

    最近有个需求需要把json按key值进行排序,可是js并没有直接的函数可以对json进行排序的这么办呢? 然后想到了一个间接的方法来实现: 1.将json中的key值取出,存在一个数组中,然后对这个数 ...

  4. MVC3 类型 System.Web.Mvc.ModelClientValidationRule 同时存在

    用文本编辑器打开  工程名称 .csproj 找到 1. <Reference Include="System.Web.WebPages" /> 2. <Refe ...

  5. java基础知识查漏 二

    一.java基本数据类型所占的内存大小 在Java中一共有8种基本数据类型,其中有4种整型,2种浮点类型,1种用于表示Unicode编码的字符 单元的字符类型和1种用于表示真值的boolean类型.( ...

  6. Maximum Memory and CPU Limitations for Linux Server

    grep NR_CPUS /boot/config-`uname -r` [webdev@test apache-flume-1.8.0-bin]$ grep NR_CPUS /boot/config ...

  7. 【智能无线小车系列八】在树莓派上使用USB网卡

    在这个腾“云”驾“物”(云:云计算,物:物联网)的时代,什么都可以没有,就是不能没有网络,树莓派也离不开它.本章节将详细介绍如何将树莓派接入互联网,因为有一些后期将要使用到的小软件需要联网进行下载和安 ...

  8. (转)findViewById 返回为null (自定义控件)

    一.自定义控件 findViewById返回为null 首先讲一个具体的问题,这几天在做demo时,写了一个自定义组合控件,最后在run的时候显示这两行报错.原先还以为是setOnClickListe ...

  9. 配置composer代理

    composer config -g repo.packagist composer https://packagist.phpcomposer.com

  10. kernel中对文件的读写【学习笔记】【原创】

    /*1. 头文件 */ #include <linux/init.h> #include <linux/module.h> #include <linux/modulep ...