传送门

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. linux上的常见命令掌握

    http://coolshell.cn/articles/8883.html 这篇文章来源于Quroa的一个问答<What are some time-saving tips that ever ...

  2. 026医疗项目-模块二:药品目录的导入导出-导入功能的Service的编写

    这个导入功能要实现的效果是: 思路是: 因为我们最后是在Action层中调用的HxlsRead工具,这个工具传入的就是我们要实现的上一篇文章说到的实现了HxlsOptRowsInterface接口的类 ...

  3. [1]Telerik Extensions for ASP.NET MVC 中文教程(转)

    http://demos.telerik.com/aspnet-mvc/ Telerik Extensions for ASP.NET MVC 是Telerik 公司专门针对Asp.net MVC 开 ...

  4. 【转】【C#】C#重绘windows窗体标题栏和边框

    摘要 windows桌面应用程序都有标准的标题栏和边框,大部分程序也默认使用这些样式,一些对视觉效果要求较高的程序,如QQ, MSN,迅雷等聊天工具的样式则与传统的windows程序大不相同,其中迅雷 ...

  5. max_allowed_packet自动恢复

    https://dev.mysql.com/doc/refman/5.5/en/packet-too-large.html http://blog.chinaunix.net/uid-20304801 ...

  6. android 中打 Log 的一些技巧

    在 android 平台上搞开发工作,会经常用到一些 Log 输出调试信息. 众所周知,android 中有五种类型的 Log , v, d, i, w, e 这里就不再赘 述 (如果对这些不了解的朋 ...

  7. C语言 共用体

    //共用体 union #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #includ ...

  8. C语言 数组之无限循环

    #include<stdio.h> #include<stdlib.h> #include<Windows.h> //定于数组的大小 #define N 10 vo ...

  9. Wifi开发技术总结1

    摘要: 刚刚接触wifi开发的东西,用的模块是 ESP8266-12E. 资料很多,淘宝地址:https://item.taobao.com/item.htm?spm=a1z09.2.9.10.qGL ...

  10. 关于错误提示:此实现不是 Windows 平台 FIPS 验证的加密算法的一部分的解决方案

    vs上的一个项目,弄了一个aes加密算法.提示说:调用的目标异常.此实现不是 Windows 平台 FIPS 验证的加密算法的一部分.弄了好久一直不行.解决不了.最后找了半天资料终于解决了.估计下来. ...