UVA 11572 Unique Snowflakes (sliding window)

Source: Internet
Author: User

Think is the data water the problem, see some directly with the array to save a value last occurrence of the position, OK 0 to 1e9 of the data range it?

Use set, save the current interval of the number of occurrences, the next number, did not appear, join, until the repetition, and then delete the left end, continue ...

#include <bits/stdc++.h>using namespacestd;Const intMAXN = 1e6+2;intA[MAXN];intMain () {intT scanf"%d",&T);  while(t--){        intN scanf"%d",&N);  for(inti =0; I < n; i++) scanf ("%d", A +i); Set<int>s; intL =0, R =0, ans =0;  while(R <N) {             while(R < n &&!s.count (A[r]) S.insert (a[r++]); Ans= Max (ans,r-L); S.erase (A[l++]); } printf ("%d\n", ans); }    return 0;}

The array version, and finally to the end of the time to be counted once, so a[n] = a[n-1].

#include <bits/stdc++.h>using namespacestd;Const intMAXN = 1e6+2;intA[MAXN];intPOS[MAXN];intMain () {intT scanf"%d",&T);  while(t--){        intN scanf"%d",&N);  for(inti =0; I < n; i++) scanf ("%d", A +i); memset (POS,-1,sizeof(POS)); intL =0, ans =0; A[n]= a[n-1];  for(inti =0; I <= N; i++){            if(Pos[a[i]] >=L) {ans= Max (ans,i-L); L= pos[a[i]]+1; } Pos[a[i]]=i; } printf ("%d\n", ans); }    return 0;}

UVA 11572 Unique Snowflakes (sliding window)

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.