传送门

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. 转载 ---> UITableViewCell的分割线

    在iOS7中,表格中经常看到的一个情况是如下所示, 解决方法: 1,手写代码控制   1 self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, ...

  2. Idea maven tomcat 配置热更新 以及 maven jar依赖

    看了视频 实在忍不住上了idea的贼船 不过这玩意确实有点坑爹,因为用的人少,所以很多配置是有问题的 例如maven配置tomcat热更新 以及tomcat的maven配置 我这里放几张图作为备用 配 ...

  3. ArcGIS Engine 中 Geometric Network 显示流向代码

    原文地址:http://hi.baidu.com/steeeeps/item/165fbc15475e94741009b5b3 非常感谢作者. 以前学习几何网络时,对效用网络流向进行了总结,原理与效果 ...

  4. 一些正则验证-JS

    Validation = { textCount: function(field, counter, maxLimit) { var message = $(field).val(); if ($(f ...

  5. 【Mysql】 my.ini配置一例

    # For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.6/en/server-co ...

  6. 开源(免费)三维 GIS(地形,游戏)

    先写想法: 想做个简单的地形漫游,于是考虑在ww直接开发,或找个控件形式的开发组件. 最大的期望有: 1. 支持google的sketchup,快速智能三维建模 2. 设计模式做好点,最好先做成组件形 ...

  7. C语言学习的记忆

    优于他人的技能 会玩双截棍: 我的经验就是Practice make perfect,熟能生巧:还有就是坚持不懈. 关于C语言的学习的回忆 1.我通过老师的教导和课外C语言书籍中学习,和我的技能相比, ...

  8. ios —— UIViewAdditions 布局坐标类库

    方便大家计算视图的高度,宽度,上下左右坐标,简化代码操作,更加直观 下载地址:http://download.csdn.net/detail/humingtao2013/7511657

  9. 第四章 Js的面向对象的初窥视(天生的哈希表)

    对象就有键值对 var speaker = { text: "Hello World", say: function(){ console.log("Hello Worl ...

  10. JavaScript基础1

    JavaScript写在<script></script>之间   <script type="text/javascript">表示在< ...