Find consecutive numbers
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total Submission (s): 1008 Accepted Submission (s): 362
problem DescriptionThe little Bear gets an unordered array, and for this array, the little bear wants to know if a K interval can be found, and the number of k in the sequence is sequential.
Now the small bear increases the difficulty of the problem, he does not want to know whether there is such a range of K, but want to know that there are several such k interval.
InputThe input contains a set of test data.
The first line contains two integers n,m,n represents how many numbers are in the array, and M represents the number of queries against this array, n does not exceed 10 4, and M does not exceed 1000. The second line contains n positive integers, and the number I represents is the number on the I-bit of the unordered array, and the number does not exceed 2 31. Next m line, each line a positive integer k, meaning see the title description, K size will not exceed 1000.
OutputThe first line loses "case #i:". (because there is only one set of samples, only "case #1:" can be output)
Then for each queried K, the output line contains an integer that represents the number of intervals in the array that satisfy the size of the condition K.
Sample Input
6 23 2 1 4 3 534
Sample Output
Case #1:22
Source2015 Baidu Star Program Design Competition-Preliminary (1)
Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5247
Problem analysis: Using set, offline preprocessing satisfies the conditions of each length interval number, because set comes in order, so once a continuous interval without repeating number of the maximum value minus the minimum value plus the interval length, then this interval is the legal interval we are looking for, recorded
#include <cstdio> #include <cstring> #include <algorithm> #include <set>using namespace Std;int Const MAX = 1e4 + 5;int A[max], Ans[max];set <int> s;int main () {int n, m, CA = 1; while (scanf ("%d%d", &n, &m)! = EOF) {printf ("Case #%d:\n", CA + +); memset (ans, 0, sizeof (ans)); for (int i = 0; i < n; i++) scanf ("%d", &a[i]); for (int i = 0; i < n; i++) {s.clear (); S.insert (A[i]); ANS[1] + +; for (int j = i + 1; j < N; j + +) {Set <int>:: Iterator it; it = S.find (A[j]); if (It! = S.end ()) break; S.insert (A[j]); int st = *s.begin (); It--; int ed = *it; int sz = S.size (); if (ed-st + 1 = = SZ) Ans[sz] + +; }} for (int i = 0; i < m; i++) {int k; scanf ("%d", &k); printf ("%d\n", Ans[k]); } }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 5247 Search consecutive number (set magical)