跳到正文

个人记录

P2310 loidc,看看海

Ycyofmine约 1 分钟
原文
文章信息与版本

文章信息

发布
2023年2月6日
收录
2025年12月12日
更新
2026年11月17日
篇幅
约 1 分钟

版本

  1. v1当前

STL大法好

翻了题解区发现没有人使用 nth_element 故发此题解。

nth_elenment

顾名思义,此函数可以求区间第 nn 小。

函数用法为:nth_element(first,kth,end)。

但使用此函数后会改变数组中元素的位置,所以需要将查询的区间提取出来,再进行查询。

copy

在source数组中选定一段,在target_first的位置放入。

函数用法为 copy(source_first,source_end,target_first)。

C++
#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;
}

复杂度:O(nm)O(nm)。

原文