zoukankan      html  css  js  c++  java
  • Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) B2. TV Subscriptions (Hard Version)

    链接:

    https://codeforces.com/contest/1247/problem/B2

    题意:

    The only difference between easy and hard versions is constraints.

    The BerTV channel every day broadcasts one episode of one of the k TV shows. You know the schedule for the next n days: a sequence of integers a1,a2,…,an (1≤ai≤k), where ai is the show, the episode of which will be shown in i-th day.

    The subscription to the show is bought for the entire show (i.e. for all its episodes), for each show the subscription is bought separately.

    How many minimum subscriptions do you need to buy in order to have the opportunity to watch episodes of purchased shows d (1≤d≤n) days in a row? In other words, you want to buy the minimum number of TV shows so that there is some segment of d consecutive days in which all episodes belong to the purchased shows.

    思路:

    双指针遍历。

    代码:

    #include <bits/stdc++.h>
    typedef long long LL;
    using namespace std;
     
    const int MAXN = 2e5+10;
     
    int Vis[MAXN*10];
    int Day[MAXN];
    int n, k, d;
     
    int main()
    {
        ios::sync_with_stdio(false);
        int t;
        scanf("%d", &t);
        while (t--)
        {
            scanf("%d%d%d", &n, &k, &d);
            for (int i = 1;i <= n;i++)
                scanf("%d", &Day[i]);
            for (int i = 1;i <= n;i++)
                Vis[Day[i]] = 0;
            int res = k, tmp = 0;
            for (int i = 1;i <= d;i++)
            {
                if (Vis[Day[i]] == 0)
                    tmp++;
                Vis[Day[i]]++;
            }
            res = min(res, tmp);
            for (int i = d+1;i <= n;i++)
            {
                if (Vis[Day[i-d]] == 1)
                    tmp--;
                Vis[Day[i-d]]--;
                if (Vis[Day[i]] == 0)
                    tmp++;
                Vis[Day[i]]++;
                res = min(res, tmp);
            }
            printf("%d
    ", res);
        }
     
        return 0;
    }
    
  • 相关阅读:
    学习淘宝指数有感
    STL学习小结
    Java里泛型有什么作用
    android 内存泄漏分析技巧
    学道1.3
    严苛模式(StrictMode)
    年龄大了还能够学习编程吗
    ORACLE EXP命令
    数学之路-python计算实战(13)-机器视觉-图像增强
    《C语言编写 学生成绩管理系统》
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11772261.html
Copyright © 2011-2022 走看看