UVA 11536-smallest Sub-array

Source: Internet
Author: User

The main topic: according to the requirements of the topic to construct a sequence, find the shortest sub-sequence, including 1~k.

Problem-solving ideas: First, according to the method of the topic constructs the sequence, then uses the Towpointer method, uses the v[i] to record the current [L, R] has several I, when R moves, appears v[i] = = 1 o'clock, C + + (used to record several 1~k numbers); when c = = k, move L, when V [i] = = 0 o'clock, c--。

1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5 using namespacestd;6 7 Const intN =1000005;8 9 intN, M, K, V[n], g[n];Ten  One voidInit () A { -Memset (G,0,sizeof(g)); -memset (V,0,sizeof(v)); the  -CIN >> n >> M >>K; -g[1] =1, g[2] =2, g[3] =3; -      for(inti =4; I <= N; i++) +G[i] = (g[i-1] + g[i-2] + g[i-3])%m +1; - } +  A BOOLSolve () at { -     intL =1, r =1, C =0; -     intAns = n +1; -      while(R <=N) -     { -         intt = g[r++]; inv[t]++; -         if(t <= k && v[t] = =1) C + +; to  +          while(L < r && c = =k) -         { theans = min (ans, R-l); *t = g[l++]; $v[t]--;Panax Notoginseng             if(t <= k && v[t] = =0) c--; -         } the     } +     if(Ans <=N) A     { theprintf"%d\n", ans); +         return false; -     } $     return true; $ } -  - intMain () the { -     intCAs;WuyiCIN >>CAs; the      for(inti =1; I <= cas; i++) -     { Wu init (); -printf"Case %d:", i); About          $         if(Solve ()) printf ("sequence nai\n"); -     } -     return 0; -}

Feel the code good not easy to feel,-:D

Can you use set to solve it like this?

Can't seem to, what about the other methods?

UVA 11536-smallest Sub-array

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.