Thinking about it:
My train of thought is a bit like sliding window. Assuming that an interval [L, R] has been determined, the sequence from L to R contains exactly the elements of [1, K], then the various positions of the iteration sequence are resumed from R, and if the number of 1 to K is found, the following processing is done:
If the number is exactly the number in the L position, then it means that the interval may be shortened, and both L and R are updated to calculate the change in interval length.
If this number is not the number on the L, then even if the R is updated it does not make the answer better, so it can be done without processing.
Then the first qualifying [L, R] can be iterated directly, and if the iteration is not able to find the individual numbers of [1, K], then the sequence is definitely not satisfying.
Ps:
Although AC this problem, but feel to the problem in the formulation of the question still some lack.
Code:
/** * AC @ Sep 11th * Run time:0.739s */#include <bits/stdc++.h>using namespace Std;const int MAXN = 1000 + 5 0;int var[maxn*1000];int case = 0;int N, M, K;void init () {for (int i = 1; I <= 3; ++i) {var[i] = i; } int sum = 6; for (int i = 4; I <=n; ++i) {var[i] = sum% M + 1; Sum-= var[i-3]; Sum + = Var[i]; }}void done () {int POS[MAXN] = {0}; int counter = 0; for (int i = 1; I <= N && counter < K; ++i) {if (Var[i] >= 1 && var[i] <= K && !pos[var[i]]) {+ + counter; } Pos[var[i]] = max (Pos[var[i]], i); if (Counter < K) {cout << "case" << (++case) << ": Sequence nai" << Endl; return; } int minpos = MAXN *, Maxpos =-1; for (int i = 1; I <= K; ++i) {minpos = min (Minpos, pos[i]); Maxpos = Max (Maxpos, pos[i]); } int ans = maxpos-minpos + 1; for (int i = 1; I <= N; ++i) {if (Var[i] >= 1 && var[i] <= K) {if (pos[var[i]] = = Minpos | | pos[var[i]] = = Maxpo s | | I > Maxpos) {pos[var[i]] = i; Minpos = Maxn *, Maxpos =-1; for (int j = 1; j <= K; ++j) {minpos = min (Minpos, pos[j]); Maxpos = Max (Maxpos, Pos[j]); } ans = min (ans, maxpos-minpos + 1); }}} cout << "case" << (++case) << ":" << ans << Endl;} void work () {cin >> N >> M >> K; Init (); Done ();} int main (int argc, char const *argv[]) {Ios::sync_with_stdio (false); Cin.tie (0); int T; Cin >> T; while (T-) {work (); } return 0;}
uva11536 Smallest Sub-array