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

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

分类讨论,正数(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("
")
#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