poj 2104 K-th Number

自定义快排
#include <iostream>
using namespace std;
struct node
{
int index;
int num;
}list[
100000];
int part(int beg,int end)
{
int low=beg,high=end;
node pivot
=list[beg];
while(low < high)
{
while(low<high && list[high].num>=pivot.num)
--high;
list[low].index
=list[high].index;
list[low].num
=list[high].num;
while(low<high && list[low].num<=pivot.num)
++low;
list[high].index
=list[low].index;
list[high].num
=list[low].num;
}
list[low].index
=pivot.index;
list[low].num
=pivot.num;
return low;
}
void QuickSort(int beg,int end)
{
if(beg<end)
{
int p=part(beg,end);
QuickSort(beg,p
-1);
QuickSort(p
+1,end);
}
}
int main()
{
int n,m,t,i,j,k;
scanf(
"%d%d",&n,&m);
for(t=0;t<n;++t)
{
list[t].index
=t+1;
scanf(
"%d",&list[t].num);
}
QuickSort(
0,n-1);
while(m--)
{
scanf(
"%d%d%d",&i,&j,&k);
for(t=0;t<n;++t)
{
if(list[t].index>=i&&list[t].index<=j)
k
--;
if(k==0)
break;
}
printf(
"%d\n",list[t].num);
}
return 0;
}

  

STL快排
#include <iostream>
#include
<algorithm>
using namespace std;
struct node
{
int index;
int num;
}list[
100000];
int cmp(const void* a,const void* b)
{
return (*(node*)a).num - (*(node*)b).num;
}
int main()
{
int n,m,t,i,j,k;
scanf(
"%d%d",&n,&m);
for(t=0;t<n;++t)
{
list[t].index
=t+1;
scanf(
"%d",&list[t].num);
}
qsort(list,n,
sizeof(list[0]),cmp);
while(m--)
{
scanf(
"%d%d%d",&i,&j,&k);
for(t=0;t<n;++t)
{
if(list[t].index>=i&&list[t].index<=j)
k
--;
if(k==0)break;
}
printf(
"%d\n",list[t].num);
}
return 0;
}

  

posted on 2011-07-20 22:46  sysu_mjc  阅读(157)  评论(0编辑  收藏  举报

导航