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

E. Epic Fail of a Genie
Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/gym/100685/problem/E

Description

Aladdin had found a new shiny lamp and has started polishing it with his hands. Suddenly a mysterious genie appeared from within and offered Aladdin to fulfill any of his three wishes. Genie had a very subtle humor that made Aladdin very sceptical about him. Aladdin didn‘t believe that genie was so powerful that could do anything he had wished and asked him to become a mouse. The genie did that without hesitation. Then Aladdin asked genie to become a mouse pad. Genie didn‘t like this kind of wish but had to submit. Finally Aladdin tested genie‘s abilities in math: he had to choose a nonempty subset giving the maximum product from the given set of numbers. Genie was shocked. Math was his Achilles‘ heel, however he was able to contact anyone on earth to help him. You are a secret weapon of the genie — help him solve the test and avoid this epic fail. This is the last chance for the genie: he‘ll be forever jailed in the lamp if his new master doesn‘t trust him.

Input

The first line of input contains an integer N (2 ≤ N ≤ 104) — the cardinality of a set of numbers.

The second line of input contains N floating-point numbers with absolute value not more than 106. The fractional part of each number does not contain more than two digits.

Output

The first line of the output should contain a single integer M — the total number of numbers that genie should choose from the set.

The second line of output should contain 1-based indexes of these numbers. Indexes must be sorted in ascending order. If multiple solutions exist please output the one with the minimal subset cardinality. If there are still several suitable solutions output any of them.

Sample Input

7
1 3 0 -1 -2 0.5 3

Sample Output

4
2 4 5 7

HINT

题意

给你一个集合,让你选择出一个非空子集,使得乘积最大

题解

1.大于1的正数必选

2.乘起来大于1的负数对也要选择

如果都没有

那么选择俩乘起来大的负数,或者一个较大的正数

虽然感觉会卡eps……

但是并没有?

代码

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
#define maxn 20001
#define mod 1000000007
#define eps 1e-9
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}
//**************************************************************************************

vector<int> Q;
struct node
{
    int x,y;
};
struct point
{
    double x;
    int y;
};
bool cmp(point a,point b)
{
    return a.x<b.x;
}
double a[maxn];
vector<point> T;
int main()
{
    node tmp;
    tmp.x=0,tmp.y=0;
    int n=read();
    for(int i=1;i<=n;i++)
        scanf("%lf",&a[i]);
    int flag=1;

    for(int i=1;i<=n;i++)
    {
        if(fabs(a[i])>1&&a[i]>0)
        {
            Q.push_back(i);
            flag=0;
        }
    }

    for(int i=1;i<=n;i++)
    {
        if(a[i]<0)
        {
            point kiss;
            kiss.x=a[i];
            kiss.y=i;
            T.push_back(kiss);
        }
    }
    if(T.size()!=0)
    {

        sort(T.begin(),T.end(),cmp);
        for(int i=0;i<T.size()-1;i++)
        {
            if(T[i].x*T[i+1].x>1)
            {
                Q.push_back(T[i].y);
                Q.push_back(T[i+1].y);
                i++;
                flag = 0;
            }
        }
    }
    a[0]=0;
    if(flag)
    {
        int max1=0,max2=0;
        int max3=0;
        for(int i=n;i>=1;i--)
        {
            if(a[i]<0)
            {
                if(fabs(a[i])>=fabs(a[max1]))
                {
                    max2=max1;
                    max1=i;
                }
                else if(fabs(a[i])>=fabs(a[max2]))
                {
                    max2=i;
                }
            }
            else
            {
                if(fabs(a[i])>=fabs(a[max3]))
                {
                    max3=i;
                }
            }
        }

        if(max3==0)
        {
            if(max2==0)
                Q.push_back(max1);
            else
                Q.push_back(max1),Q.push_back(max2);
        }
        else
        {
            if(max2==0)
                Q.push_back(max3);
            else
            {
                double tmp1=a[max3],tmp2=a[max1]*a[max2];
                if(tmp1-tmp2>-eps)
                    Q.push_back(max3);
                else
                    Q.push_back(max1),Q.push_back(max2);
            }
        }

    }
    sort(Q.begin(),Q.end());
    printf("%d\n",Q.size());
    for(int i=0;i<Q.size();i++)
        printf("%d ",Q[i]);
    printf("\n");
}
时间: 2024-08-02 14:36:30

Codeforces gym 100685 E. Epic Fail of a Genie 贪心的相关文章

CodeForces Gym 100685E Epic Fail of a Genie (贪心,控制精度)

题意:给定 n 个数,然后让从中选取一些数使得它们的总乘积最大.如果有多个,要求这些数尽量少,如果还有多个,随便输出一组即可. 析:一个贪心题,根据乘法的性质,很容易知道,如果一个数大于1,那么肯定要选的,然后如果有两个负数乘积大于1,也要选上,其他的尽量不要选. 最后如果没有这样数,那么就只要计算最小的两个数乘积与最大的比较即可,主要是因为是最小的两个可能是负数,如果不是也不影响结果. 这个题在比赛时,竟然没有AC,...主要原因是我没有控制精度,卡了好久,如果控制一下精度就AC了,可以把所有

Codeforces gym 100685 F. Flood bfs

F. FloodTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/F Description We all know that King Triton doesn't like us and therefore shipwrecks, hurricanes and tsunami do happen. But being bored with the same routine

Codeforces gym 100685 C. Cinderella 水题

C. CinderellaTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/C Description Cinderella is given a task by her Stepmother before she is allowed to go to the Ball. There are N (1 ≤ N ≤ 1000) bottles with water in th

Codeforces gym 100685 A. Ariel 暴力

A. ArielTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/A Description King Triton really likes watching sport competitions on TV. But much more Triton likes watching live competitions. So Triton decides to set up

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

题意:给出一堆元素,求一个子集,使子集的乘积最大,如有多个,应该使子集元素个数尽量小. 题解:贪心,如果有乘积大于1的正数,那么是一定要选的,注意负数也可能凑出大于1的正数,那么将绝对值大于1的负数两两配对,如果还剩下一个,那么在判断一下,那个负数和比它小的最小负数的乘积是否大于1,如果是那么就选这两个.把所有可能凑成大于1的数选完以后,剩下的数一定比1小,那么就不选. 如果无法凑出大于1的数,那么再分类讨论一下. 挺容易写错... #include<bits/stdc++.h> using

Codeforces gym Hello 2015 Div1 B and Div2 D

Codeforces gym 100571 problem D Problem 给一个有向图G<V,E>和源点S,边的属性有长度L和颜色C,即E=<L,C>.进行Q次询问,每次给定一个点X,输出S到X的最短路的长度(不存在则输出 -1).但要求S到X的路径中相邻两条边颜色不一样. Limits Time Limit(ms): 1000 Memory Limit(MB): 256 |V|, |E|: [1, 10^5] X, S: [1, |V| ] L: [1, 10^9] |C|

Codeforces gym Hello 2015 Div1 E

Codeforces gym 100570 problem E (一种处理动态最长回文子串问题的方法) Problem 给一个长度为N的字符串S,字符集是'a'-'z'.进行Q次操作,操作分三种.一,修改位置X的字符为C:二,查询以P位置为中心的最长回文子串的长度,并输出:三,查询以P与P+1的中间位置为中心的最长回文子串的长度,并输出. More 第二种操作子串长度为奇数,一定存在:第三种操作子串长度为偶数,若不存在,输出 -1. Limits Time Limit(ms): 4000(1s足

Codeforces gym Hello 2015 Div1 C and Div2 E

Codeforces gym 100570 problem C Codeforces gym 100571 problem E Problem 给一个N行M列的矩阵Ma,进行Q次(Q<=10)查询,每次给定一个K,问有多少子矩阵,满足最大值max - 最小值min <=K. Limits Time Limit(ms): 8000 Memory Limit(MB): 512 N, M: [1, 400] Q: [1, 10] Ma(i, j), K: [1, 10^9] Solution (Th

程序员的Epic Fail [0]

作为程序员,我们经常会被客户问的一个问题一定是不是说很容易么,为什么花了这么长时间.不得不说,程序员可能是最糟糕的计划者,按时按点按计划完成的软件项目永远是下一个项目.一个项目的延期,有很多这样那样的原因,其中不得不说的一个原因就是很多代码想起来很容易,但是真的写起来,细节里全是魔鬼.在这个Epic Fail的系列中,我会记录一些在我平常写代码的过程中遇到的那些本来很简单却花了很长时间的有趣问题.这个系列会不定时更新,不断记录我写代码中的糗事,希望我永远不用更新:). 作为开篇,记录一个花了我1