• XidianOJ 1175: count


    题目描述

    There is a sequence with n integers, your task is finding there are how many consecutive subsequences have two characters:
    1.The length of the subsequence is k.
    2.Every number in this subsequence is different.

    输入

    The input contains multiple test cases.
    The first line of each case contains two integers n and k (1≤k≤n≤200000). Then second line contains n positive integers a[i](1≤a[i]≤1000000000).

    输出

    For each case ,print the number of consecutive subsequences in the only line.

    样例输入

    10 3
    1 2 3 4 3 2 1 1 2 2

    样例输出

    4

    动态规划

    设dp[i]是以第i个数字结尾的满足不重复数字的最长长度,则dp[i] = dp[i-1] + 1(a[i]未出现过),dp[i] = i - m[a[i]] (a[i]出现过,使用map容器储存上个a[i]的下标)

    #include <cstdio>
    #include <cctype>
    #include <iostream>
    #include <cstring>
    #include <algorithm>
    #include <string>
    #include <vector>
    #include <map>
    #include <set>
    #include <cmath>
    using namespace std;
    typedef long long LL;
    
    LL n,k;
    LL a[200005];
    LL dp[200005] = {0};
    map<LL,LL> m;
    
    int main(){
      //freopen("test.in","r",stdin);
      while (scanf("%lld %lld",&n,&k) != EOF){
        m.clear();
        memset(dp,0,sizeof(dp));
        LL i,total = 0;
        for (i=1;i<=n;i++){
          scanf("%lld",&a[i]);
        }
        dp[1] = 1; m[a[1]] = 1;
        for (i=2;i<=n;i++){
          if (m[a[i]] == 0){
            dp[i] = dp[i-1]+1; m[a[i]] = i;
          }
          else {
            dp[i] = i - m[a[i]];
            // printf("%lld %lld
    ",i,dp[i]);
            m.clear();
            for (int j=i,k=0;k<dp[i];k++){
              m[a[j-k]] = j-k;
            }
            if (i == 5){
              //printf("%lld
    ",m[a[2]]);
            }
          }
        }
        //printf("%lld = k
    ",k);
        for (i=k;i<=n;i++){
          if (dp[i] >= k) {
          //  printf("%lld
    ",i);
            total ++;
          }
        }
        printf("%lld
    ",total);
      }
      return 0;
    }
    View Code
  • 相关阅读:
    03:信号与槽,以字体修改为例
    赞不绝口点赞器原作者的撤项信
    钉钉魔铃 铃声切换器 1.0 项目失败
    KC开发组官方网站
    搜索引擎知识
    现在的手机厂家真浮躁
    02:QT的第一个程序
    第一次做编程语言的英语阅读理解
    mysql 报错[Err] [Dtf] 1292
    Mysql 精确查询是否字段中包含某个字符串
  • 原文地址:https://www.cnblogs.com/ToTOrz/p/6735997.html
Copyright © 2020-2023  润新知