传送门

E. Epic Fail of a Genie
time limit per test

0.5 seconds

memory limit per test

64 megabytes

input

standard input

output

standard output

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.

Examples
Input
7
1 3 0 -1 -2 0.5 3
Output
4
2 4 5 7

题目大意:

给出N个实数(绝对值不超过10^6,小数部分最多两位),从中选出若干个数使它们的乘积最大,按升序输出所选数的下标(1-based index)。如果有多种方案输出所选的数的数目最少的方案,如仍有多种方案任意输出一组。

Solution:

容易看出选数步骤/策略:

为描述方便,记所选数的(可重)集合为S,初始S为空, N个数的(可重)集合为A。

(1)大于1的全选

(2)对于负数,从小到大排序,每次考虑头两个(最小的两个),若它们的乘积大于1则选出,否则停止

(3)若S为空,在取两个负数(若可能)与取最大数之间择最优者; 否则结束。


总结:

这道题我WA了8发。原因是:

(1) 对于负数的选取策略(步骤(2))我想错了

(2) 我把第三步细分了好几种情况,写得比较繁,bug百出。

另外,这道题还要注意精度,可以将输入的数乘100转成int来避免。


Implementation:

#include <bits/stdc++.h>
using namespace std; const int N(1e4+);
typedef long long LL;
vector<pair<int,int>> a;
vector<int> ans; int main(){
int n, y, ma=INT_MIN, p;
double x;
cin>>n;
for(int i=; i<=n; i++){
cin>>x;
y=*x;
if(y>) ans.push_back(i);
else{
a.push_back({y, i});
if(y>ma) p=i, ma=y;
}
}
sort(a.begin(), a.end());
for(int i=; i<a.size() && a[i].first< && a[i-].first< && (LL)a[i].first*a[i-].first>; i+=)
ans.push_back(a[i-].second), ans.push_back(a[i].second);
if(!ans.size())
if(a[].first*a[].first>ma*) //error-prone
ans.push_back(a[].second), ans.push_back(a[].second);
else ans.push_back(p);
cout<<ans.size()<<endl;
sort(ans.begin(), ans.end());
for(int i=; i<ans.size(); i++){
if(i) putchar(' ');
cout<<ans[i];
}
puts("");
return ;
}

CF Gym 100685E Epic Fail of a Genie的更多相关文章

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

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

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

    题意:给定 n 个数,然后让从中选取一些数使得它们的总乘积最大.如果有多个,要求这些数尽量少,如果还有多个,随便输出一组即可. 析:一个贪心题,根据乘法的性质,很容易知道,如果一个数大于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. 程序员的Epic Fail [0]

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

  5. CF Gym 102028G Shortest Paths on Random Forests

    CF Gym 102028G Shortest Paths on Random Forests 抄题解×1 蒯板子真jir舒服. 构造生成函数,\(F(n)\)表示\(n\)个点的森林数量(本题都用E ...

  6. CF gym 101933 K King's Colors —— 二项式反演

    题目:http://codeforces.com/gym/101933/problem/K 其实每个点的颜色只要和父亲不一样即可: 所以至多 i 种颜色就是 \( i * (i-1)^{n-1} \) ...

  7. cf Gym 101086M ACPC Headquarters : AASTMT (Stairway to Heaven)

    题目: Description standard input/output As most of you know, the Arab Academy for Science and Technolo ...

  8. CF Gym 100685A Ariel

    传送门 A. Ariel time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  9. CF GYM 100703A Tea-drinking

    题意:龙要制作n个茶,每个茶的配方是一个字符串,两个字符串之间有一个差值,这个差值为两个字符串每个对应字母之间差的绝对值的最大值,求制作所有茶时获得的所有差值中的最大值. 解法:克鲁斯卡尔.将茶的配方 ...

随机推荐

  1. Android 动态加载 (一) 态加载机制 案例一

    在目前的软硬件环境下,Native App与Web App在用户体验上有着明显的优势,但在实际项目中有些会因为业务的频繁变更而频繁的升级客户端,造成较差的用户体验,而这也恰恰是Web App的优势.本 ...

  2. Android SQLite (四 ) 全面详解(二)

    SQLite创建数据库 创建数据库语法: sqlite3 DatabaseName.db 如下展示一个实例: SQLite附加数据库 假设这样一种情况,当在同一时间有多个数据库可用,您想使用其中的任何 ...

  3. localStorage实现购物车数量单价和总价实时同步(二)

    利用localStorage实时显示购物车小计和总价页面显示: 和昨天的原理相同,本地存储同时实时循环计算总价之和,注意循环时候的先清空再计算 Success is getting what you ...

  4. Jython概要

    1.安装jython 1.1 进入http://www.jython.org/downloads.html ,网页上会显示当前最稳定的版本(The most current stable releas ...

  5. js中的return,return true,return false小结

    return  函数执行到这句时会终结,并返回调用函数,而且把表达式的值作为函数的结果返回 return false 可以防止默认的事件行为.例如,默认情况下点击一个<a>元素,页面会跳转 ...

  6. fanghuangscannerV3 字典生成器

    #-*-coding=GB2312-*- import random import sys def makedict(name): f1 =open(name+'_user.txt','r') f2 ...

  7. Discuz X3核心文件解析

    <?php /** *      [Discuz!] (C)2001-2099 Comsenz Inc. *      This is NOT a freeware, use is subjec ...

  8. 深入了解view以及自定义控件

    参考文章: http://blog.csdn.net/guolin_blog/article/details/12921889 Android LayoutInflater原理分析,带你一步步深入了解 ...

  9. swift第二季高级语法

    一.类和结构体 二.属性 三.方法 四.下标 五.继承和扩展 六.初始化和反初始化

  10. IOS开发之——reveal 的使用

    Reveal是一个iOS程序界面调试工具.使用Reveal,我们可以在iOS开发时动态地查看和修改应用程序的界面. 对于动态或复杂的交互界面,手写UI是不可避免的.通过Reveal,我们可以方便地调试 ...