强大的unique

两道红题为例眼熟一下unique

P1138 第k小整数

题解

这里用到了STL的去重函数哦

unique

首先你有一个待处理的数组 a[n]

一定要先排序鸭  sort( a+1 , a+n+1 )

然后  int  nn=unique( a+1 , a+n+1 ) - (a+1)

这个nn就是去重后的数组长度,不重复而且排好序的数字就存在 a[1]~a[nn] 啦

重复的数字放在后面了 a[nn+1]~a[n]

注意这题有无解的情况QWQ

代码

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<queue> using namespace std; inline int read()
{
int ans=;
char last=' ',ch=getchar();
while(ch<''||ch>'') last=ch,ch=getchar();
while(ch>=''&&ch<='') ans=ans*+ch-'',ch=getchar();
if(last=='-') ans=-ans;
return ans;
} int n,k;
int a[]; int main()
{
n=read();k=read();
for(int i=;i<=n;i++) a[i]=read();
sort(a+,a+n+);
int kk=unique(a+,a+n+)-(a+);
if(k>kk) printf("NO RESULT\n");
else printf("%d\n",a[k]);
return ;
}

P1317 低洼地

题解

你考虑unique不先排序就去重

你发现它会把相邻的重复的给去掉
 
               就好比啊    0 1 2 2 1 1 0 1 0 1
 排序之后去重就是    0 1 2 0 0 1 1 1 1 2
     不排序去重就是    0 1 2 1 0 1 0 1 1 2
 
然后你就可以巧妙的利用这个阔怕的性质
 

代码

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstdlib> using namespace std; inline int read()
{
int ans=;
char last=' ',ch=getchar();
while(ch<''||ch>'') last=ch,ch=getchar();
while(ch>=''&&ch<='') ans=ans*+ch-'',ch=getchar();
if(last=='-') ans=-ans;
return ans;
} int n,ans=;
int a[]; int main()
{
n=read();
for(int i=;i<=n;i++)
a[i]=read();
int kk=unique(a+,a+n+)-(a+);
for(int i=;i<=kk;i++)
{
if(i==||i==kk) continue;
if(a[i-]>a[i]&&a[i]<a[i+])
ans++;
} printf("%d",ans);
return ;
}

强大的unique的更多相关文章

  1. jQuery数组($.each,$.grep,$.map,$.merge,$.inArray,$.unique,$.makeArray)处理函数详解

    1. $.each(array, [callback]) 遍历[常用] 解释: 不同于例遍jQuery对象的$().each()方法,此方法可用于例遍任何对象.回调函数拥有两个参数:第一个为对象的成员 ...

  2. Laravel的unique和exists验证规则的优化

    本文是Laravel实战:任务管理系统(一)的扩展阅读 原文链接:http://pilishen.com/posts/Improvements-to-the-Laravel-unique-and-ex ...

  3. 【原创】大叔案例分享(3)用户行为分析--见证scala的强大

    一 场景分析 用户行为分析应用的场景很多,像线上网站访问统计,线下客流分析(比如图像人脸识别.wifi探针等),比较核心的指标有几个: PV | UV | SD | SC 指标说明: PV(Page ...

  4. 朱晔和你聊Spring系列S1E10:强大且复杂的Spring Security(含OAuth2三角色+三模式完整例子)

    Spring Security功能多,组件抽象程度高,配置方式多样,导致了Spring Security强大且复杂的特性.Spring Security的学习成本几乎是Spring家族中最高的,Spr ...

  5. {MySQL完整性约束}一 介绍 二 not null与default 三 unique 四 primary key 五 auto_increment 六 foreign key 七 作业

    MySQL完整性约束 阅读目录 一 介绍 二 not null与default 三 unique 四 primary key 五 auto_increment 六 foreign key 七 作业 一 ...

  6. iOS 强大第三方资源库

    Github用法 git-recipesGit recipes in Chinese. 高质量的Git中文教程. lark怎样在Github上面贡献代码 my-git有关 git 的学习资料 giti ...

  7. 将VIM打造成强大的IDE

    转载自:所需即所获:像 IDE 一样使用 vim 如侵犯您的版权,请联系:2378264731@qq.com --------------------------------------------- ...

  8. 强大的JQuery数组封装使用

    JQuery对数组的处理非常便捷并且功能强大齐全,一步到位的封装了很多原生js数组不能企及的功能.下面来看看JQuery数组的强大之处在哪. $.each(array, [callback]) 遍历 ...

  9. .Net强大的列表控件XPTable [转]

    .Net强大的列表控件XPTable http://blog.csdn.net/bodybo/article/details/7359531 XPTable的大名,想必C#开发的人都有所耳闻,使用下来 ...

随机推荐

  1. OGG学习笔记03

    OGG学习笔记03-单向复制简单故障处理 环境:参考:OGG学习笔记02-单向复制配置实例实验目的:了解OGG简单故障的基本处理思路. 1. 故障现象故障现象:启动OGG源端的extract进程,da ...

  2. hadoop 中ALL Applications 中Tracking 下History查找不到MapReduce Job 日志

    运行一个Map Reduce job 想查看日志: 点击History ,找不到网页 解决办法如下: 1.其中有一个进程是需要启动的: Hadoop自带了一个历史服务器,可以通过历史服务器查看已经运行 ...

  3. 安装openblas库

    http://www.openblas.net/ Linux:下载源码直接make即可

  4. Bridge的数据在内核处理流程

    转:http://blog.sina.com.cn/s/blog_67cc0c8f0101oh33.html 转载一篇Bridge的数据在内核处理流程,文章写的不错啊! (2013-07-05 16: ...

  5. 给移动硬盘装win10,知道这些就足够了

    随着制造工业的不断发展,储存介质逐渐廉价化,以某猫和某狗为代表的电商平台上都能轻松买到大容量的原装移动硬盘.如果工厂的产品还不能满足你的要求,那么DIY是一个不错的选择,可以选择购买移动硬盘盒(2.5 ...

  6. (八)zabbix获取到的数值自定义单位

    1) 查找php文件 # find / -name "func.inc.php" /usr/share/zabbix/include/func.inc.php 2)修改文件 #vi ...

  7. Myeclipse中左边的项目目录没了

    切换一下Perspective, java, resource都有项目窗口 具体的 Window->Open Perspective 如果项目窗口被关了的话 windows->show v ...

  8. [Git] How to revert one file changes from one commit

    Many times we might changed one file which we don't intent to do... but it was too late, until we fo ...

  9. BFC的理解

    一.BFC概念 BFC即Block Formatting Contexts(块级格式化上下文),它属于普通流.它是页面中的一块渲染区域,并且有一套渲染规则,它决定了其他子元素将如何定位,以及和其他元素 ...

  10. RestFul是啥

    1. 什么是REST REST全称是Representational State Transfer,中文意思是表述(编者注:通常译为表征)性状态转移. 它首次出现在2000年Roy Fielding的 ...