zoukankan      html  css  js  c++  java
  • Day8

    Berland starts to seize the initiative on the war with Flatland. To drive the enemy from their native land, the berlanders need to know exactly how many more flatland soldiers are left in the enemy's reserve. Fortunately, the scouts captured an enemy in the morning, who had a secret encrypted message with the information the berlanders needed so much.

    The captured enemy had an array of positive integers. Berland intelligence have long been aware of the flatland code: to convey the message, which contained a number m, the enemies use an array of integers a. The number of its subarrays, in which there are at least k equal numbers, equals m. The number k has long been known in the Berland army so General Touristov has once again asked Corporal Vasya to perform a simple task: to decipher the flatlanders' message.

    Help Vasya, given an array of integers a and number k, find the number of subarrays of the array of numbers a, which has at least k equal numbers.

    Subarray a[i... j] (1 ≤ i ≤ j ≤ n) of array a = (a1, a2, ..., an) is an array, made from its consecutive elements, starting from the i-th one and ending with the j-th one: a[i... j] = (ai, ai + 1, ..., aj).

    Input

    The first line contains two space-separated integers nk (1 ≤ k ≤ n ≤ 4·105), showing how many numbers an array has and how many equal numbers the subarrays are required to have, correspondingly.

    The second line contains n space-separated integers ai (1 ≤ ai ≤ 109) — elements of the array.

    Output

    Print the single number — the number of such subarrays of array a, that they have at least k equal integers.

    Please do not use the %lld specifier to read or write 64-bit integers in С++. In is preferred to use the cin, cout streams or the %I64d specifier.

    Examples

    Input
    4 2
    1 2 1 2
    Output
    3
    Input
    5 3
    1 2 1 1 3
    Output
    2
    Input
    3 1
    1 1 1
    Output
    6

    Note

    In the first sample are three subarrays, containing at least two equal numbers: (1,2,1), (2,1,2) and (1,2,1,2).

    In the second sample are two subarrays, containing three equal numbers: (1,2,1,1,3) and (1,2,1,1).

    In the third sample any subarray contains at least one 1 number. Overall they are 6: (1), (1), (1), (1,1), (1,1) and (1,1,1).

    思路:双指针问题,和A题相似,一个指针每次递增一,另一个指针随条件改变不止一,一般是维护第一个指针的极值,然后计算数量

    const int maxm = 4e5+5;
    
    int buf[maxm], n, k;
    map<int, int> vis;
    
    int main() {
        ios::sync_with_stdio(false), cin.tie(0);
        cin >> n >> k;
        for(int i = 0; i < n; ++i)
            cin >> buf[i];
        LL ans = 0;
        int l = 0, r = 0;
        vis[buf[0]]++;
        while(l + k -1 < n) {
            while(r < n) {
                if(vis[buf[r]] >= k) break;
                r++;
                vis[buf[r]]++;
            }
            if(r == n) break;
            ans += (LL)(n - r);
            vis[buf[l]]--;
            l++;
        }
        cout << ans << "
    ";
        return 0;
    }
    View Code
  • 相关阅读:
    2019-2020-2 网络对抗技术 20175318 Exp0 kali Linux系统的安装(雾
    手动编译 TCP BBR v2 Alpha/Preview 内核
    Adobe Flash Player解除限制版
    ABBYY FineReader 15.0.18.1494 Corporate Multilingual破解
    易语言5.9精简学习增强版
    Debian9 快速开启 TCP BBR 实现高效单边加速
    ucosii-2(选做)
    Ubuntu 18.04/18.10快速开启Google BBR的方法
    基于 Nginx 的 v2+websocket+tls 域名伪装
    Google TCP升级版加速:BBR 2.0对比BBR Plus
  • 原文地址:https://www.cnblogs.com/GRedComeT/p/12228427.html
Copyright © 2011-2022 走看看