Ultraviolet A 11572-unique snowflakes (good question)

Source: Internet
Author: User

Question Link

Question:

Give N number, n <= 100 W, and find a continuous subsequence with no duplicates in it. What is the maximum length of this subsequence?

Ideas:

Open an array pos. Pos [x] indicates the position where x appears. The array is initialized to-1.

Use the start variable to record the start point of the current enumeration sequence. The initial value is 0.

Then enumerate this sequence and record the positions of each number in sequence,

Assume that the current enumeration is to I. before recording this position, check whether POS [arr [I] is greater than or equal to start. If it is greater, note that this number has already appeared in [start, I-1], and write down the length of the Child sequence that meets the condition. Then enumerate the next subsequence: Start = POS [I] + 1. ThisAlgorithmThe total complexity is O (n)

Code:

/** Ultraviolet A 11572 unique snowflakes **/# include <iostream> # include <cstdio> # include <cstring> using namespace STD; typedef long int64; const int INF = 0x3f3f3f3f; const int maxn = 1000010; int arr [maxn]; int POS [maxn]; int main () {int ncase, m, n; scanf ("% d ", & ncase); While (ncase --) {scanf ("% d", & N); int CNT = 0; For (INT I = 0; I <N; ++ I) scanf ("% d", & arr [I]); memset (Pos,-1, sizeof (POS); int start = 0; int Maxx = 0; arr [N] = arr [n-1]; for (INT I = 0; I <= N; ++ I) {If (Pos [arr [I]> = Start) {int TMP = I-start; Maxx = max (TMP, Maxx ); start = POS [arr [I] + 1; POS [arr [I] = I;} else {pos [arr [I] = I ;}} printf ("% d \ n", Maxx);} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.