Gym 100685E Epic Fail of a Genie(贪心)

本文提供了一道CodeForces健身房题目E的解决方案。通过分析数值特性,采用排序及比较策略选择最优组合,适用于竞赛编程爱好者和技术人员参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目地址:http://codeforces.com/gym/100685/problem/E

思路:1.对于每个大于1的数,选择。 2.对于负数,从小到大排序,每次选择最小的两个, 若其乘积大于1,选择,否则,退出(负数其绝对值在减小,后面数一定不大于)。3.若此时选择集合为空,则比较最小的两个负数乘积(此时正数中全部小于1,相乘后变小)与整个数列最大的数,选择其中最大的。

#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ft first
#define sd second
#define PII pair<int,int>
using namespace std;
typedef long long LL;
vector<PII> a;
int n,pos,maxx;
vector<int> ans;
int main()
{
    maxx=-1e9;
    scanf("%d",&n);
    for(int i=1; i<=n; i++)
    {
        double x;
        scanf("%lf",&x);
        int tmp=x*100;
        if(tmp>100) ans.push_back(i);
        else
        {
            if(tmp>maxx)
            {
                maxx=tmp;
                pos=i;
            }
            a.push_back(PII(tmp,i));
        }
    }
    sort(a.begin(),a.end());
    for(int i=1; i<a.size()&&a[i].ft<0&&a[i-1].ft<0&&(LL)a[i].ft*a[i-1].ft>10000; i+=2)
    {
            ans.push_back(a[i].sd);
            ans.push_back(a[i-1].sd);
    }
    if(!ans.size())
    {
        if(a[0].ft*a[1].ft>maxx*100)
        {
            ans.push_back(a[0].sd);
            ans.push_back(a[1].sd);
        }
        else ans.push_back(pos);
    }
    sort(ans.begin(),ans.end());
    printf("%d\n",ans.size());
    for(int i=0; i<ans.size()-1; i++)
        printf("%d ",ans[i]);
    printf("%d\n",ans[ans.size()-1]);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值