HDU 2158 simulation questions, hdu2158 simulation questions
Question:
Given a sequence, there are N integers in the range of [0, N ).
There are M inquiries. Each time you ask for a given Q integer, duplicate values may appear.
You must find a shortest interval that contains the Q integer values.
Question:
First, a whole L and R are convenient, then L is enumerated, and R is maintained at the same time, so that the range meets the question requirements, the minimum interval is updated, and the direct path does not meet the requirements.
Code:
#include<stdio.h>#include<string.h>#define N 100005int a[N], find[N], mark[N];int l, r, n, m, count, num;void Find(){ int kk = 0; for(int i = 1; i <= n; i++) { if(mark[a[i]]) { if(!find[a[i]]) kk ++; find[a[i]] ++; if(kk == num) { r = i; break ; } } } }void slove(){ for(int i = 1; i <= n; i++) { if(mark[a[i]]) { if(!(--find[a[i]])) { int flag = 0; for(int j = r+1; j <= n; j++) { if(mark[a[j]]) { find[a[j]] ++; if(a[j] == a[i]) { flag = 1; r = j; break; } } } if(!flag) break; } } if(count > r - i) count = r - i; } }int main(){ int q, p; while(scanf("%d %d", &n, &m)!=EOF) { if( n == 0 && m == 0 ) break; for(int i = 1; i <= n; i++) { scanf("%d", &a[i]); } while(m --) { memset(mark,0,sizeof(mark)); memset(find,0,sizeof(find)); scanf("%d", &q); num = 0; while(q--) { scanf("%d", &p); if(!mark[p]) num ++; mark[p] = 1; } l = 1, count = n; Find(); count = r - l + 1; slove(); printf("%d\n", count); } } }