[CodeForces-1225B] TV Subscriptions [greedy] [size emulated]

[CodeForces-1225B] TV Subscriptions [greedy] [size emulated]

Tags: problem solution codeforces explanations foot emulated


Title Description

Time limit
2000 ms
Memory limit
262144 kB
Source
Technocup 2020 - Elimination Round 2
Tags
implementation  two pointers  *1300
Site
https://codeforces.com/problemset/problem/1225/B2

Face questions

CodeForces-1225B.png

Example
Input

4
5 2 2
1 2 1 2 1
9 3 3
3 3 3 2 2 2 1 1 1
4 10 4
10 8 6 4
16 9 8
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3

Output

2
1
4
5

Subject to the effect

Given \ (n, k, d \ ) and a sequence \ (A [. 1 \ n-cdots] \) , \ (. 1 \ Leq A [I] \ Leq K \) . Q continuous sequence \ (d \) minimum number can have several different numbers?

例如,
\(n = 5, k = 2, d = 2, a[1 \cdots 5] = [1, 2, 1, 2, 1]\)。连续的两个数只有\([1, 2], [2, 1]\)两种情况都是包含2个不同的数。所以输出2;
\(n = 9, k = 3, d = 3, a[1 \cdots 9] = [3, 3, 3, 2, 2, 2, 1, 1, 1]\)。当连续的三个数为\([3, 3, 3]\)时,包含的不同数的个数最少,为1个。若为其他,如\([2, 2, 1]\)\([3, 2, 2]\),则包含2个不同的数。所以输出1。


解析

这道题用到了尺取法,尺取法在Codeforces中的标签是two pointers(双指针法)

但因为题目中规定了\(d\),即尺取的长度,两个指针只能一起移动,所以尺取的思想体现的不是很明显。

Since it takes about two feet pointers \ (l, r \) how to move the issue is resolved, then the next to be solved is how to maintain information-foot range to take? This question is the minimum number of different digital interrogation interval included. We observed that each \ (l, r \) right by one bit interval in fact only two changes, a previously \ (l \) within the meaning of the digital team, and the other is the current \ (r \ ) numbers within the meaning of the team. Then we simply check the two numbers a single input queue does not have an impact on different digital production number on it. So that we through the array \ (vis [1 \ cdots k ] \) to record the number of foot section take each number appears.

  • If \ ((VIS ++ [A [R & lt]]) =. 1 \) , then the number of different numbers is incremented.
  • If \ ((- VIS [A [L -. 1]]) = 0 \) , then a different digital data is decremented.

Such maximum foot section taken from the leftmost rightmost slide, different digital data that is recorded in the answer. The time complexity of this method is that \ (O (n-) \) .

Because multiple sets of input, so every time we have to an array \ (vis \) to zero. Note that the array zero time, if k, k cycle times will TLE.


By Code

/*
Status
    Accepted
Time
    31ms
Memory
    7836kB
Length
    937
Lang
    GNU G++11 5.1.0
Submitted
    2019-12-17 21:52:46
RemoteRunId
    67074221
*/


#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1e6 + 50;
int vis[MAXN], a[MAXN], n, k, d;

inline int read()       //快读,因为是2e5的输入量,所以加快读能明显提高程序速度.
{
    int res = 0, f = 1;
    char ch;

    ch = getchar();

    while(!isdigit(ch)){
        if(ch == '-')
            f = -1;

        ch = getchar();
    }
    while(isdigit(ch)){
        res = (res << 3) + (res << 1) + ch - 48;
        ch = getchar();
    }

    return f * res;
}

void mem0()            //vis数组归零函数.
{
    for(int i = 1; i <= n; i ++)     //注意这里要循环n次而不是k次,否则会超时.
        vis[a[i]] = 0;

    return ;
}

int main()
{
    int times;
    times = read();

    while(times --){

        int minn = 0, cnt = 0;
        mem0();

        n = read(), k = read(), d = read();

        for(int i = 1; i <= n; i ++){
            a[i] = read();
        }
        for(int i = 1; i <= d; i ++){      //先将最左端的d个数加入区间,构造好尺取区间.
            if(!vis[a[i]])  cnt ++;
            vis[a[i]] ++;
        }
        minn = cnt;
        for(int i = d + 1; i <= n; i ++){

            if(!vis[a[i]])  cnt ++;         //注意这里并没有真的使用l,r指针(下标),因为l,r的移动是同时的,所以我们就用循环的i代替r,i-d代替l-1.
            vis[a[i]] ++;

            vis[a[i - d]] --;
            if(!vis[a[i - d]])  cnt --;

            minn = min(minn, cnt);      //每次移动一位之后,看是否可以更新最小值.

        }

        printf("%d\n", minn);
    }
    return 0;
}


Guess you like

Origin www.cnblogs.com/satchelpp/p/12068814.html