第九届蓝桥杯省赛最后一题 乘积最大

第九届蓝桥杯省赛最后一题

分类讨论,正数(0归到正数集合),负数分别排序。k%2==0负数肯定两两取和正数比较一下哪个大,k%2==1,有正数先取一个正数,转换成k%2==0,否则取负数中最大的k个呗。

不知道能过多少数据,没测过。思路应该是对的。

蓝桥杯题目难度不一定按照顺序来额。所有题都看一遍,先写简单的,当时一直卡前面的题,后面的题看都没看,QAQ。

题目都不是很难,除了那个摔手机的,自闭。

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define huan printf("\n")
#define debug(a,b) cout<<a<<" "<<b<<" "<<endl
#define ffread(a) fastIO::read(a)
using namespace std;
typedef long long ll;
const int maxn = 6e3+10;
const int inf = 0x3f3f3f3f;
const ll mod = 1000000009;
const double epx = 1e-6;
const double pi = acos(-1.0);
//head------------------------------------------------------------------
vector<ll> zheng,fu;
int cmp(ll a,ll b)
{
    return a>b;
}
int main()
{
    int n,k;
    cin>>n>>k;
    for(int i=0;i<n;i++)
    {
        ll x;
        cin>>x;
        if(x>=0)
            zheng.pb(x);
        else if(x<0)
            fu.pb(x);
    }
    sort(all(fu));
    sort(all(zheng),cmp);
    for(auto it:fu)
        cout<<it<<endl;
    for(auto it:zheng)
        cout<<it<<endl;
    ll ans;
    if(k==n)
    {
        ans=1;
        for(auto it:zheng)
            ans=ans*it%mod;
        for(auto it:fu)
            ans=ans*it%mod;
    }
    else if(k==1)
    {
        if(zheng.size()==0)
            ans=*(--fu.end());
        else
            ans=*zheng.begin();
    }
    else if(k%2==0)
    {
        int cnt1=0,cnt2=0;
        ans=1;
        while(k>0)
        {
            int temp1=-inf,temp2=-inf;
            if(cnt1+1<fu.size())
                temp1=fu[cnt1]*fu[cnt1+1]%mod;
            if(cnt2+1<zheng.size())
                temp2=zheng[cnt2]*zheng[cnt2+1]%mod;

            if(temp1>temp2)
                ans=ans*temp1%mod,cnt1+=2;
            else
                ans=ans*temp2%mod,cnt2+=2;
            k-=2;
        }
    }
    else
    {
        int cnt1=0,cnt2=0;
        if(zheng.size()>0)
        {
            ans=zheng[cnt2++]%mod;
            k--;
            while(k>0)
            {
                int temp1=-inf,temp2=-inf;
                if(cnt1+1<fu.size())
                    temp1=fu[cnt1]*fu[cnt1+1]%mod;
                if(cnt2+1<zheng.size())
                    temp2=zheng[cnt2]*zheng[cnt2+1]%mod;
                if(temp1>temp2)
                    ans=ans*temp1%mod,cnt1+=2;
                else
                    ans=ans*temp2%mod,cnt2+=2;
                k-=2;
            }
        }
        else
        {
            ans=1;
            cnt1=fu.size()-1;
            while(k--)
            {
                ans=ans*fu[cnt1--]%mod;
            }
        }
    }
    cout<<ans<<endl;
}

 

转载于:https://www.cnblogs.com/stranger-/p/10476987.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值