强大的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 ;
}

最新文章

  1. 【Raspberry Pi】新入手的Raspberry Pi3拼装日志
  2. 简单BigDecimal运算精度
  3. ARPACK在window visual Studio的安装配置
  4. 点亮一个led灯
  5. cocos基础教程(12)点击交互的三种处理
  6. C# const与static的理解
  7. Linux服务器集群系统(二)--转
  8. Java中快速排序的实现
  9. TeamCity vs Jenkins: Which is the Better Continuous Integration (CI) Server for .NET Software Development?
  10. elasticsearch映射
  11. RDD概念、特性、缓存策略与容错
  12. apache tomcat集群
  13. 2019.03.09 bzoj4491: 我也不知道题目名字是什么(线段树)
  14. Python内置模块之random
  15. hive web界面管理
  16. simple简单消息队列
  17. TTL集成门电路工作原理和电压传输特性
  18. PS中标尺工具在哪里
  19. CSS控制滚动条的样式
  20. nodejs rar/zip加密压缩、解压缩

热门文章

  1. Qt5配置winpCap
  2. Delphi 动态链接库编程
  3. websocket 多聊天室功能
  4. fragment初步认识
  5. contos7 yum 安装golang
  6. Atcoder CODE FESTIVAL 2016 Final G - Zigzag MST[最小生成树]
  7. 洛谷-p5410-扩展KMP模板
  8. Oracle查询死锁
  9. jmeter-模板报错-Fatal Error! com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: 2 字节的 UTF-8 序列的字节 2 无效
  10. xcode打正式包提示缺少icon图标解决方法