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;
    }
    
  • 相关阅读:
    关于等价类测试的简单实践 20150322
    对软件测试的理解 20150314
    pthread_wrap.h
    libuv 错误号UV_ECANCELED 的处理
    简单的后台日志组件
    Windows NTService 后台框架封装
    检查程序进程是否存在/强制杀掉程序进程
    析构函数结束线程测试
    移动天线
    猜数字游戏的Java小程序
  • 原文地址:https://www.cnblogs.com/grcyh/p/10201435.html
Copyright © 2011-2022 走看看