STL大法好
翻了题解区发现没有人使用 nth_element 故发此题解。
nth_elenment
顾名思义,此函数可以求区间第 小。
函数用法为:nth_element(first,kth,end)。
但使用此函数后会改变数组中元素的位置,所以需要将查询的区间提取出来,再进行查询。
copy
在source数组中选定一段,在target_first的位置放入。
函数用法为 copy(source_first,source_end,target_first)。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 4005;
int n, m;
pair < int, int > a[N], b[N];
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i].first;
a[i].second = i;//second记录在数组中的位置
}
cin >> m;
while (m--) {
int x, y, k;
cin >> x >> y >> k;
copy(a + x, a + y + 1, b + 1);
nth_element(b + 1, b + k, b + (y - x + 1) + 1);
cout << b[k].second << '\n';
}
return 0;
}复杂度:。