强大的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. MySql学习笔记【四、数据相关操作】

    CURD--增改查删 创建数据 INSERT [INTO] tb_name [(col_name,...)] VALUES(val,..) 若列名缺省,表示插入全部列,也可指定部分列名 如: INSE ...

  2. go语言json转map

    package util import ( "encoding/json" "fmt" ) // json转map函数,通用 func JSONToMap(st ...

  3. centos redis自启动

    #!/bin/sh # chkconfig: 2345 90 10 # description: Redis is a persistent key-value database # Simple R ...

  4. deep_learning_cross_entropy

    交叉熵损失函数 交叉熵代价函数(Cross-entropy cost function)是用来衡量人工神经网络(ANN)的预测值与实际值的一种方式.与二次代价函数相比,它能更有效地促进ANN的训练.在 ...

  5. 使用js打印时去除页眉页脚

    写在前面 今天的开发遇到了使用window.print()功能进行当前页面打印的功能,因为页脚左边部分显示了url,这是不能存在的,已解决,写在这里. 正文 很多网上的方法都是不能用的,最后我找到一个 ...

  6. thinkphp5 二维码生成 composer

    进入extend文件夹 composer require endroid/qrcode 2.将二维码生成封装为服务 QrcodeServer.php代码如下: <?php /** * Creat ...

  7. 6.Nginx的session一致性(共享)问题配置方案2

    1.利用memcached配置session一致性的另外一种方案tengine的会话保持功能 1.1:Tengine会话保持:通过cookie来实现的 该模块是一个负载均衡模块,通过cookie实现客 ...

  8. 浅入深出Vue:文章列表

    终于到我们小项目的最后一个功能了,那就是列表页展示! 新建组件 先来新建组件 List.vue: <template> <div></div> </templ ...

  9. JAVA面试/笔试经典题

    1.short s1 = 1; s1 = s1 + 1;有什么错? short s1 = 1; s1 += 1;有什么错? 对于short s1 = 1; s1 = s1 + 1; 由于s1+1运算时 ...

  10. ios11返回按钮问题

    在苹果系统升级到iOS11之后,页面的返回按钮的点击区域是根据设置的按钮的frame来确定的,在设置按钮太小的时候,点击就会出现点击多次才能点击到一次的现象,处理的方法就是设置按钮的frame变大代码 ...