http://poj.org/problem?id=2104
题意:长度为n的int型数组,进行m次询问,求出从bg到ed第k小的数是什么
只用排序,勉强可以卡时间过。
思路:保存数组中每个点的位置id后,对整个数组排序。每次询问都遍历整个数组,当遍历到满足区间bg到ed的点时,就表示该点是该区间中没有计算过的最小的点
代码:
#include <iostream>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
struct node
{
int value,id;
bool operator < (const node a)const
{
return value < a.value;
}
}a[100005];
int main()
{
//freopen("ub.txt","r",stdin);
int n,m,bg,ed,k;
scanf("%d%d",&n,&m);
for(int i = 0; i < n; i ++)
{
scanf("%d",&a[i].value);
a[i].id = i;
}
sort(a,a + n);
for(int i = 0; i < m; i ++)
{
scanf("%d%d%d",&bg,&ed,&k);
bg = bg - 1;
ed = ed - 1;
for(int j = 0; j < n; j ++)
{
if(a[j].id >= bg && a[j].id <= ed)
{
k --;
}
if(k == 0)
{
printf("%d\n",a[j].value);
break;
}
}
}
return 0;
}