poj 3399 Product

#include<iostream>
#include
<algorithm>
#include
<functional>
using namespace std;
__int64 data[
105],pos[105],neg[105],len1,len2,count0;
__int64 n,k,i,ans[
2][105];
void printout(__int64 a,__int64 b,__int64 c,bool tag)
{
for(i=len1-1;i>len1-1-a;--i)
cout
<<pos[i]<<" ";
while(b--)
cout
<<"0 ";
if(tag==0)
for(i=0;i<c;++i)
cout
<<neg[i]<<" ";
else
for(i=len2-c;i<len2;++i)
cout
<<neg[i]<<" ";
cout
<<endl;
}
int main()
{
cin
>>n>>k;
for(i=0;i<n;++i)
{
cin
>>data[i];
if(data[i]==0)
count0
++;
else if(data[i]>0)
pos[len1
++]=data[i];
else
neg[len2
++]=data[i];
}
sort(pos,pos
+len1);
sort(neg,neg
+len2,greater<__int64>());
if( len1+len2<k || ( len1+len2==k && len2%2==1 ) || ( len1+len2>k && len1==0 && k%2==1 ) ) //考虑最大乘积不大于0的情况
{
__int64 t
=min(k-len1,count0);
printout(len1,t,k
-len1-t,0);
}
else
{
__int64 a
=0,c=0;
if(k%2==1)
a
++,k--;
while(k>0)
{
if( len2-c-2<0 || (len1-a-2>0 && pos[len1-a-1]*pos[len1-a-2] > neg[len2-c-1]*neg[len2-c-2] ) )
a
+=2;
else
c
+=2;
k
-=2;
}
printout(a,
0,c,1);
}
return 0;
}
//注意到当len1+len2>k 时,只要正数的个数不为0,那么乘积一定可以为正

  

posted on 2011-07-22 16:50  sysu_mjc  阅读(187)  评论(0编辑  收藏  举报

导航