随机化算法之四

#include<iostream>        //随机化算法  poj 2454
#include<algorithm>
#include
<vector>
using namespace std;
struct node
{
int num;int idx;
bool operator<(const node& o)
{
return num<o.num;
}
}data[
200];
int ans[200];
int main()
{
int k;
cin
>>k;
for(int i=1;i<=3*k;++i)
{
cin
>>data[i].num;
data[i].idx
=i;
}
sort(data
+1,data+3*k+1);
int s=0;
for(int i=k+1;i<=3*k;++i)
s
+=data[i].num;
for(int i=1;i<=k;++i)
cout
<<data[i].idx<<endl;
while(1)
{
int sum=0,c=0,d;
memset(ans,
0,sizeof(ans));
while(c<k)
{
d
=rand()%(2*k)+k+1;
if(ans[d]==1)
continue;
c
++;
ans[d]
=1;
sum
+=data[d].num;
}
if(s-sum>k*500&&sum>k*500)
{
for(int i=k+1;i<=3*k;++i)
if(ans[i]==1)
cout
<<data[i].idx<<endl;
for(int i=k+1;i<=3*k;++i)
if(ans[i]==0)
cout
<<data[i].idx<<endl;
break;
}
}
return 0;
}

  

posted on 2011-08-23 17:07  sysu_mjc  阅读(123)  评论(0编辑  收藏  举报

导航