第九屆藍橋杯省賽最後一題 乘積最大

第九屆藍橋杯省賽最後一題c++

分類討論,正數(0歸到正數集合),負數分別排序。k%2==0負數確定兩兩取和正數比較一下哪一個大,k%2==1,有正數先取一個正數,轉換成k%2==0,不然取負數中最大的k個唄。spa

不知道能過多少數據,沒測過。思路應該是對的。debug

藍橋杯題目難度不必定按照順序來額。全部題都看一遍,先寫簡單的,當時一直卡前面的題,後面的題看都沒看,QAQ。code

題目都不是很難,除了那個摔手機的,自閉。blog

#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; }
相關文章
相關標籤/搜索