CF Gym 100685E Epic Fail of a Genie
0.5 seconds
64 megabytes
standard input
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.
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.
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.
7
1 3 0 -1 -2 0.5 3
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的更多相关文章
- codeforce Gym 100685E Epic Fail of a Genie(MaximumProduction 贪心)
题意:给出一堆元素,求一个子集,使子集的乘积最大,如有多个,应该使子集元素个数尽量小. 题解:贪心,如果有大于1的正数,那么是一定要选的,注意负数也可能凑出大于1的正数,那么将绝对值大于1的负数两两配 ...
- CodeForces Gym 100685E Epic Fail of a Genie (贪心,控制精度)
题意:给定 n 个数,然后让从中选取一些数使得它们的总乘积最大.如果有多个,要求这些数尽量少,如果还有多个,随便输出一组即可. 析:一个贪心题,根据乘法的性质,很容易知道,如果一个数大于1,那么肯定要 ...
- 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 ...
- 程序员的Epic Fail [0]
作为程序员,我们经常会被客户问的一个问题一定是不是说很容易么,为什么花了这么长时间.不得不说,程序员可能是最糟糕的计划者,按时按点按计划完成的软件项目永远是下一个项目.一个项目的延期,有很多这样那样的 ...
- CF Gym 102028G Shortest Paths on Random Forests
CF Gym 102028G Shortest Paths on Random Forests 抄题解×1 蒯板子真jir舒服. 构造生成函数,\(F(n)\)表示\(n\)个点的森林数量(本题都用E ...
- CF gym 101933 K King's Colors —— 二项式反演
题目:http://codeforces.com/gym/101933/problem/K 其实每个点的颜色只要和父亲不一样即可: 所以至多 i 种颜色就是 \( i * (i-1)^{n-1} \) ...
- 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 ...
- CF Gym 100685A Ariel
传送门 A. Ariel time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- CF GYM 100703A Tea-drinking
题意:龙要制作n个茶,每个茶的配方是一个字符串,两个字符串之间有一个差值,这个差值为两个字符串每个对应字母之间差的绝对值的最大值,求制作所有茶时获得的所有差值中的最大值. 解法:克鲁斯卡尔.将茶的配方 ...
随机推荐
- [转] Linux下防火墙iptables用法规则详及其防火墙配置
from: http://www.cnblogs.com/yi-meng/p/3213925.html 备注: 排版还不错,建议看以上的链接. iptables规则 规则--顾名思义就是规矩和原则,和 ...
- ralitive absolute
3.relative与absolute的主要区别: 首先,是上面已经提到过的在正常流中的位置存在与否. 其次,relative定位的层总是相对于其最近的父元素,无论其父元素是何种定位方式.如图3: 图 ...
- 004医疗项目-逆向工程-pojo类的创建和对应xml的生成
我们使用mybatis的逆向工程来生成pojo类,省去很多不必要的工作. 我把逆向工程需要的项目如下:
- Web知识总结
一)window.location.href和window.location.replace的区别 1.window.location.href=“url”:改变url地址: 2.window.loc ...
- sqlalchemy 的 ORM 与 Core 混合方式使用示例
知乎: sqlalchemy 的 ORM 与 Core 混合方式操作数据库是一种怎样的体验? 答: 酸! 本文基于:win 10 + python 3.4 + sqlalchemy 1.0.13 基本 ...
- Code First开发系列之管理并发和事务(转)
转自:http://www.cnblogs.com/farb/p/ConcurrencyAndTransctionManagement.html 返回<8天掌握EF的Code First开发&g ...
- Android 获取手机Mac地址,手机名称
/** * 获取手机mac地址<br/> * 错误返回12个0 */ public static String getMacAddress(Context context) { // 获取 ...
- 从0开始学Java——eclipse下运行servlet程序警告:Setting property 'source' to 'org.eclipse.jst.jee.server:类名' did not find a matching property.
在使用Eclipse 创建第一个 Servlet之后,并且配置好了tomcat,然后Run on server的之后,提示标题所示错误: 警告: [SetContextPropertiesRule]{ ...
- 实验二 Linux下C语言编程基础
1. 熟悉Linux系统下的开发环境 2. 熟悉vi的基本操作 3. 熟悉gcc编译器的基本原理 4. 熟练使用gcc编译器的常用选项 5 .熟练使用gdb调试技术 6. 熟悉makefile基本原理 ...
- sql server 2008 登录 4064 错误解决办法
出现这个错误是账户无法打开默认数据库导致的 修改一下该账户的默认打开数据库即可. 如果很不幸,你的sa帐号和windows身份验证默认都打开同一个数据库,那么无论换哪种方式登录都是一样没用的 如果你有 ...