Title Description
Description
Give you a string of numbers of length n, which will contain 1-m numbers. If a contiguous number of substrings contains 1-m these numbers, the number string is said to be num strings. Your task is to find out what the shortest num string is, just output this length.
1<=n,m<=200000
Enter a description
Input Description
The first line is given N and M.
The second row n number, representing the number string, between the numbers separated by a space.
Output description
Output Description
If the NUM string is present, output the shortest num string length, otherwise output "NO".
Sample input
Sample Input
5 3
1 2 2) 3 1
Sample output
Sample Output
3
Data range and Tips
Data Size & Hint
Each test point 1s
/*set the L and R sequence of the left and right end points, the length is r-l+1, first increase R, and count the number of times each number appears, 1~m all together, and then increase the L, when a number appears more than 1 times, you can abandon it until a certain number appears once, it must be the edge of the loop to judge, Because in the process of the cycle, there is constant to meet in the 1~m in the number of add in, so the optimal solution is constantly updated. */#include<cstdio>#include<iostream>#defineM 200010#defineINF 0x3f3f3f3fusing namespacestd;intA[m],sum[m];intMain () {intn,m; scanf ("%d%d",&n,&m); for(intI=1; i<=n;i++) scanf ("%d",&A[i]); intL=1, cnt=0, ans=INF; for(intR=1; r<=n;r++) { if(a[r]<1|| A[R]>M)Continue; if(!sum[a[r]]) cnt++; SUM[A[R]]++; if(cnt==m) { for(inti=l;i<=n;i++) { if(sum[a[i]]>1) {Sum[a[i]]--; L++; } Else Break; } ans=min (ans,r-l+1); } } if(cnt!=m) printf ("NO"); Elseprintf"%d", ans); return 0;}
View Code
Digital string (Codevs 1394)