zoukankan      html  css  js  c++  java
  • 强大的unique

    强大的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=0;
        char last=' ',ch=getchar();
        while(ch<'0'||ch>'9') last=ch,ch=getchar();
        while(ch>='0'&&ch<='9') ans=ans*10+ch-'0',ch=getchar();
        if(last=='-') ans=-ans;
        return ans;
    }
     
    int n,k;
    int a[10010];
    
    int main()
    {
        n=read();k=read();
        for(int i=1;i<=n;i++) a[i]=read();
        sort(a+1,a+n+1);
        int kk=unique(a+1,a+n+1)-(a+1);
        if(k>kk) printf("NO RESULT
    ");
        else printf("%d
    ",a[k]);
        return 0;
    }
    
     

    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=0;
        char last=' ',ch=getchar();
        while(ch<'0'||ch>'9') last=ch,ch=getchar();
        while(ch>='0'&&ch<='9') ans=ans*10+ch-'0',ch=getchar();
        if(last=='-') ans=-ans;
        return ans;
    }
    
    int n,ans=0;
    int a[10005];
    
    int main()
    {
        n=read();
        for(int i=1;i<=n;i++)
          a[i]=read();
        int kk=unique(a+1,a+n+1)-(a+1);
        for(int i=1;i<=kk;i++)
        {
            if(i==1||i==kk) continue;
            if(a[i-1]>a[i]&&a[i]<a[i+1])
             ans++;
        }
          
        printf("%d",ans);
        return 0;
    }
  • 相关阅读:
    Sql Server 2008卸载后再次安装一直报错
    listbox 报错 Cannot have multiple items selected when the SelectionMode is Single.
    Sql Server 2008修改Sa密码
    学习正则表达式
    Sql Server 查询第30条数据到第40条记录数
    Sql Server 复制表
    Sql 常见面试题
    Sql Server 简单查询 异步服务器更新语句
    jQuery stop()用法以及案例展示
    CSS3打造不断旋转的CD封面
  • 原文地址:https://www.cnblogs.com/xiaoyezi-wink/p/11257163.html
Copyright © 2011-2022 走看看