Luo Gu P1638 visiting the exhibition problem solution

P1638 visiting the exhibition

Title Description

AsiaWorld-Expo is being exhibited by the best in the world M painters painted pictures.

wangjy think Expo to see that several masters.

However, where the Expo have a very strange rule is that two figures must be stated in the purchase of tickets,

a and b, between the representatives of all the pictures he wanted to see the exhibition of a web through b picture (and contains a b), while tickets

The price is a picture of one yuan.

To see more teacher's paintings, wangjy would like to see a picture after the admission of all teachers (at least each one).

But he wanted to save money. . .

As wangjy friend, he asks you to write a program that he decided a and b values ​​when buying tickets.

Input Format

The first row is N and M, representing the total number of pictures and Expo hall The pictures are drawn by how many bits the teacher

The painting.

Subsequent row comprising N digital, they are between 1 and M, representing the number of bits teacher.

Output Format

a and b (a <= b) is separated by a space character.

To ensure a solution, if multiple solutions, minimum output a.

Sample input and output

Input # 1

12 5
2 5 3 1 3 2 4 1 1 5 4 3

Output # 1

2 7

Description / Tips

30% of the agreed data N <= 200, M <= 20

60% of the data N <= 10000, M <= 1000

100% data N <= 1000000, M <= 2000

[Thinking]

Double pointer
enumerated a starting
end b (tail pointer) accumulated
until a teacher can be seen that each of the paintings
and then comparing the length of the recording interval
if it is small the interval a, b replace recorded before

How to determine whether every teacher's paintings are seen?
With a bucket,
if the teacher's paintings join it accumulated on his bucket
if it is from 0 to 1 a barrel that is added to a new artist
so the counter counts (counter used to count the currently visible the number of artist paintings)
If it is a change from 1 to 0 in the bucket
that is less to see an artist's paintings
so this time to counter regressive

[Complete code]

#include<iostream>
#include<cstdio>

using namespace std;
int l = 1,r = 1;
int acioi[1000006];
int tong[2020];
int js = 0;
int L,R,M = 0x7fffffff;

int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(register int i = 1;i <= n;++ i)
        scanf("%d",&acioi[i]);
    tong[acioi[1]] ++;
    js ++;
    for(l = 1,r = 1;l <= n;++ l)
    {
        tong[acioi[l - 1]] --;
        if(tong[acioi[l - 1]] == 0)
            js --;
        while(js < m && r <= n)
        {
            ++r;
            tong[acioi[r]] ++;
            if(tong[acioi[r]] == 1)
                js ++;
        }
        if(js == m && r - l + 1 < M)
        {
            M = r - l + 1;
            L = l;R = r;
        }
    }
    cout << L << " " << R << endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/acioi/p/11645626.html