The main topic: according to the requirements of the topic to construct a sequence, find the shortest sub-sequence, including 1~k.
Problem-solving ideas: First, according to the method of the topic constructs the sequence, then uses the Towpointer method, uses the v[i] to record the current [L, R] has several I, when R moves, appears v[i] = = 1 o'clock, C + + (used to record several 1~k numbers); when c = = k, move L, when V [i] = = 0 o'clock, c--。
1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5 using namespacestd;6 7 Const intN =1000005;8 9 intN, M, K, V[n], g[n];Ten One voidInit () A { -Memset (G,0,sizeof(g)); -memset (V,0,sizeof(v)); the -CIN >> n >> M >>K; -g[1] =1, g[2] =2, g[3] =3; - for(inti =4; I <= N; i++) +G[i] = (g[i-1] + g[i-2] + g[i-3])%m +1; - } + A BOOLSolve () at { - intL =1, r =1, C =0; - intAns = n +1; - while(R <=N) - { - intt = g[r++]; inv[t]++; - if(t <= k && v[t] = =1) C + +; to + while(L < r && c = =k) - { theans = min (ans, R-l); *t = g[l++]; $v[t]--;Panax Notoginseng if(t <= k && v[t] = =0) c--; - } the } + if(Ans <=N) A { theprintf"%d\n", ans); + return false; - } $ return true; $ } - - intMain () the { - intCAs;WuyiCIN >>CAs; the for(inti =1; I <= cas; i++) - { Wu init (); -printf"Case %d:", i); About $ if(Solve ()) printf ("sequence nai\n"); - } - return 0; -}
Feel the code good not easy to feel,-:D
Can you use set to solve it like this?
Can't seem to, what about the other methods?
UVA 11536-smallest Sub-array