zoukankan      html  css  js  c++  java
  • CF616D Longest k-Good Segment

    题目描述

    给定一个包含(n)个整数的序列(a)(0le a_i le 10^6),询问不重复数字个数(le k)的最长区间的左右端点。如果有多解输出任意一组。

    输入输出格式

    输入格式:

    The first line contains two integers (n,k) ( (1 leq k leq n leq 5·10^{5})) — the number of elements in (a) and the parameter (k) .

    The second line contains nn integers (a_{i})(( 0<=a_{i}<=10^{6})) — the elements of the array (a) .

    输出格式:

    Print two integers (l,r) ((1<=l<=r<=n)) — the index of the left and the index of the right ends of some k-good longest segment. If there are several longest segments you can print any of them. The elements in aa are numbered from (1) to (n) from left to right.

    输入输出样例

    输入样例#1:

    5 5
    1 2 3 4 5
    

    输出样例#1:

    1 5
    

    输入样例#2:

    9 3
    6 5 1 2 3 2 1 4 5
    

    输出样例#2:

    3 7
    

    输入样例#3:

    3 1
    1 2 3
    

    输出样例#3:

    1 1
    

    思路:题意为在长度为(n)的串中找出有k个不同数字的最长连续子串,输出子串开始以及结束的位置,然而数据有点水,我们直接用(STL)中的(map)记录一下即可。

    代码:

    #include<cstdio>
    #include<algorithm>
    #include<map>
    #define maxn 1000007
    using namespace std;
    map <int,int> mp;
    int a[maxn],l,len,cyh,zrj;
    void del(int x)
    {
        mp[x]--;
        if(mp[x]==0) mp.erase(x);
    }
    int main() {
      int n,k;
      scanf("%d%d",&n,&k);
      for(int i=0;i<n;++i) scanf("%d",&a[i]);
      for(int i=0;i<n;++i) {
        mp[a[i]]++;
        if(mp.size()>k) {
          for(;l<n&&mp.size()>k;++l)
            del(a[l]);
        }
        int ll=i-l+1;
        if(ll>len) {
          len=ll;
          cyh=l;
          zrj=i;
        }
      }
      printf("%d %d
    ",cyh+1,zrj+1);
      return 0;
    }
    
  • 相关阅读:
    element-ui Checkbox 实现单选
    鼠标移到到div上,div放大
    linux下hue的安装与部署
    window10搭建pyspark(超级详细)
    Spark的checkpoint源码讲解
    Phoneix(一)简介及常用命令
    Phoneix(四)hbase导入数据同时与phoenix实现映射同步
    Phoneix(三)HBase集成Phoenix创建二级索引
    Hive数据导入Hbase
    Spark Streaming 与Filnk对比分析
  • 原文地址:https://www.cnblogs.com/grcyh/p/10201435.html
Copyright © 2011-2022 走看看