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个茶,每个茶的配方是一个字符串,两个字符串之间有一个差值,这个差值为两个字符串每个对应字母之间差的绝对值的最大值,求制作所有茶时获得的所有差值中的最大值. 解法:克鲁斯卡尔.将茶的配方 ...
随机推荐
- S2小测--索引--视图
1. 关于Sql server 视图 视图是一个虚拟表,我们在查询视图的时候,实际上是对基础表的查询.视图不仅可以作为SELECT查询的目标,也可以作为修改语句的目 标.理论上它可以像普通的物理表一 ...
- 13SpringMvc_限定某个业务控制方法,只允许GET或POST请求方式访问
这篇文章要实现的功能是:在一个Action中,有些业务方法只能是post提交上来的才能执行,有些方法是只能get提交上来的才能执行. 比如上篇文章中的UserAction.java(代码如下) pac ...
- 4201 TortoiseSVN常用配置
在Windows下推荐使用乌龟(Tortoise)SVN客户端. TortoiseSVN 是 Subversion 版本控制系统的一个免费开源客户端,可以超越时间的管理文件和目录.文件保存在中央版本库 ...
- WP老杨解迷:开发生态两极化和榜单乱象
Windows Phone 自2013年的一片浪潮推动下,2014年终于开始引起了各大小CP们的注意,于是大量的产品开始乘风破浪一路涌进Windows Phone平台,立即改变了榜单的格局,如今,苦B ...
- 一段后台C#查询SQL Server数据库代码
using System; using System.Data; using System.Collections.Generic; using System.Linq; using System.W ...
- MySQL基础 - 如何系统地学习数据库?
对于数据库的认知,除了大学的时候上过数据库这门课,留下的印象大概就是几条SQL语句一些模棱两可的基本概念,直到工作后面临使用场景才发现数据库的重要性.故归纳总结一下自己的数据库学习之路. 学习资源: ...
- 多个相同jar存在时的引用顺序
起因:今天一个aar包在测试环境中正常运行,使用soapui测试正常返回,在本地环境中运行则老是报数据库连接异常,经检查,是因为在运行时环境中缺少ojdbc相关的jar包引起的. 重新打了一个aar包 ...
- 用postgreSQL做基于地理位置的app(zz)
前言:项目中用到了postgreSQL中的earthdistance()函数功能计算地球上两点之间的距离,中文的资料太少了,我找到了一篇英文的.讲的很好的文章,特此翻译,希望能够帮助到以后用到eart ...
- LeetCode 笔记21 生成第k个排列
题目是这样的: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all ...
- Metasploit_01_信息搜集技术
信息搜集技术 姓名: 谈愈敏 学号: 20135220 日期: 2016.9.7 攻击机:135220-V.BT5, msf 靶 机:135220-V.W2k3_Sploitable 一.实验过程概述 ...