使用方法:nth_element(start, start+n, end)

使第n大元素处于第n位置(从0开始,其位置是下标为n的元素),并且比这个元素小的元素都排在这个元素之前,比这个元素大的元素都排在这个元素之后,但不能保证他们是有序的。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N=;
char s[N];
int main()
{
int len,n;
gets(s);
len=strlen(s);
scanf("%d",&n);
nth_element(s,s+n,s+len);
puts(s);
return ;
}

nth_element函数原型有四个,详细我就不一一累赘了,我们就用最普通的用法寻找第k位置的元素。

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

first,last 第一个和最后一个迭代器,也可以直接用数组的位置。 
kth,要定位的第k个元素,能对它进行随机访问.

将第k_th元素放到它该放的位置上,左边元素都小于等于它,右边元素都大于等于它.

例如:

vector<int> a(9);
for(int i = 0; i < 9; i++)
a[i] = i+1;
random_shuffle(a.begin(),a.end());
for(int i = 0; i < 9; i++)
cout << a[i] << " ";
cout << endl;

nth_element(a.begin(),a.begin()+4,a.end());
cout << *(a.begin()+4) << endl;

for(int i = 0; i < 9; i++)
cout << a[i] << " ";

cout << endl;

结果为:

可以发现函数只是把kth的元素放在了正确位置,对其他元素并没有排序,所以可以利用这个函数快速定位第k个元素,当然,这个函数也支持你直接写比较函数,此处不再累赘因为ACM中基本不会用到。

那么为什么要选择这个函数呢,这就和复杂度有关了,nth_element的空间复杂度为O(1),在数据量大的时候时间复杂度为O(n),数据少的情况最坏为O(n^2),因为函数原理是随机访问,但实际运用过程中,基本都会是O(n)的时间复杂度。所以说是非常迅速的。

https://ac.nowcoder.com/acm/contest/327/A

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int t,n,k;
struct point
{
ll x,y;
} a[]; int main()
{
scanf("%d",&t);
while (t--)
{
scanf("%d%d",&n,&k);
for (int i=; i<=n; i++)
{
scanf("%lld%lld",&a[i].x,&a[i].y);
}
vector<ll> v;
for (int i=; i<=n; i++)
{
for (int j=i+; j<=n; j++)
{
for (int w=j+; w<=n; w++)
{
ll area=abs((a[j].x-a[i].x)*(a[w].y-a[i].y)-(a[w].x-a[i].x)*(a[j].y-a[i].y));
v.push_back(area);
}
}
}
nth_element(v.begin(),v.begin()+n*(n-)*(n-)/-k,v.end());
ll ans=v[n*(n-)*(n-)/-k];
if (ans%==) printf("%lld.00\n",ans/);
else printf("%lld.50\n",ans/);
}
return ;
}

nth_element函数的更多相关文章

  1. 【快速选择算法与nth_element函数】【续UVA11300 】

    在白书中提到了一种O(n)级别的寻找中位数算法 就是我们今天要介绍的主角 快速选择算法 类似快排 选择一个比较元素 进行递归处理寻找第k大元素 假设最后比较元素到了i 以下描述是我写快排的常用字符 所 ...

  2. HDU 6040 Hints of sd0061 nth_element函数

    Hints of sd0061 Problem Description sd0061, the legend of Beihang University ACM-ICPC Team, retired ...

  3. STL之nth_element()(取容器中的第n大值)

    nth_element()函数 头文件:#include<algorithm> 作用:nth_element作用为求第n大的元素,并把它放在第n位置上,下标是从0開始计数的,也就是说求第0 ...

  4. 随机生成数组函数+nth-element函数

    这几天做了几道随机生成数组的题,且需要用nth-elemeng函数,并且都是北航出的多校题…… 首先我们先贴一下随机生成数组函数的代码: unsigned x = A, y = B, z = C; u ...

  5. 牛客2018多校第六场 J Heritage of skywalkert - nth_element

    传送门 题意:提供一个随机生成函数,让你生成n个数,然后问你其中能找到的两个数的最小公倍数 最大 是多少. 思路:可以用nth_element()函数在O(n)下求出前 15 个大的数(当然,100个 ...

  6. STL中nth_element的用法

    nth_element函数原型有四个,详细我就不一一累赘了,我们就用最普通的用法寻找第k位置的元素. 函数用法为:nth_element(first,kth,end). first,last 第一个和 ...

  7. k-d tree 学习笔记

    以下是一些奇怪的链接有兴趣的可以看看: https://blog.sengxian.com/algorithms/k-dimensional-tree http://zgjkt.blog.uoj.ac ...

  8. c++ algorithm 的用法

    1 , accumulate()template<class _II, class _Ty> inline_Ty accumulate(_II _F, _II _L, _Ty _V){fo ...

  9. 【STL源码学习】STL算法学习之四

    排序算法是STL算法中相当常用的一个类别,包括部分排序和全部排序算法,依据效率和应用场景进行选择. 明细: sort 函数原型: template <class RandomAccessIter ...

随机推荐

  1. shell编程2:数组的运用

    Shell 数组 定义数组 在Shell中,用括号来表示数组,数组元素用"空格"符号分割开.定义数组的一般形式为: name=(name1 name2 name3) 复制代码 还可 ...

  2. Matplotlib ValueError: _getfullpathname: embedded null character

    Traceback (most recent call last): File "<stdin>", line 1, in <module> File &q ...

  3. spark性能调优02-JVM调优

    1.降低cache操作的内存占比 1.1 为什么要降低cache操作的内存占比 spark的堆内存分别两部分,一部分用来给Rdd的缓存进行使用,另一部分供spark算子函数运行使用,存放函数中的对象 ...

  4. 2018-2-13-win10-uwp-活动磁贴

    title author date CreateTime categories win10 uwp 活动磁贴 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17: ...

  5. shell read变量

  6. 使用ReadStream方法读取文件事件传递过程

    const fs = require('fs'); let file = fs.createReadStream("filename.js"); file.on("ope ...

  7. 前端学习(十七)js数组(笔记)

    数组:        1.    var arr=[1,2,3,4]; 2.    var arr=new Array(1,2,3,4,5); 区别:        1.写法不一样,选择第一种(好写) ...

  8. composer安装后台模板

    先下载composer的windows安装包 cmd切换到源代码所在目录 https://www.cnblogs.com/wgphp/p/8001434.html 安装过程可以参照这篇文章 一点问题是 ...

  9. Shell4

    ssh 192.168.4.5>提示continue,连接过的主机不会提示>连接过的主机 文件存放位置:/root/.ssh/known_hosts ################### ...

  10. 芜湖市2018市队选拔Day2T1

    好激动啊,Day2竟然AK了! Day2T1养宠物 Descriptionbadgers是可爱的动物,Smart想拥有一些.宠物店提供N个badgers,编号为1..N,Smart都很喜欢,所以他想拥 ...